You need to sign in to do that
Don't have an account?
Chidanand Magadum 24
Line: 47, Column: 1 System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, This price definition already exists in this price book: []
Hi all,
I am getting the following error for my code. Any workaround.
Line: 47, Column: 1
System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, This price definition already exists in this price book: []
Code.
Date d = Date.Today();
List<Account> AllAccounts= new List<Account>([select id,Name,Service_End_Date__c,Customer_Success_Manager__c,ARR__c,(Select Name from Opportunities) from Account ]);
Map<String,Set<String>> MapAccwithOpps =new Map<String,Set<String>>();
List<PriceBookEntry> entryList = new List<PriceBookEntry>();
for(Account a:AllAccounts)
{
Set<String> Oppnames = new Set<String>();
//string Oppnames='';
for(Opportunity opp:a.Opportunities)
{
Oppnames.add(opp.Name);
}
MapAccwithOpps.put(a.Name,Oppnames);
}
system.debug('MapAccwithOpps==='+MapAccwithOpps);
for(Account a:AllAccounts)
{
//Integer numberDaysDue= a.Service_End_Date__c.daysBetween(d);
if(a.Service_End_Date__c!=NULL){
Integer numberDaysDue= d.daysBetween(a.Service_End_Date__c);
if(numberDaysDue<=60)
{
Opportunity O= new Opportunity();
o.Name=a.Name;
o.StageName='Legal';
o.Amount=a.ARR__c;
o.AccountId=a.ID;
o.CloseDate=Date.today();
o.Customer_Success_Manager__c=a.Customer_Success_Manager__c;
o.NextStep='Won';
o.OwnerID='005d0000001Kt9D';
if(!(MapAccwithOpps.get(a.Name).contains(o.Name))) {
insert o;
}
List<Opportunity> AllOpps = new List<Opportunity>([select id,Name from Opportunity where Name='o.Name']);
List<OpportunityLineItem> oliList = new List <OpportunityLineItem>();
Pricebook2 stdpb = [SELECT Id FROM Pricebook2 WHERE IsStandard = true];
//List<PriceBookEntry> entryList = new List<PriceBookEntry>();
PriceBookEntry pe= new PriceBookEntry();
pe.Pricebook2Id=stdpb.Id;
pe.Product2Id='01tJ0000003qkev';
pe.UnitPrice=500;
pe.IsActive=true;
entryList.add(pe);
insert pe;
List<PriceBookEntry> priceBookList = [select Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry where Product2.Id='01tJ0000003qkev' And
PriceBook2.isStandard=true LIMIT 1 ];
OpportunityLineItem oli = new OpportunityLineItem();
oli.OpportunityId = o.Id;
oli.PricebookEntryId=priceBookList[0].Id;
oli.UnitPrice = 500;
oli.Quantity=5;
insert oli;
}
}
}
I am getting the following error for my code. Any workaround.
Line: 47, Column: 1
System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, This price definition already exists in this price book: []
Code.
Date d = Date.Today();
List<Account> AllAccounts= new List<Account>([select id,Name,Service_End_Date__c,Customer_Success_Manager__c,ARR__c,(Select Name from Opportunities) from Account ]);
Map<String,Set<String>> MapAccwithOpps =new Map<String,Set<String>>();
List<PriceBookEntry> entryList = new List<PriceBookEntry>();
for(Account a:AllAccounts)
{
Set<String> Oppnames = new Set<String>();
//string Oppnames='';
for(Opportunity opp:a.Opportunities)
{
Oppnames.add(opp.Name);
}
MapAccwithOpps.put(a.Name,Oppnames);
}
system.debug('MapAccwithOpps==='+MapAccwithOpps);
for(Account a:AllAccounts)
{
//Integer numberDaysDue= a.Service_End_Date__c.daysBetween(d);
if(a.Service_End_Date__c!=NULL){
Integer numberDaysDue= d.daysBetween(a.Service_End_Date__c);
if(numberDaysDue<=60)
{
Opportunity O= new Opportunity();
o.Name=a.Name;
o.StageName='Legal';
o.Amount=a.ARR__c;
o.AccountId=a.ID;
o.CloseDate=Date.today();
o.Customer_Success_Manager__c=a.Customer_Success_Manager__c;
o.NextStep='Won';
o.OwnerID='005d0000001Kt9D';
if(!(MapAccwithOpps.get(a.Name).contains(o.Name))) {
insert o;
}
List<Opportunity> AllOpps = new List<Opportunity>([select id,Name from Opportunity where Name='o.Name']);
List<OpportunityLineItem> oliList = new List <OpportunityLineItem>();
Pricebook2 stdpb = [SELECT Id FROM Pricebook2 WHERE IsStandard = true];
//List<PriceBookEntry> entryList = new List<PriceBookEntry>();
PriceBookEntry pe= new PriceBookEntry();
pe.Pricebook2Id=stdpb.Id;
pe.Product2Id='01tJ0000003qkev';
pe.UnitPrice=500;
pe.IsActive=true;
entryList.add(pe);
insert pe;
List<PriceBookEntry> priceBookList = [select Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry where Product2.Id='01tJ0000003qkev' And
PriceBook2.isStandard=true LIMIT 1 ];
OpportunityLineItem oli = new OpportunityLineItem();
oli.OpportunityId = o.Id;
oli.PricebookEntryId=priceBookList[0].Id;
oli.UnitPrice = 500;
oli.Quantity=5;
insert oli;
}
}
}
Shashank (Salesforce Developers)
You may find this helpful: https://success.salesforce.com/answers?id=90630000000gmJnAAI