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
Swamy P R NSwamy P R N 

Approval process error by apex trigger

Hello everyone,

I'm executing the approval process by apex trigger on my customobject. While inserting record i'm facing the below error:
"Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, getApproverValueFromApproversGroup: execution of BeforeInsert caused by: System.DmlException: Process failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, missing required field: [objectId]: [objectId] Trigger.getApproverValueFromApproversGroup: line 31, column 1: []"

trigger logic:
Set<Id> ordrprdctIds = new Set<Id>();
    for(RMA_Request__c r:trigger.new){
        Id prdctid=ordrPrdctMap.get(r.Order_Product__c);
        if(aprvrGrpMap.get(prdctid)!=null)
           r.Approver__c= aprvrGrpMap.get(prdctid);
        // Create an approval request for the RMA
        if(r.Status__c=='In-Progress' && r.Approver__c!=null){
            Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
            req1.setComments('Submitting request for RMA Approval ');
            req1.setObjectId(r.id);
            req1.setNextApproverIds(new Id[] {r.Approver__c});
           System.Debug('Approver value@@'+r.Approver__c);
            // Submit the approval request for the RMA
            Approval.ProcessResult result = Approval.process(req1);---> Error line
        }    
    }

Please assist me with your valuable solution.
Thanks in advance!!
Best Answer chosen by Swamy P R N
Shaijan ThomasShaijan Thomas
Before insert will not get the id of the record; Hope you know this

All Answers

PratikPratik (Salesforce Developers) 
Hi Swamy,

Here is the sample code for the trigger on approval process which will help you:

http://blog.jeffdouglas.com/2010/01/04/automating-salesforce-approval-processes-with-apex-triggers/

Thanks,
Pratik
Shaijan ThomasShaijan Thomas
what kind of trigger it is, before or after?
Thanks
Shaijan
Swamy P R NSwamy P R N
@Shaijan,
It is for before trigger.

@Pratik,
My trigger is also exactly like that only but this error is coming on record creation, not on the updation.
Shaijan ThomasShaijan Thomas
Swamy : did you print the r.id, if not please try; I hope its not before insert
Shaijan ThomasShaijan Thomas
Before insert will not get the id of the record; Hope you know this
This was selected as the best answer
Swamy P R NSwamy P R N
Hi Shaijan,

There are many persons in this world, that they will do all the things which are not possible also but they usually forget to correct small small mistakes.

That i did today, there is no object ID on before triggers. Thank you for ur idea and time :) :) .