Monday, March 6, 2023

How to cover code for catch block in Apex

Sometime developer needs to cover the catch part in the Apex Class.

Code Snippet:

try{
    Account acc = new Account();    
    acc.Name = 'Test';    
    Database.insert (tempList,false);
}
catch(exception e){
    LogObj obj = new LogObj();
    obj.error = e.getMessage();
    insert obj;
}

To cover the catch, we can modify the code as shown:

try{
    boolean allOrNone = false;
    Account acc = new Account();    
    acc.Name = 'Test';    
    Database.insert (tempList, allOrNone );
    if(Test.isRunningTest()){
        Database.insert (tempList, 0);
    }
}
catch(exception e){
    LogObj obj = new LogObj();
    obj.error = e.getMessage();
    insert obj;
}

In this case, code will run the Database.insert (tempList, 0); line and will eventually fail and code will go to catch block.

Please share the blog and leave a comment about your thoughts.

Click here for more blogs

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.

Friday, December 24, 2021

Update Custom Setting in Apex Salesforce

Many times we came across scenarios where we sometime need to insert/update/delete the custom setting in our apex logic. The below code will help you in that.

Syntax : 

<Custom Setting Name>   cs = new <Custom Setting Name>();
cs.<Custom Setting Field Name> = true;
insert cs;

Example 
Custom setting name is Test__c and one of the field name in custom setting is Active__c.

Test__c cs = new Test__c();
cs.Active__c = true;
insert cs;

Hierarchy Custom Settings
Now if you want to Insert a record in Hierarchy Custom Settings then,
Test__c cs = new Test__c();
cs.Active__c = true;
cs.SetupOwnerId = UserInfo.getUserId();
insert cs;

cs.SetupOwnerId-- You can add the user id or profile id.

Update Org Level Custom Settings

Test__c cs = Test__c.getOrgDefaults();
cs.Active__c = true;
update cs;

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










Mostly Viewed