You need to sign in to do that
Don't have an account?
NBP1411
Adding Product X to Opportunities with Y and Z via Apex
I am trying to create Apex code to automatically add Product X to all future opportunities with Products Y and Z? What would be the best way to do this?
This is the code I have so far:
trigger CreateOLI on Opportunity (after insert) {
List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
List<PriceBookEntry> priceBookList = [SELECT Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry WHERE Product2Id='01t1a000000Cwc3' AND PriceBook2.isStandard=true LIMIT 1];
for (Opportunity oppty: Trigger.new) {
if (/OLI.Name=Z/) (/OLI.Name=Y) {
//create new Oli
OpportunityLineItem oli = new OpportunityLineItem(OpportunityId=oppty.Id, PricebookEntryId=priceBookList[0].Id /*rest of required fields*/);
oliList.add(oli);
}
}
insert oliList;
}
This is the code I have so far:
trigger CreateOLI on Opportunity (after insert) {
List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
List<PriceBookEntry> priceBookList = [SELECT Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry WHERE Product2Id='01t1a000000Cwc3' AND PriceBook2.isStandard=true LIMIT 1];
for (Opportunity oppty: Trigger.new) {
if (/OLI.Name=Z/) (/OLI.Name=Y) {
//create new Oli
OpportunityLineItem oli = new OpportunityLineItem(OpportunityId=oppty.Id, PricebookEntryId=priceBookList[0].Id /*rest of required fields*/);
oliList.add(oli);
}
}
insert oliList;
}
You will need to fix the syntax to make it compile and work properly.
thx
if (/OLI.Name=Z/) (/OLI.Name=Y)
What are you trying to compare? OLI is not defined yet.
As a common practice, if your question is answered, please choose 1 best answer.
But you can give every answer a thumb up if that answer is helpful to you.
Thanks
Thank you for the response. What I am trying to compare is if an Opportunity has products X and Y in it, to then allow the trigger to happen. How would I be able to do that?
If you did this from a Process it would look something like this: