Monday, March 6, 2023

Resolve Error 'Unable to lock row' in Salesforce

How to Resolve Unable to lock row error in Salesforce

When a record is being updated or created, Salesforce places a lock on that record to prevent another operation from updating the record at the same time and causing inconsistencies on the data.

Record locking errors is a common source of headache for people coding Data Migrations or Integrations with Salesforce. The good news is that most of the time It's our own integration code that is causing self-contention as opposed to some other user locking the record on us (because we are plowing too many conflicting updates at once, usually in an effort to improve performance). It can also be caused by Salesforce code that is triggered by our updates that require the locks, and then, when not getting them, ultimately fail. Then, that error bubbles up and our updates fail.

Lets walk through an example:

  1. User A imports a task via the data loader and assigns it to an existing account record. When the task is inserted, the apex trigger is fired.
  2. Just 2 seconds after User A starts the insert via the data loader, User B is manually editing the same account record the task is related to.
  3. When User B clicks Save, internally we attempt to place a lock on the account, but the account is already locked so we cannot place another lock. The account had already been locked by the creation of the Task.
The 2nd transaction then waits for the lock to be removed. Because the task takes about 14 seconds to be created, the lock is held for 14 seconds. The second transaction (from User B) times out, as it can only wait for a maximum of 10 seconds.

To prevent this, you can do either of the following:

  • Reduce the batch size
  • Process the records in Serial mode instead of parallel, that way one batch is processed at a time.
  • Sort main records based on their parent record, to avoid having different child records (with the same parent) in different batches when using parallel mode. 
  • Check for overlapping schedules, 2 schedules updating the same object should not start at the same time if possible.

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

Click here for more blogs

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.

Mostly Viewed