• JamesM
  • NEWBIE
  • 10 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
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".
 
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
 
  • April 13, 2016
  • Like
  • 0
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".
 
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
 
  • April 13, 2016
  • Like
  • 0