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
vivek raj 47vivek raj 47 

How to avoid duplicate record created by Apex batch job that runs from different user account at the same time

Hi Everyone,

I am facing duplicate record issues that is created by APEX batch job. When this batch has been invoked by salesforce button (Javascript) at the same time by different user account. 
  1. Both user account (A1 and A2) have duplicate record created A1R1, A1R2 by A1 & A2R1, A2R2 by A2. 
  2. This scenario only occured if the button pressed at the same by by both the user account. 
  3. If the button is not clicked at the same time , it works perfectly fine. 
Please suggest if any solution for this. 

Thanks,
Vivek
ShirishaShirisha (Salesforce Developers) 
Hi  Vivek,

Greetings!

Have you tried by creating the duplicate rule on the Object based on the field.So,whenever the user tries to click on the button then it will check for the duplicate records if it exists then it will throw the error.

Kindly let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.

Warm Regards,
Shirisha Pathuri