0

I am hoping to build an LWC for displaying pending Approvals related to Opportunities, as well as a number of field values from the Opportunities themselves. Since the ProcessInstance and child StepsAndWorkItems only referencesa given Opportunity through TargetObjectId, the Opportunity fields are inaccessible in the same query, so I'm assuming I need to separate them.

In my attempts, I've struggled to keep the data related in a helpful way and get it accessible in the JS of the LWC. A basic example of what I'm trying to accomplish is below.

Any suggestions would be much appreciated!

    @AuraEnabled(Cacheable=true)
public static ProcessInstanceHistory[] getAllOppApprovals() {
    ProcessInstance[] piList = [SELECT Id, ProcessDefinitionId, Status, SubmittedById, (SELECT Id, ActorId, Actor.FirstName, StepStatus, CreatedDate, TargetObjectId, TargetObject.Name FROM StepsAndWorkItems ORDER BY CreatedDate DESC) FROM ProcessInstance];

    List<Id> oppIds = new List<Id>();
    for (ProcessInstance pi : piList) {
        for (ProcessInstanceHistory pih : pi.StepsAndWorkItems) {
            pihList.add(pih);
            oppIds.add(pih.TargetObjectId);
        }
    }
    Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>([SELECT Id, Name, StageName FROM Opportunity WHERE Id IN :oppIds]);
    // relate Approvals to their target Opportunities in an iterable that can be returned to LWC
    return iterable;
}
  • In V46, SOQL support for polymorphic fields (e.g. TargetObject) became GA. This might help; Otherwise, return a custom type that has two variables - the processInstance and the corresponding opportunity – cropredy Sep 24 '19 at 1:23
  • Thank you for providing the link to the TypeOf documentation. Helpful, and something I should have been aware of. I solved my problem with use of a wrapper class inspired by: developer.salesforce.com/forums/?id=9062I000000g9QbQAI – Nate Gelle Sep 25 '19 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.