Monday, 7 November 2022

Pre-requisites for creating Second-Generation Managed Packages

 1. Enable Dev Hub in your Org.

2. Enable Second-Generation Managed Packaging.

3. Install Salesforce CLI.

4. Create and Register Your Namespace


Note : Developers who work with 2GP packages need the correct permission set in the Dev Hub org.

       Developers need either the System Administrator Profile or the Create and Update Second-Generation Packages permission.

       

Namespace:


1.The namespace of a managed package is created in a namespace org and linked to the Dev Hub.

we can associate multiple namespaces to a single Dev Hub.

A namespace is linked with a 2GP when you run the force:package:create Salesforce CLI command And you must specify the namespace in the sfdx-project.json file.


2.Multiple packages can use the same namespace.


3.Multiple packages sharing the same namespace can share code using public 

Apex classes and methods @namespaceAccessible annotation.


 

How to register/link a namespace ?


Login to your Dev Hub org as the System Administrator or as a user with the Salesforce DX Namespace Registry Permissions.


Some of the orgs that you use with second-generation managed packaging have a unique purpose.


1.Choose your Dev Hub Org.

2.Namespace Org.

3.Other Orgs.



Purpose of Dev Hub :


As owner of all second-generation managed packages.

To link your Namespaces.

To authorize and run your force:package commands.

Note : Salesforce recommend that your Partner Business Org is also your DevHub Org.


Purpose of Namespace Org :


The primary purpose of the namespace org is to acquire a package namespace.

If you want to use the namespace strictly for testing,choose a disposable namespace.


After you create a namespace org and specify the namespace in it, open the Dev Hub org 

and link the namespace org to the Dev Hub org.


purpose of Other Orgs :


When you work with packages, you also use these orgs: you can ceate scratch orgs on the fly to use while testing your packages.

The target or installation org is where you install the package.

Behavior of Salesforce data in Apex tests

 1.Existing org data isn't visible to Apex tests.

2.As a best practice you should always create your own test data for each test.


Reasons to create your own test data :


1.Data unavailability

  your org might not have the data for both positive and negative tests.

2.Data inconsistency

  Data can be different between orgs and test runs.


Note : In some scenarios like testing report data or field history records 

        you might need to have access to our org data because you can't create 

         those records through apex.

       In those cases you can annotate your test class with 'seeAllData' is equal to true annotation.


  ex : @isTest(seeAllData=true)

       public class DemoCtrlTest{

          

          @isTest

          static void testAccountFilter(){

          

          }

       

       }  


3. Apex test data is transient and isn't committed to the database.


Note : which means the data is present only as long as your test is running.


4.Testsetup methods are helpful test setup methods are defined in a test class itself

   they take no arguments and no return a value.

   

   ex 

      @istest

      public class DemoCtrlTest{

      

      @testSetup 

       static void createTestData(){

         // create test data here

       }

      }


a. Test setup method is automatically executed first, before any test method.

b. Records created in the test setup method are accessible to all test methods.

c. Changes to records by a test method are rolled back before another test method is executed.

d. All records are rolled back after the test class finishes executing.


create test data using CSV file :


ex : @testSetup 

      static void creatTestData(){

        Test.loadData(Account.sobjectType,'Mock_Account_Data');

      }   


Test Data and Governor Limits :


Whenever a test class runs the test data creation and test execution both happens in the same transaction.   


ex : 

Test.startTest();

Test.stopTest();


The startTest() method marks the point in your test code where the test 

actually begins and stopTest() method marks the point where your test ends.


Any code that executes between these two methods is assigned a 'new set of governor limits '.

The code inside start and stop block has one set of limits and the code outside has another set of limits.

The code outside whether before or after that start and stop methods shares the same limit.


Wednesday, 26 October 2022

Deleting Salesforce Debug Logs

 Using SFDX:


COMMAND 1:


sfdx force:data:soql:query -q "SELECT Id FROM ApexLog" -r csv > apexlogs.csv


COMMAND 2:


sfdx force:data:bulk:delete -s ApexLog -f apexlogs.csv


Using Workbench:


Navigate to queries -> SOQL Query:


Select Bulk CSV and Input “Select id from ApexLog” to query Box then click Query.


Then download the CSV file by clicking on “download completed batch results” icon under Batches


Once you have the csv file: Navigate to data->delete. Then select from file, choose the downloaded file and click next.


Select Process records asynchronously via Bulk API and choose Object Type : ApexLog then click Confirm Delete. Done

How to add connected app in 2GP Managed Package?

 1.Create a connected App in Salesforce Instance where we maintain namespace.

2.Create a first-generation Managed package(1GP) and add the connected app.

