I am new to Apex and just starting to understand using maps to avoid loops within loops and hitting limits. My trigger below updates opp line items with the internal cost from another object using a map and the one value - "Service_Name".
My issue is I need to update another field on the Opp line item with "Agent Fee" in the same way - but there are 4 criteria to get the right Agent Fee from another object. I cant see how do so this other than have a loop of agent Fees object with the opp line loop? How else can I do this? Is it possible with Maps ?
trigger sectortest on Opportunity (after update) {
for (Opportunity eachopportunity : Trigger.new){
//Get costs for the Opps FY and put in map
List<Internal_Costs__c> costs = [SELECT Id, Financial_Year__c, Service_Name__c, Standard_Cost__c FROM Internal_Costs__c WHERE Financial_Year__c =: eachopportunity.Financial_Year__c];
Map<string, Decimal> costmap = new Map<string, Decimal>();
for (Internal_Costs__c eachcost : costs){
costmap.put(eachcost.Service_Name__c, eachcost.Standard_Cost__c);
}
//Get Line Items for Opp
List<OpportunityLineItem> opplines = new List<OpportunityLineItem>();
opplines = [SELECT Id, DS_Status__c, Country__c, Product2Id, UnitPrice FROM OpportunityLineItem WHERE OpportunityId =: EachOpportunity.Id];
List<OpportunityLineItem> opplinesupdate = new List<OpportunityLineItem>();
//assign cost to line items
for (OpportunityLineItem EachOppline : opplines) {
decimal cost = costmap.get(EachOppline.Service_Name__c);
EachOppLine.Internal_Cost__c = cost;
opplinesupdate.add(EachOppline);
}
update opplinesupdate;
}
}
Agent Fee
exist? Is it on the sameInternal_Costs__c
object?