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
Danielle Rosen 8Danielle Rosen 8 

opportunity line item trigger to opp update help

Hello,

I am completely new to Apex and triggers, and hoping someone can help with my first one. What I am trying to do is every time an opportunity line item is created, if it is a particular ProductId, I want it to populate a custom field on the parent opportunity. I'd like for it to do the same thing when the OLI sales price is updated, it should update the appropriate custom field. 

Any guidance here would be very appreciated as I am unsure how to get started. Thanks so much in advance!
Best Answer chosen by Danielle Rosen 8
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Danielle,

Can you try the below code so I can share the test class and explanation for the same.

Here I used product code instead of product. Change the product code based on the product you want. 

 Also I created oli_price__c field on opporunity to capture unit price so replace it with your API name
 
trigger Updateopporutiyfromline on OpportunityLineItem (after insert,after update) {
    map<id,Decimal> mapcost= new map<id,Decimal>();
    OpportunityLineItem oldoli = new OpportunityLineItem();
    for(OpportunityLineItem oli:Trigger.new){
        if(Trigger.isupdate)
         oldoli= trigger.oldmap.get(oli.id);
        if(oli.ProductCode=='GC1060' &&(Trigger.isinsert || (Trigger.isupdate && oli.unitprice!=oldoli.UnitPrice))) {
           mapcost.put(oli.OpportunityId, oli.UnitPrice); 
            system.debug('into it');
        }
        
    }
    
    if(mapcost.size()>0){
        List<Opportunity> opplist=[select id,oli_price__c  from opportunity where id in :mapcost.keySet()];
        List<Opportunity> opptobeupdate= new List<Opportunity>();
        
        for(opportunity opp:opplist){
            opp.oli_price__c=mapcost.get(opp.id);
            opptobeupdate.add(opp);
        }
        
        if(opptobeupdate.size()>0)
            update opptobeupdate;
    }
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Danielle,

Are you using standard objects and also can you confirm you only want the field to update only for particular product for both price and Name.


Are you using Opportunity Product?

Thanks,
 
Danielle Rosen 8Danielle Rosen 8
Hi Sai,

Thanks so much for your response. I am only using the Opportunity and the OpportunityLineItem objects, so yes all standard. I only need the OLI to update the corresponding custom field on the opportunity. So for example, we have a product named "Monthly Minimum", so if that OLI is created or if the unit price is updated, I would want it to populate/update the Monthly Minimum currency field on the parent opportunity.

Danielle
Danielle Rosen 8Danielle Rosen 8
@Ankaiah that's correct.
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Danielle,

I will try the logic in my org and will share the same to you.

Thanks,
 
Danielle Rosen 8Danielle Rosen 8
Wow, thank you so much! Hoping this will help me learn how to do these on my own in the future. Appreciate it!
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Danielle,

Can you try the below code so I can share the test class and explanation for the same.

Here I used product code instead of product. Change the product code based on the product you want. 

 Also I created oli_price__c field on opporunity to capture unit price so replace it with your API name
 
trigger Updateopporutiyfromline on OpportunityLineItem (after insert,after update) {
    map<id,Decimal> mapcost= new map<id,Decimal>();
    OpportunityLineItem oldoli = new OpportunityLineItem();
    for(OpportunityLineItem oli:Trigger.new){
        if(Trigger.isupdate)
         oldoli= trigger.oldmap.get(oli.id);
        if(oli.ProductCode=='GC1060' &&(Trigger.isinsert || (Trigger.isupdate && oli.unitprice!=oldoli.UnitPrice))) {
           mapcost.put(oli.OpportunityId, oli.UnitPrice); 
            system.debug('into it');
        }
        
    }
    
    if(mapcost.size()>0){
        List<Opportunity> opplist=[select id,oli_price__c  from opportunity where id in :mapcost.keySet()];
        List<Opportunity> opptobeupdate= new List<Opportunity>();
        
        for(opportunity opp:opplist){
            opp.oli_price__c=mapcost.get(opp.id);
            opptobeupdate.add(opp);
        }
        
        if(opptobeupdate.size()>0)
            update opptobeupdate;
    }
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
This was selected as the best answer
Danielle Rosen 8Danielle Rosen 8
Thank you so much! Will try now and let you know.
Danielle Rosen 8Danielle Rosen 8
Hi Sai, 

Here is what I entered and received the below error. We don't use product codes, so I changed that to the Product2.Id and the oli_price__c field on the Opp is CPM1__c. I feel like I may have messed something up...

Error: Compile Error: Extra '<EOF>', at '}'. at line 29 column 1

trigger Updateopportunityfromline on OpportunityLineItem (after insert,after update) {
    map<id,Decimal> mapcost= new map<id,Decimal>();
    OpportunityLineItem oldoli = new OpportunityLineItem();
    for(OpportunityLineItem oli:Trigger.new){
        if(Trigger.isupdate)
         oldoli= trigger.oldmap.get(oli.id);
        if(oli.Product2.Id=='01t2E00000O3paYQAR' &&(Trigger.isinsert || (Trigger.isupdate && oli.unitprice!=oldoli.UnitPrice))) {
           mapcost.put(oli.OpportunityId, oli.UnitPrice); 
            system.debug('into it');
        }
        
    }
    
    if(mapcost.size()>0){
        List<Opportunity> opplist=[select id,CPM1__c  from opportunity where id in :mapcost.keySet()];
        List<Opportunity> opptobeupdate= new List<Opportunity>();
        
        for(opportunity opp:opplist){
            opp.CPM1__c=mapcost.get(opp.id);
            opptobeupdate.add(opp);
        }
        
        if(opptobeupdate.size()>0)
            update opptobeupdate;
    }
}


}
Danielle Rosen 8Danielle Rosen 8
ah i did get it to save but I don't believe it is updating the field on the opportunity...
Danielle Rosen 8Danielle Rosen 8
I got it! I just needed to get rid of the . on Product2.Id. Thank you so much for your help Sai!
Danielle Rosen 8Danielle Rosen 8
Hi Sai I'm running into an issue on deployment. I'm receiving an error that says I have 0% code coverage, even tho they seemed to be fine in the sandbox. Am I missing a step somewhere?
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Are you deploying to from one sandbox to other sandbox with out running test classes?

Thanks,


 
Danielle Rosen 8Danielle Rosen 8
Hi Sai,

I'm reading that that is the issue. I'm just not sure what that means or how to execute on it.

Danielle