function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ChrisbrisbaneChrisbrisbane 

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

Best Answer chosen by Admin (Salesforce Developers) 
Sagarika RoutSagarika Rout

Hi

 

I have modified your code. Please try below code. If it would help you or not.

Let me know.

 

 

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>{};
	Map<ID, Opportunity> oppList = new Map<ID, Opportunity>();
    
    //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);
	 oppList.put(opp.Id,Opp);
    }
    }
     list<OpportunityLineItem> listOfOpportunityLineItems = new list<OpportunityLineItem>();
    //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);
    Opportunity OppObj = oppList.get(oli.OpportunityId);
	   if(OppObj != NUll){
	       oli.Quantity = OppObj.Expected_Number_Of_Students__c;
	   }
       listOfOpportunityLineItems.add(oli);
    }
	Update listOfOpportunityLineItems;

}

 

 

Sagarika Rout

SFDC Developer

All Answers

GlynAGlynA

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

 

ChrisbrisbaneChrisbrisbane

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.

ChrisbrisbaneChrisbrisbane

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.

 

 

Sagarika RoutSagarika Rout

Hi

 

I have modified your code. Please try below code. If it would help you or not.

Let me know.

 

 

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>{};
	Map<ID, Opportunity> oppList = new Map<ID, Opportunity>();
    
    //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);
	 oppList.put(opp.Id,Opp);
    }
    }
     list<OpportunityLineItem> listOfOpportunityLineItems = new list<OpportunityLineItem>();
    //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);
    Opportunity OppObj = oppList.get(oli.OpportunityId);
	   if(OppObj != NUll){
	       oli.Quantity = OppObj.Expected_Number_Of_Students__c;
	   }
       listOfOpportunityLineItems.add(oli);
    }
	Update listOfOpportunityLineItems;

}

 

 

Sagarika Rout

SFDC Developer

This was selected as the best answer
ChrisbrisbaneChrisbrisbane
Many thanks for providing the update to code Sagarika I have just tested and after changing the Expected_Number_Of_Students__c field and saved the Opportunity all the OpportunityLineItems all changed. Thanks once again
Sagarika RoutSagarika Rout

Its my pleasure that , this could help you....!!!!. Thanks for making it as a solution.

Eelco LitjensEelco Litjens
Hi, i try to use this code with a different field, only the field is a formula (number) which refers to a field in the (related) case.
should it work too?