Ask Search:
Surya NSurya N 

I ahve to unlock the record which was locked by Approval process in initial submission. i tried below

trigger Approve_Project_In_Take_Request on Project_In_Take_Request__c(after insert,after update) {
Set<ID> ids = new Set<ID>();
        ids=  trigger.newMap.keySet();
        List<Id> lStrings = new List<ID>(ids);
        system.debug('lStrings lStrings '+lStrings );
           
    try
        {
            for (Integer i = 0; i < Trigger.new.size(); i++) 
            {
                 if (Trigger.new[i].Status__c=='Submit')
                    {
                        // create the new approval request to submit
                        Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
                        req.setComments('Submitted for approval. Please approve.');
                        req.setObjectId(Trigger.new[i].Id);
                        // submit the approval request for processing
                        Approval.ProcessResult result = Approval.process(req); 
                       Approval.UnLockResult[] lrList = Approval.unlock(lStrings , true); 
                    }
            }
        }
    catch(System.DMLException e) 
    { 
      
    }
            
        
    
}
By using the above code im agetting Error below in above highlighted line.Error:Apex trigger Approve_Project_In_Take_Request caused an unexpected exception, contact your administrator: Approve_Project_In_Take_Request: execution of AfterUpdate caused by: System.UnexpectedException: null: Trigger.Approve_Project_In_Take_Request: line 22, column 1
Best Answer chosen by Surya N
Surya NSurya N
Hi Priyanka and Amnon Thank you for your responses, Changing API Version to 44 this solved my isssue....

All Answers

Amnon KruviAmnon Kruvi
It looks like right now you're unlocking ALL modified records regardless of their status field. You need to populate your lStrings list only when the stage changed to Submit, and if the list is not empty, run the unlock call once after the loop. 
Surya NSurya N
Hi Priyanka and Amnon Thank you for your responses, Changing API Version to 44 this solved my isssue....
This was selected as the best answer