+ Start a Discussion
Chidanand Magadum 24Chidanand Magadum 24 

Line: 51, Column: 1 System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field

Hi guyz,
            when i run the below code, it is giving me the exception.(Line: 51, Column: 1
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: PricebookEntryId, unknown (versions 3.0 and higher must specify pricebook entry id, others must specify product id): [PricebookEntryId, unknown])




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>>();
 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='01tJ0000003qkck';
        pe.UnitPrice=500;
        //entryList.add(pe);
        insert entryList;
        OpportunityLineItem oli = new OpportunityLineItem();
        oli.OpportunityId = o.Id;
        oli.PricebookEntryId=pe.Id;
        oli.UnitPrice = 500;
        oli.Quantity=5;
        insert oli;
        //PriceBookEntry pe= new PriceBookEntry();
        //List<PriceBookEntry> priceBookList = [select Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry where Product2.Id='01tJ0000003qkck' And
       // PriceBook2.isStandard=true LIMIT 1];
        //OpportunityLineItem oli = new OpportunityLineItem();
        //oli.OpportunityId = o.Id;
        //oli.PricebookEntryId=priceBookList[0].Id;
        //oli.PricebookEntryId=stdpb.Id;
        //priceBookList[0].UnitPrice=100;
        //oli.UnitPrice = a.ARR__c;
        //oli.UnitPrice = 500;
        //oli.Quantity=5;
        //oliList.add(oli);
        //AllOpps.add(oliList);
        //update(AllOpps);
        //insert oli;
        
        }
        
        
    }
    }
// system.debug('Diff==='+numberDaysDue);
 
 }
PratikPratik (Salesforce Developers) 
Hi Chidanand,

Please refer to this article:
https://help.salesforce.com/apex/HTViewSolution?id=000166658&language=en_US

Thanks,
Pratik