function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jake AllenJake Allen 

Having troubles with process automation on bulk lead upload

I'm not sure exactly where to put this, but to make a long story short, we have several apex triggers and process automations in place that will automatically convert new leads to contacts when the leads are uploaded from a csv. We're running into a number of issues that happen for a portion of the leads being uploaded. The number of leads affected seems to be relative to the number of leads being uploaded. Specific Isuses:
  • Apex trigger that populates a custom contact field may or may not update the contact field. No exception is generated.
  • Apex trigger that maps contact account structure into an organizational structure within our internal software will often create duplicate organizations rather than adding to existing. No exception is generated.
  • Process Flow that adds contact to email campaigns may or may not succeed. When it fails, it give an exception of "UNABLE_TO_LOCK_ROW: unable to obtain exclusive access to this record or 1 records"
I believe what's ultimately happening here is the entire process is stepping on itself, as indicated by the UNABLE_TO_LOCK_ROW error. The entire conversion process is a bit complex and involved, and I'm not sure what details may be helpful here (feel free to ask for specifics), but to outline the process:
  • A user goes to Leads and clicks "Import", then uploads a csv with the required fields for each lead.
  • If a lead is missing a timezone, a process builder grabs it from their associated account and populates the lead (this is required for a later step)
  • An Apex trigger hands off the new leads in batches of 10 to be converted by a separate class.
  • An Apex trigger calls our internal api to create an internal user account, then populates their Salesforce record with their new username
  • A Process Builder adds the contact with the new username into a Salesforce email campaign
What I'd ultimately like here is to get our system working more reliably. Currently we have to put in a good deal of manual intervention whenever a list of leads is uploaded, and I'd like to make this as hands-off as possible. Please let me know if you any suggestions on how to improve the reliability of our workflow or if you need any additional information.
AbhishekAbhishek (Salesforce Developers) 
Hi,

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

ROW_LOCK issue is very common if you have multiple users updating the record at the same time. Or say a batch job is running and is updating a record and same record another trigger or code snippet (usually a future method) is updating.

Account [] accts = [SELECT Id FROM Account LIMIT 2 FOR UPDATE];
Using FOR UPDATE keyword helps to achieve a lock a client end to prevent this locking issues.

Here is a small article on the sharing model to avoid this granular locking

https://help.salesforce.com/apex/HTViewSolution?urlname=How-can-I-avoid-getting-lock-errors-in-my-organization-1327109108393&language=en_US

http://blogs.developerforce.com/engineering/2013/01/reducing-lock-contention-by-avoiding-account-data-skews.html

Also, read on data skews which may be another cause.

I hope you find the above information is helpful. If it does, please mark as Best Answer to help others too.

Thanks.