I am using platform events to send an event in from an external system that triggers a lead to be converted via apex. The incoming platform event sends data that is needed to convert the lead and also create multiple custom child records associated with an opportunity during the lead conversion. I have a custom object hanging off the opportunity object (1-to-many).

As part of the lead conversion logic, I first check if the lead has been converted already by running a SOQL statement. Since two incoming platform events are coming in within milliseconds of each other, the SOQL query returns empty result when I check to see if the lead has been converted already.

Is there a way to somehow get more synchronous behavior versus asynchronous with platform events and callouts?

  • You cannot achieve a synchronous behavior with what you have presently. Platform Events are supposed to be asynchronous in nature. If you try to make it more synchronized, you will still have the same issue as the difference between two events will be fairly less.
    – Jayant Das
    Apr 11, 2019 at 0:47

1 Answer 1


Use "FOR UPDATE" to force the platform to wait for pending database commits to the record. This should give you more "synchronous" behavior at the cost of additional transaction time (usually just a few milliseconds). See Locking Statements for more information.

  • While this may resolve the problem of verifying the records, but technically this is not synchronous in nature. The root cause of the issue here seems to be near synchronous behavior instead of asynchronous.
    – Jayant Das
    Apr 11, 2019 at 1:13
  • @JayantDas If you have a better solution, feel free to add an answer. As far as I'm aware, row-locking is the only real solution to this sort of problem. Locking makes the transaction synchronous with respect to prior transactions on the same record.
    – sfdcfox
    Apr 11, 2019 at 1:20
  • I think your solution seems to work here. What I am referring is the usage of term synchronous in this context. By definition, typically if you get a response right away is what I consider as synchronous. The more synchronous the operations are, the more contention they will get into, that's where a Locking will be required to avoid that deadlock. Do you see what I am referring to?
    – Jayant Das
    Apr 11, 2019 at 1:39
  • I get a row lock error using "FOR UPDATE" System.DmlException: ConvertLead failed. First exception on row 0; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record This error is generated by this code: Database.LeadConvertResult lcr = Database.convertLead(lc); Apr 12, 2019 at 20:45
  • @user1669296 As long as you've queried that record first, you should be okay. It sounds like you have a trigger or other process trying to lock a related record?
    – sfdcfox
    Apr 12, 2019 at 20:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .