You need to sign in to do that
Don't have an account?
Apex Trigger to update OpportunityLineItem after Opportunity has been updated.
Good Morning,
I am fairly new to apex and have been trying to create a Trigger that updates all OpportunityLineItem Quantity field to a specific value from a named Expected_Number_Of_Students__c on the related Opportunity.
I have got the following compile error:
Error: Initial term of field expression must be a concrete SObject: MAP<String,Decimal> at line 23 column 32
The code is as follows.
trigger opportunityLineItemsQuantity on Opportunity (after update) {
//Get list of record types from Opportunity
list<RecordType> rTypes = [Select Name, Id from RecordType Where SObjectType= 'Opportunity'];
//Create a Map for the Opportunity record types
Map<String,Id> OppRecordTypes = new Map<String, Id>{};
for(RecordType rt: rtypes)
OppRecordTypes.put(rt.Name,rt.Id);
//Create a map for Opportunity records
Map<String, Decimal> oppList = new Map<String, Decimal>{};
//Iterate through the Opportunities
for(Opportunity opp : Trigger.New)
if(opp.RecordTypeId == OppRecordTypes.get('ELT Digital Products Opportunity')){
if(Opp.Expected_Number_of_Students__c != Null){
oppList.put(opp.Id,Opp.Expected_Number_Of_Students__c);
}
}
//Get Opportunity Line Items that are associated to the Opportunity
for(OpportunityLineItem oli : [Select OpportunityId, Quantity From OpportunityLineItem Where OpportunityId In : oppList.keyset()]){
//This is where the code is failing. This is where I want the OpportunityLineItem Quantity to be the same as the Opportunity Expected_Number_Of_Sudents__c value.
oli.Quantity = oppList.get(oppList.Expected_Number_Of_Students__c);
}
}
I am not sure how to resolve this on line 23 I would be very grateful for some guidance on this.
Thanks
Hi
I have modified your code. Please try below code. If it would help you or not.
Let me know.
Sagarika Rout
SFDC Developer
All Answers
Try replacing the offending line with this:
oli.Quantity = oppList.get(oli.OpportunityId);
If this helps, please mark it as a solution, and give kudos (click on the star) if you think I deserve them. Thanks!
-Glyn Anderson
Certified Salesforce Developer | Certified Salesforce Administrator
Many thanks for your comment.
In line 23 I am trying to assign the OpportunityLineItem Quantity to be the same value as the Opportunity Expected_Number_Of_Students__c.
I tried your suggestion but nothing happened when I updated the Opportunity Expected_Number_Of_Students__c and the OpportunityLineItem Quantity vales stayed the same.
What I did notice that when the OpportunityLineItem was saved the value updated to that of the Expected_Number_Of_Students__c.
What I am trying to acheieve is when the Opportunity is saved the OpportunityLineItems update automatically rather than updating the OpportunityLineItems one by one.
Hi
I have modified your code. Please try below code. If it would help you or not.
Let me know.
Sagarika Rout
SFDC Developer
Its my pleasure that , this could help you....!!!!. Thanks for making it as a solution.
should it work too?