Sunday, August 14, 2022

Bypass Salesforce Trigger Methods Conditionally

We know that we cannot update the code in Production. Sometimes we are stuck in a situation where we need to stop the triggers. For that we need to deploy the trigger again from pre-prod environment to Prod Environment with the required modification.

To overcome this long process, we can use switch on/switch off framework.

Lets us see how.

  1. Create a list custom settings, lets say Trigger_Switch__c
  2. Create a Boolean field called Active__c
  3. Create a record in the custom setting with Name as per your choice(I will keep it Method1) and make Active__c as True and Save it.
  4. Now open a trigger and write below code.

trigger AccountTrigger on Account (<events>) {
    Map<String,Trigger_Switch__c> mapTriggerSwitch = Trigger_Switch__c.getAll();
    if(mapTriggerSwitch .containsKey('Method1')
        && mapTriggerSwitch .get('Method1').Core_Active__c){
        accountCtrl.Method1(trigger.new);
    }
}

This code will check whether the custom setting record is active or not. If it is active then the method will be called else not.


Please share the blog and leave a comment about your thoughts.
Click here for more blogs.

Saturday, August 13, 2022

Bypass validation rule for different users in Salesforce

 In some scenarios, we need to bypass the validation rules.

For instance, the requirement is to insert the new records in an object using data loader and the user using the data loader should not get the validation rule error.

Lets see how we can do it.

  1. Create a hierarchy custom setting, lets say the name is Validation_Bypass__c (We are creating hierarchy here because it will help us to bypass the validation based on our user or profile.)
  2. Create a boolean field in the custom setting, lets say Bypass__c
  3. Create a record in that custom setting. Make the Bypass__c checkbox = true and in the location give your name and save it.
  4. Click on Save.
  5. Create or open an existing validation rule.
  6. Modify the validation validation like this: IF($Setup.Validation_Bypass__c.Bypass__c=TRUE,FALSE,Add your existing logic here)


Please share the blog and leave a comment about your thoughts.
Click here for more blogs.


Friday, August 12, 2022

Types of Apex Errors in Salesforce

 Get Familiar with Apex Errors!

Humans are bound to make some mistakes. We all face some errors while we code. It is perfectly fine if errors are encountered.

The main thing which we need to focus here is why do we get such exceptions and how can we resolve them.

  1. List has no rows for assignment to SObject: When query doesn't return any record. Check your query and put appropriate filters.