I'm trying to pass a collection variable of opportunities from a flow to apex. Here is my apex and a picture of my action element. Why can't I pass my collection of opportunities (socOpportunity in my flow) to apex via the Action element? FYI, the apex code is used in case hundreds of opportunity records are imported at once. The code is very basic; it's matching products to opportunity records based on the vehicle preference on the opportunity record.
public class OpportunityProductMatching {
@InvocableMethod(label='Match Opportunities to Products' category='Opportunity')
public static List<List<Opportunity>> matchOpportunities (List<Opportunity> listOpp){
List<List<Opportunity>> listFlowOpps = new List<List<Opportunity>>();
listFlowOpps.add(listOpp);
List<OpportunityLineItem> listOppLineItem = new List<OpportunityLineItem>();
List<Product2> listProduct = new List<Product2>(
[SELECT Id, Name, Manufacture__c, Model__c, Year__c, Price__c, Miles__c
FROM Product2
WHERE IsActive = true]
);
for(Opportunity opp : listOpp) {
for(Product2 prod : listProduct) {
if (opp.Vehicle_Preference__c == prod.Model__c) {
OpportunityLineItem oppLineItem = new OpportunityLineItem();
oppLineItem.OpportunityId = opp.Id;
oppLineItem.Product2Id = prod.Id;
listOppLineItem.add(oppLineItem);
}
}
}
insert listOppLineItem;
return listFlowOpps;
}
}
Thanks for your help in advance!
Best Answer
Flows are bulkified, which is why the apex method accepts a list of records. You would only pass in 1 record in the flow, but the apex method accepts a list.
To get around this, your apex method can accept a
List<List<sObject>>
so that you can pass a record collection variable from your flow into the apex method.