You need to sign in to do that
Don't have an account?
Danielle 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!
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!
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
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,
All Answers
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,
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
I will try the logic in my org and will share the same to you.
Thanks,
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
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,
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;
}
}
}
Are you deploying to from one sandbox to other sandbox with out running test classes?
Thanks,
I'm reading that that is the issue. I'm just not sure what that means or how to execute on it.
Danielle