Wednesday, May 18, 2011

Salesforce - To get the information about an object record which is sent for the approval


Solution – We can make use of ProcessInstance standard object which can be queried to get the end to end approval process of the record. The related child objects of the ProcessInstance can be used to get more information,
ProcessInstanceStep (child relationship name - "Steps") - Represents one step in an approval process (ProcessInstance).
ProcessInstanceWorkitem (child relationship name - "Workitems") - Represents a user's pending approval request.
ProcessInstanceHistory (child relationship name - "StepsAndWorkitems") - This read-only object shows all steps and pending approval requests associated with an approval process (ProcessInstance). You cannot query on this read only object instead you can query ProcessInstanceHistory by including it in a nested query on the parent ProcessInstance object.
Sample Code: The following code retrieves those submitted case records which needs the logged in user approval

 // Query the list of cases where in the Branch Manager is the logged in user
List<Case> userCaseList = [select Id, casenumber, company_name__c, Type,OwnerId, Status  from Case where Branch_Manager__c = :loggedUser];

// Query those submitted cases which needs the logged in user approval 
List<ProcessInstance> caseApprovals =[SELECT Id,TargetObjectId,
   (SELECT Id, ActorId, StepStatus, Comments, IsPending FROM StepsAndWorkitems where IsPending = True)
FROM ProcessInstance  where TargetObjectId in :userCaseList];

       

No comments:

Post a Comment