it's fine if the connected app is the only component in the package.

Always use the same namespace as the 2GP package for the 1GP package.

3.Take note of the version number of the connected app,this number is needed later.

4.Upload the 1GP package to create a package version.

5.Promote the 1GP version to the released state.

6.Promoting the 1GP version allows the connected app to be included in a second-generation managed package.

we don't need to install the 1GP version into an org.

7.Now in our source code navigate to folder 'connectedApps' where we are trying to generate 2GP Package.


create an XML file with <connectedAppName>.connectedApp-meta.xml

and the body of the XML file should be as shown below.


<ConnectedApp xmlns="http://soap.sforce.com/2006/04/metadata">

    <developerName><namespace>__<connected_app_Name></developerName>

    <label>A Connected App</label>

    <version>1.0</version>

</ConnectedApp>


Now generate a 2GP package and promote. Then the connected app is automatically added to your 2GP Package.


The version specified in the source file is the version number of the connected app. Use decimal formatting when specifying the version number. 

The version number must match the version number of the connected app before it was added to the 1GP managed package.


Note:


When you add a connected app to a 1GP package and upload the package, the version number of the connected app is auto-incremented. 

For example, when version 1.0 of a connected app is added to a 1GP package, the package version increments the version number 

of the connected app from 1.0 to 2.0. When creating the source file for your 2GP package, specify the version number of the connected app 

before it was uploaded into a 1GP package, in this case, 1.0.


Reference:


https://developer.salesforce.com/docs/atlas.en-us.224.0.sfdx_dev.meta/sfdx_dev/sfdx_dev_dev2gp_connected_app.htm


Saturday, 15 October 2022

Salesforce : Order of Execution

 System Validation Rules (SVR)

Before-Save RT Flows

Apex Before Triggers

Runs Most SVR Again and all Custom Validation Rules

Duplicate Rules

Save Record but does not Commit.

Apex After Triggers

Assignment Rules

Auto-Response Rules

workflow Rules

Escalation Rules

Process/Autolaunched Flows

After-Save RT Flows

Rollup Summary Fields

Commits All DML Operations to Database

Post-Commit Logic Like Email Send


Sunday, 14 August 2022

Salesforce Shield - Data Monitoring and Data Encryption

 Data Encription :


The process of applying a cryptographic function to data that results 

in ciphertext,also known as encrypted data.


Salesforce Shield :


Salesforce Shield is a suite of products that gives you more control over your security and monitoring of sensitive data.


Platform Encription :


Platform encryption is Salesforce's product that gives you a point-and-click way to encrypt data at rest.

Platform encryption also allows you to select objects and fields that will be encrypted and what key and schema that will be used for the encryption.


Event monitoring :


Event monitoring is the counterpoint of that data security. 

It enables you to monitor how that data is accessed in the platform and also taken out of that platform. 

Within event monitoring, you can set up policies that will monitor specific criteria that has been met and proactively block or notify you when that data is being accessed.


Common Terms :


1.Data Encription keys :


Keys used to encrypt and decrypt data on the database.


2.Encription at Rest :


Data that is store on disk in an encrypted state.


3.Bring your Own Key :


When you're able to bring your own material to encryption.


Tenant Secret :


A piece of the encryption credential that is specific to your organization.


Note :


A tenant secret is an organization-specific secret used in conjunction with the master secret Salesforce has to generate the information needed to actually encrypt your data.


Shield Features :


1.Specify what fields & objects should be encrypted at rest.

2.Control over key permission

3.Bring your Own key.

4.Maintain most existing functionality with encryption*

5.Monitor key activities performed by your users.


Encription Schemes :


How does this encryption actually work?


Within Salesforce, they use two different types of algorithms. 


1.Probabilistic schema

2.Deterministic schema


Probabilistic schema :


This is the default encryption of Salesforce, and this is where data is fully randomized and is the most secure option.

Each bit of data is turned into a fully random ciphertext string every time it's encrypted.

Encryption generally doesn't impact users who are authorized to view the data.


The exception is when logic is executed in the database or when encryption values are compared to strings or each other. 

In these cases, because the data has been turned into a complete random, patternless string, filtering isn't possible. 

It's recommended to use probabilistic on fields that are not going to be used for filtering or comparisons such as Social Security numbers, 

phone numbers, etc.


Deterministic schema :


To be able to use filters when data is encrypted, we have to allow some type of pattern into our data. 

Deterministic encryption uses a static initialization vector so that encryption data can be matched to a particular field value. 

The system can't read that piece of data that's encrypted, but it does know how to retrieve the ciphertext that can stand for that piece of data.

