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
PranavLAXPranavLAX 

Inserting PriceBookEntry getting DUPLICATE_VALUE message

Hi,

 

I am tying to insert an OLI to the Opportunity here via the apex controller class.

 

The following code occurs when the save button is clicked.

 

When I am trying to insert the PriceBookEntry, I am getting the message of Duplicate Value. Please help.

 

 

Opportunity UpgradedOpportunity = new Opportunity(Name='Upgrade From' +CurrOpp.Name,StageName=CurrOpp.StageName,CloseDate=CurrOpp.CloseDate,Upgraded_From__c = opp.Id);
insert UpgradedOpportunity ;

Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];
Pricebook2  ResellerPb = [select id, name, isActive from Pricebook2 where IsStandard = false and Name ='Reseller Price Book' limit 1];

PriceBookEntry PBESW = new PriceBookEntry(Pricebook2ID=StandardPb.Id,Product2ID=opp.Software_Product__c,UseStandardPrice=FALSE, UnitPrice= 2109.0);
System.debug('***************Product2ID'+opp.Software_Product__c);

insert PBESW ;

Best Answer chosen by Admin (Salesforce Developers) 
crop1645crop1645

PranavLax

 

You have a couple of conceptual issues here:

 

1. PricebookEntries should generally already exist before you try inserting an OLI. They represent the junction between a Product2 and a Pricebook for a particular currency

 

2. Assuming you already have your reseller pricebook all set up in advance, then the OLI that you want to insert should set the value of oli.pricebookentryId based on doing a search on PricebookEntry for the one that you want (e.g. a SOQL ... where product2Id = xx && pricebook2Id = resellerPb.id); the other OLI fields are set normally - quantity, unitPrice

 

All Answers

crop1645crop1645

PranavLax

 

You have a couple of conceptual issues here:

 

1. PricebookEntries should generally already exist before you try inserting an OLI. They represent the junction between a Product2 and a Pricebook for a particular currency

 

2. Assuming you already have your reseller pricebook all set up in advance, then the OLI that you want to insert should set the value of oli.pricebookentryId based on doing a search on PricebookEntry for the one that you want (e.g. a SOQL ... where product2Id = xx && pricebook2Id = resellerPb.id); the other OLI fields are set normally - quantity, unitPrice

 

This was selected as the best answer
PranavLAXPranavLAX

Thank you very much.

 

I indeed seem to have conceptual understanding issue here.

 

Previously I had worked on writing only test classes where I had built everything from scratch to insert an OLI.

 

Somehow related that to the actual scenario when coding this out.

 

“I need to query rather that insert”.

 

Working on your implementation now and it should work now.

 

Will reply back incase of any issues.

 

Really appreciate your time in replying to my query.

 

Thank you very much!!