You need to sign in to do that
Don't have an account?
JamesM
APEX code to insert opportunity line item
Hello All
Im having a few issues with an Apex class im calling using the process builder.
The following code is creating the OLI ok, but only when a specific pricebook is assigned to the Opp.
Essentially im unable to find a way to set the pricebook to the one that is already assigned to the Opp, as currently its just using the first active pricebook it finds that the product is present in. Resulting in the following error; "pricebook entry is in a different pricebook than the one assigned to the opportunity".
Just some extra info, as im sure im not doing this in the best manner.
Im currently tasked with setting up fully automatic Lead conversion, and iv completed everystep up to this.
So leads are automatically converted in accounts, contacts and opps and have a pricebook assigned to them.
I have custom fields on the opportunity that will effect the prodcuts that will be added aswell as products that are in multiple pricebooks.
This last step is casing me no end of trouble, and iv not even added in any logic yet for the custom fields.
Thank you in advance for any help that can be offered.
Regards
James
Im having a few issues with an Apex class im calling using the process builder.
The following code is creating the OLI ok, but only when a specific pricebook is assigned to the Opp.
Essentially im unable to find a way to set the pricebook to the one that is already assigned to the Opp, as currently its just using the first active pricebook it finds that the product is present in. Resulting in the following error; "pricebook entry is in a different pricebook than the one assigned to the opportunity".
Public class AutoCreateProduct { @InvocableMethod public static void ProductAssign(List<Id> OppIds) { List<OpportunityLineItem> oliList = new List<OpportunityLineItem>(); List<PriceBookEntry> priceBookList = [SELECT Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry WHERE Product2Id='01ta0000003vqHeAAI' AND PriceBook2.isActive=true LIMIT 1]; OpportunityLineItem oli = new OpportunityLineItem (OpportunityId=OppIds[0], PricebookEntryId=priceBookList[0].Id, Quantity = 1); oliList.add(oli); insert oliList; } }
Just some extra info, as im sure im not doing this in the best manner.
Im currently tasked with setting up fully automatic Lead conversion, and iv completed everystep up to this.
So leads are automatically converted in accounts, contacts and opps and have a pricebook assigned to them.
I have custom fields on the opportunity that will effect the prodcuts that will be added aswell as products that are in multiple pricebooks.
This last step is casing me no end of trouble, and iv not even added in any logic yet for the custom fields.
Thank you in advance for any help that can be offered.
Regards
James
You can compare the CurrencyISOCode and assign the record appropriately.
Regards,
Mahesh
All Answers
You can try below code:
Regards,
Mahesh
Thanks alot for that.
It looks like the correct pricebook is now being selected, but is resulting in an error as it isnt selecting the price with the same currency value as the opp.
The Products usually have three prices, EUR, USD and GBP
thanks again
Regards
James
You can compare the CurrencyISOCode and assign the record appropriately.
Regards,
Mahesh
Perfect thanks alot, this has made my morning.
I just needed to remove the "LIMIT 1" as it wasnt storing the three currencys only the first one
Regards
James