+ Start a Discussion
Yousef Bayari 7Yousef Bayari 7 

Approval Process works fine when manually submitted but fails when called in a trigger

Hello , 

I have an approval process on custom object where I want 2 related user to be assigned as approvers , I defined the approval step for this and everything works fine when I manually submit a record for approval. 

But if I submit the record for approval from a trigger I am getting the error

MANAGER_NOT_DEFINED, Manager undefined

I did not define the ( Next Automated Approver Determined By) since I dont want the manager to approve this , I want only the 2 related users defined in the approval step to be assigned. 

Any IDeas? 


 User-added image
That looks like a validation rule is returning "Manager undefined" because the field is null. Are you passing a value to that field when inserting a record via your trigger?
Yousef Bayari 7Yousef Bayari 7
Hi Chris, 

 There is no such field on my custom object , this field is on the approval process " Next Automated Approver Determined By " , if I set it to manager the submission for approval will work from a trigger. if I leave it empty then I get the error above when submitting from a trigger , but it works fine if I manually submit the record for approval. 

In my scenariio , I dont want the manager to approve ! I want the approvers to be assigned fron the first approval step. 


Yousef Bayari 7Yousef Bayari 7
I figured out what causing the issue ! still need to see how to resolve it 


On a given approval step, you might have chosen the "related user" who would approve this record. This would be a lookup field on the object that points to a user. Let's say the field is called "X". When the record enters this approval step and X is empty, you will get MANAGER_NOT_DEFINED error.

The solution is to make sure "X" is always set when you enter this step.

So based on the code in the post you shared…. Maybe something like the below would work? Included some links below as well. You'd have to come up with the code to populate m.SomeID

trigger modSubmitter on Modification__c (after Insert) {

Approval.ProcessSubmitRequest[] reqs = new Approval.ProcessSubmitRequest[]{};
for(Modification__c m : Trigger.New){
                                Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
                                req1.setNextApproverIds(new Id[] {m.SomeID});


if(reqs.size()>0) Approval.ProcessResult[] result = Approval.process(reqs); }