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
Marco PinderMarco Pinder 

ODD ONE: PLEASE HELP - Cannot submit object already in process

Hi all,

 

I have written the following trigger that automatically starts an approval process for a campaign record provided that the 'Estimated_Cost_per_head__c' value is greater than or equal to 25.

 

trigger AuthSpendProcessApproval on Campaign (after insert, after update){ for (Campaign c : trigger.new){ if (c.Estimated_Cost_per_head__c >= 25){ if (c.Approved__c == FALSE){ // Create an approval request for the Campaign Approval.ProcessSubmitRequest app = new Approval.ProcessSubmitRequest(); app.setObjectId(c.Id); //Submit the approval request for the Campaign Approval.ProcessResult result = Approval.Process(app); } } } }

 My test class passes fine and under most circumstances this trigger works the way I intended, however I have found a way to break it and would welcome some feedback please.

 

When I create a new Campaign record there are certain fields that have been set as mandatory that I deliberately don't complete. I get the error message reminding me to complete them (which I do), however instead of saving the record correctly I am supplied with the following error message:

 

Validation Errors While Saving Record(s)
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger AuthSpendProcessApproval caused an unexpected exception, contact your administrator: AuthSpendProcessApproval: execution of AfterUpdate caused by: System.DmlException: Process failed. First exception on row 0; first error: ALREADY_IN_PROCESS, Cannot submit object already in process.: []: Trigger.AuthSpendProcessApproval: line 16, column 49".

 

This to me suggests that the record has already been entered into the approval process at the point of trying to save the record the first time (when it is unsuccessful due to mandatory data missing).

 

This error does not appear if I create a Campaign record from scratch with all data supplied first time around.

 

I then spoke to one of our database guys to try and understand database transactions better, but I am still confused as to what circumstance is causing the record to (in my mind) prematurely enter the approval process.

 

Can anyone shed some light on this please?

 

Thanks,

 

Marco

Andy BoettcherAndy Boettcher

Marco,

 

When you're creating the Campaign record without the mandatory fields - are you doing this via the native UI or via code?

 

-Andy

Marco PinderMarco Pinder

Hi Andy,

 

Thanks for your reply.

 

The creation of the Campaign record is done through the UI.

 

Regards,

 

Marco

Andy BoettcherAndy Boettcher

This is very strange - the SF order of execution should be kicking your initial incomplete record out and rolling the entire transaction back.

 

Profile-specific layout rules are checked before any BEFORE or AFTER triggers are fired.

 

Can you reproduce the issue logged in as a regular (non-admin) user?

 

-Andy

Marco PinderMarco Pinder

Hi Andy,

 

I just tested it with a different profile and role (to replicate one of our standard users that would use this functionality) and it happens again.

 

It baffles me as I would have thought that when the record errors (with missing mandatory data) no transactions to the database would be made.

 

Do you have any ideas?

 

Thanks,

 

Marco

Andy BoettcherAndy Boettcher

It is baffling - you're not alone on that part.  According to Salesforce's own Order of Execution - this should not be happening.

 

Going on pure conjection...this could be because you're calling essentially an API-like call.  You could check the error collection in your trigger and see if there are any errors - if so, don't run the Approval call?

 

-Andy

Marco PinderMarco Pinder

Thanks Andy, that sounds like a suitable solution.

 

Do you have any advice on how I can check for errors on the record in the trigger?

 

My coding skills are fairly limited, but I appreciate all your help so far.

Andy BoettcherAndy Boettcher

I would start here and see if this collection has the information you need:

 

http://bit.ly/v2UQRp

 

-Andy

stollmeyerastollmeyera

I am getting a very similar error, ecept my APEX was working fine for three months and then all of the sudden started throwing this error.  

 

Did you find a resolution?