Pages

Saturday 3 October 2020

Safe Navigation Operator | Winter '21 Release

 In Winter '21 Salesforce Release, Salesforce has introduced a new operator called "Safe Navigation Operator".  This operator will help us to avoid NullPointer Exceptions and reduce lines of code and make the code more cleaner. 

This is not something new, this already exist in many other programming languages (also known as optional chaining operator, safe call operator, null-conditional operator).

the safe navigation operator is a binary operator that returns null if its first argument is null; otherwise it performs a dereferencing operation as specified by the second argument.

The main advantage of using this operator is this avoids multiple if s and else conditions.

If the left-hand-side of the chain expression evaluates to null, the right-hand-side is not evaluated. Use the safe navigation operator (?.) in method, variable, and property chaining. The part of the expression that is not evaluated can include variable references, method references, or array expressions.

Example

  • This example first evaluates a, and returns null if a is null. Otherwise, the return value is a.b.
    a?.b // Evaluates to: a == null? Null : a.b
  • This example returns null if a[x] evaluates to null. If a[x] does not evaluate to null and aMethod() returns null, then this expression throws a null pointer exception.
    a[x]?.aMethod().aField // Evaluates to null if a[x] == null
  • This example returns null if a[x].aMethod() evaluates to null.
    a[x].aMethod()?.aField 
  • This example indicates that the type of the expression is the same, whether the safe navigation operator is used in the expression or not.
    Integer x = anObject?.anIntegerField; // The expression is of type Integer because the field is of type Integer
  • This example shows a single statement replacing a block of code that checks for nulls.
    // Previous code checking for nulls
    String profileUrl = null;
    if (user.getProfileUrl() != null) {
    	profileUrl = user.getProfileUrl().toExternalForm();
    	}
    // New code using the safe navigation operator
    String profileUrl = user.getProfileUrl()?.toExternalForm();
  • This example shows a single-row SOQL query using the safe navigation operator.
    // Previous code checking for nulls
    results = [SELECT Name FROM Account WHERE Id = :accId];
    if (results.size() == 0) { // Account was deleted
        return null;
    }
    return results[0].Name;
    // New code using the safe navigation operator
    return [SELECT Name FROM Account WHERE Id = :accId]?.Name;

 See Also

https://developer.salesforce.com/docs/atlas.en-us.228.0.apexcode.meta/apexcode/langCon_apex_SafeNavigationOperator.htm 

Saturday 14 September 2019

Recycle Bin in Lightning Experience

In Winter '20 Salesforce release, Salesforce has introduced the accessibility of Recycle Bin in Lightning Experience. No longer have to switch to Salesforce Classic to access the Recycle Bin. We can now view, restore, and permanently delete the items in the personal or Org Recycle Bin.

How to access:
Access the Recycle Bin by selecting it in the App Launcher under All Items or personalizing your navigation bar. Or add the Recycle Bin tab for your org in the Lightning App Builder. 

The Recycle Bin in Lightning Experience works the same as it does in Salesforce Classic, except for a few differences. In Lightning Experience:

The org’s Recycle Bin is called Org Recycle Bin (1). The Salesforce Classic name is All Recycle Bin. 

Restore records by clicking Restore (2) instead of Undelete in Salesforce Classic. 

Permanently delete records by selecting them and clicking Delete (3) versus Undelete or Empty in Salesforce Classic. 

You can’t restore reports and dashboards. Switch to Salesforce Classic to access them in the Recycle Bin.














Tuesday 10 September 2019

Subscribe to Lightning Reports and Dashboards by Public Groups or Roles

In Spring '19 Salesforce extended the Report & Dashboard subscription beyond the user. Now, subscriptions can be done by Public Groups or Roles as well. This feature will help your recipient list stays current even when group membership or roles change.

A group subscription keeps your subscriber list current even when people join or leave the group. A role-based subscription keeps your subscriber list current even when people change roles.

In order to take the use of this feature, you must have the following user permissions enabled for your user:
  • To subscribe others to a dashboard, you need the Subscribe to Dashboards: Send to Groups and Roles permission
  • To subscribe others to a report, you need the Subscribe to Reports: Send to Groups and Roles user permission
  • To specify who runs the report in a report subscription, you need the Subscribe to Reports: Set Running User user permission.
How to verify:
When you’re setting up a report or dashboard subscription, click Edit Recipients.

  1. For the Entity Type, select the recipient type. 
  2. Start typing to see matching entries of the selected entity type and make a selection. 
  3. Click Add to add your selection to the list of subscribers

















Note: Only those User, Roles & Groups will be available for subscription who has the folder sharing. So, before the subscription make sure folder sharing is correct.

Monday 2 September 2019

Control the Lightning Component visibility using Permission Set / Custom Permission

At Dreamforce'18, Salesforce had announced lightning pages. This adds even more custom pages by hiding and displaying fields/components based on the criteria. It means that you could potentially have a one-page layout with all the differences being controlled by component visibility criteria. No more tons of different page layouts by profile!

With Lightning Component Visibility, we can decide if the component will display on the page for the user based on some set of criteria. A common practice is based on the user’s profile or username.

The example we have here is pretty simple.

The custom lightning component should only to be visible to a particular set of users within the profile.


1. Create new Custom Permission if it does not exist. Setup -> Custom Permission -> New.




 2. Go to the Lightning record page, set component visibility -> Filters. Choose Permissions -> Custom Permission and search for custom permission which was created in step 1. 












3. Create a new permission set and assign the custom permission to it.
4. Final Step, Assign the Permission set to the users.

Result:






Monday 19 August 2019

Validation rule to prevent the deletion of Record

Salesforce does not give any function to check for delete events in Validation rule e.g. ISDELETE() which could be similar to ISNEW().

There are many ways to achieve this, I know you would be thinking to write a trigger on before delete event and throw the error message. Yes, your thinking is correct, However, this can also be achieved without writing code. #powerofadmin

Let's consider a scenario. 
Opportunity line items cannot be deleted if the opportunity stage is closed-won.

1. Create new Roll-up summary field on Opportunity Object. This field will be used to count the total number of Opportunity Products on the Opportunity.


2. Create Validation rule on the Opportunity object. This rule will prevent the record delete if the Opportunity Stage is "Closed Won". 

Result:

Note: The above solution works for Master-Detail relationship between two objects.