The IV is unique to a given field in a given org and can be only decrypted with an org-specific encryption key. 



Classic Encryption vs Shield Encryption :


Features                 Classic Encryption       Shield Encryption


Encryption at Rest               Y                      Y

Native Solution                  Y                      Y

Masking                          Y                      -

Encrypt Standard Fields          -                      Y

Encrypt Custom Fields      Only in special field        Y

Encrypt Files                    -                      Y

Encrypt Search & Events          -                      Y


Tenant secret :


Generate tenant secret is where Salesforce generates everything for you on your behalf and manages everything inside of a protected encrypted schema.


Bring your Own Key :


Bring your own key, however, is the opposite of that where you come to the table with more information and manage that yourself and give Salesforce enough information to be able to encrypt the data with your own key.


Tenant secret Type :


1.Data in Salesforce

2.Search Index

3.Event Bus


Note :


1.Probabilistic algorithm


Probabilistic algorithm is the default encryption of Salesforce.

This is where data is fully randomized and is the most secure option. 

Each bit of data is turned into a fully randomized cipher text string every time it's encrypted. 

Encryption within Salesforce generally doesn't impact users who are authorized to view the data. 

The exceptions are when logic is executed in the database or when encryption values are compared to a string or to each other. 

In these cases because the data has been turned into a random patternless string, filtering is not possible.


2.Deterministic algorithm


To be able to use filtering when data is encrypted, we have to allow some patterns into our data. 

Deterministic encryption uses a static initialization vector, or also known as IV, so that encryption data can be matched to a particular field value. 

The system can't read a piece of data that's been encrypted, but it does know how to retrieve the cipher text that stands for that piece of data. 

The IV is unique for each given field in a given org and can only be decrypted with your org‑specific encryption key.


Within deterministic, there's two subtypes.


1.case-sensitive deterministic

2.case-insensitive deterministic


Note : This is very important when you're using deterministic that you choose this correctly. Otherwise, you will not get the results you're expecting within your filters.


Event Monitoring :


Event Monitoring within the Salesforce Platform is a granular detail view of how users and how the system is performing at an event level. 

Every time an action is placed or a record has changed, what Salesforce called as an event is created within the platform. 

Within the Salesforce Shield, you have a granular view of being able to monitor what's happening within the platform. 


1.monitoring activity

2.Increase Adoption

3.Optimize Performance


within Event Monitoring, they have something called transaction security policies.

These are the policies that allow you to monitor or take actions on certain types of data interaction with the system.


1.Condition Builder

Condition Builder actually allows you to apply these rules with no code and with the interface.

2.Apex

The second is you can apply Apex to your transaction security policies to get a fine-grained way of controlling what notifications on what field and objects these Event Monitoring events are actually occurring.


Within the transaction security policy, there are four types of notifications.

1.Block

This block allows you to block a user's interaction completely when they've done a specific thing, such as try to load a report that has more records than you've allowed.

2.multi-factor authentication

The second is require a multi-factor authentication so that a user can prove that they are actually who they say they are. 

3.Email Notification

A simple notification to your system admins or a group of individuals so you can understand what's happening in real time.

4.In-app notification

An In-app notification back to that system admin or a group of admins to make sure you understand what's happening. 


Tableau CRM for Event Monitoring :


This platform gives sales, service, and the other core applications inside of CRM the ability to have advanced analytics and gives you the ability to slice and dice and create tables and visualizations that are above and beyond the standard reporting and dashboarding tools inside of core.


Benefits of the Event monitoring App


1.Easy Access

2.Visual

3.Filter & Facet

4.Shareability


Wednesday, 6 July 2022

Internationalization properties in LWC

 To make components Internationalize you can use Internationalization Properties in Salesforce as shown below

import internationalizationPropertyName from @salesforce/i18n/internationalizationProperty;

The Property values will be returned for current user.



1.Internationalize Locale Date

ex:

import LOCALE from "@salesforce/i18n/locale";

@track date = new Date(2022, 6, 25);
@track formattedDate;
this.formattedDate = new Intl.DateTimeFormat(LOCALE).format(this.date);
  
2.Internationalize Currency
ex:
import LOCALE from "@salesforce/i18n/locale";
import CURRENCY from "@salesforce/i18n/currency";

@track number = 10050.5;
@track formattedCurrency;

 this.formattedCurrency = new Intl.NumberFormat(LOCALE, {
      style: "currency",
      currency: CURRENCY,
      currencyDisplay: "symbol"
    }).format(this.number);
    
3.Internationalize Time Zone    

ex :
import TIMEZONE from "@salesforce/i18n/timeZone";

@track timeZone;
this.timeZone = TIMEZONE;