Thursday, March 28, 2019

Salesforce Lightning Platform Security and Sharing

Motivation behind this


Many a times, I thought of having a single glance of Security and Sharing architecture, but over the net I couldn't find it as a simple single flow. I am trying to portray this flow on best of my knowledge.

In the below flow, I am trying to showcase what the different layers of Platform Security and when a user tries on login to Salesforce what are the various steps come to play. 

After successful login, if that user tries to access the record then how Object level, field level security comes into picture.

Obviously, in a private sharing model, data or record sharing is always in demand and how that can be achievable.

Finally, to showcase transport layer security, if the user wants to send data from Salesforce to external system how different steps occur is good to know.

The following picture might help those aspirants who are preparing for Admin, Sales Cloud and Integration Architecture certifications.


Further Reference





Saturday, March 16, 2019

Sending daily reminders/email alerts before Event Start Date

Use Case


Business has a requirement to send to daily reminders/email alerts from 20 days prior to Event Start Date until event is not complete.


Solution


I was answering this question (How to send daily reminders/email alerts before event start date) and thought that it might be beneficial for other too. This kind of use case it also applicable to send birthday wishes to Contacts every year.

Here is the way to solve this use case.

Create a workflow rule based on `Event Start Date > Reminder Date` with other criteria like status is not closed.

Create a time-dependent workflow like `1 hour after Reminder Date`. Here you will send an email and most importantly, update `Reminder Date` field to next date.

System will compares the dates and every day until Event Start Date it will send reminders.

Flow will look like this:




Tuesday, March 12, 2019

Efficient way of dynamically casting SObject to Specific Object to update records

Use Case


Developer has a requirement to typecast specific object type from the name of SObject and try to update record of the SObject.

For example, SObject name is "Account" and Id is ''0012v00002AyZWE''.

Developer wants to update Account Name or other field values.



Solution


Create an instance of SObject based on reflection as follows:

  1. String sObjectName = 'Account';  
  2.   
  3. SObject actObj = (SObject)(Type.forName('Schema.'+ sObjectName).newInstance());  


We can also create an instance of SObject based on global describe as follows:

  1. String sObjectName = 'Account';  
  2.   
  3. SObject sObj = Schema.getGlobalDescribe().get(sObjectName).newSObject(); 

But, first one is most efficient which executes in 1ms where as second one takes 75ms and may be higher based on number of objects available at our Salesforce org.

So, lets put the desired solution:

  1. String sObjectName = 'Account';  
  2. String recordId = '0012v00002AyZWE';  
  3.   
  4. SObject actObj = (SObject)(Type.forName('Schema.'+ sObjectName).newInstance());  
  5. actObj.Id = recordId;  
  6. actObj.put('Name''New Account2');  
  7. update actObj; 

 We can easily put that in the list of SObject and update the records in one single goal.

If we have Id of a record then we can create an instance of SObject as follows:

  1. Id sampleid = '0012v00002AyZWE';  
  2. SObject actObj = sampleid.getSObjectType().newSObject();  


So, the entire solution approach will be as follows:

  1. Id sampleid = '0012v00002AyZWE';  
  2. SObject actObj = sampleid.getSObjectType().newSObject();  
  3. actObj.Id = recordId;  
  4. actObj.put('Name''New Account3');  
  5. update actObj; 

Conclusion


From the above approaches which one needs to be followed that truly depends on individual use cases. It's good that Apex provides different ways to typecast SObject to specific object.