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
JoeSpecJoeSpec 

synchronous apex code execution

I have an instance where it looks like all the workflows/process flows associated with an update command in apex are sometimes not completed before a later insert command.
The system has a validation rule where we can only have a single 'Active' ag, which uses a unique field that is set by a workflow.
There's also a process builder process that does some updating on the ag based on record type.

here's the code I believe is causeing the error:

             ag = [select Id, Funding__c,Contract__c,Revision_Narrative__c, RecordTypeId  from Allocation_Group__c where id = :ag.id];
             if(ag.recordtypeId == rtIdActive )  {  // we need to set the old AG to inactive before proceeding, otherwise duplicate active ag error
                ag.recordtypeId = rtIdInactive ;
                update ag;
                ag.recordtypeId = rtIdActive;
             }    
             ag.Revision_Narrative__c=New_Revision_Narrative;
             ag.Revision_History__c=New_Revision_History;
             newAG = ag.clone(false);
             insert newAG;

The problem is I'm sometimes, but not always getting an error from the process builder process that says there are duplicate active AG's.
So I'm wondering how syncronous is the apex.  Does the first update command wait until all workflows/process builders etc have completed before moving on to the next line of code?   I've seen the order of execution information on a update or what happens during the commit, but I'd like to know if apex waits for all that to completely finish before moving on to the next step.

Thanks,

Joe

Dev_AryaDev_Arya
Hi Joe,

If your calls (all of them: flows, apex, etc...) are running in synchornous mode than yes, no matter what, they will be executed sequentially with respect to the order of execution. And the user has to wait untill all the processes are executed. Synchronous means 'I want it right now' :).

Cheers,
Dev