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
shruthikashruthika 

Urgent:FIELD_INTEGRITY_EXCEPTION

Hi all,

 

I am getting an error while writing test class on opportunity line item.

 

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: PricebookEntryId (pricebook entry currency code does not match opportunity currency code): [PricebookEntryId]

 

This is the error am getting please give me the solution as soon as possible.

 

Below is the code which i have written.....

 

 

@isTest
private class TestOpportunityProductLineCheck
{
    static testMethod void myTest()
   { 
            Opportunity o = new Opportunity(Id='006T0000008kKcc', Name = 'praveen', StageName='B - Prove', CloseDate = Date.today());
            update o;

            Pricebook2 standardPB = [select id from Pricebook2 where isStandard=true];

            Pricebook2 pb = new Pricebook2(Id='01s30000000A8FCAA0', Name= 'PSC Price Book', IsActive = true);
            update pb;
            
            Product2 prod = new Product2(Name = 'Orbix Product',IsActive = true);
            insert prod;
    
            
            PricebookEntry standardPrice = new PricebookEntry(Pricebook2Id = standardPB.Id, Product2Id = prod.Id, UnitPrice = 10000, IsActive = true, UseStandardPrice = false);
        insert standardPrice;
        
        PricebookEntry pbe = new PricebookEntry(Pricebook2Id = pb.Id, Product2Id = prod.Id, UnitPrice = 10000, IsActive = true, UseStandardPrice = false);
        insert pbe;

        
        //PricebookEntry pbe = new PricebookEntry(Pricebook2Id = '01s30000000A8FCAA0', Product2Id = prod.id, UnitPrice=100, UseStandardPrice = false, IsActive = true);
         //insert pbe ;
    
            
            //OpportunityLineItem ol = new OpportunityLineItem(OpportunityId = o.id , Quantity = 20 , TotalPrice = 100,PricebookEntryId = pbe.id );
            //insert ol;
    
            OpportunityLineItem oli = new OpportunityLineItem(opportunityId = o.Id, PricebookEntryId= pbe.Id, Quantity = 1, UnitPrice = 7500);
            insert oli;

List<OpportunityLineItem> olis = [Select Id From OpportunityLineItem Where OpportunityId =: o.Id];
        update olis[0];

            
 }
}



 

Thanks in advance.....

shruthika...


yashagarwalyashagarwal

Hi Sruthika,

 

I have modifed you code below :

 

@isTest
private class TestOpportunityProductLineCheck
{
    static testMethod void myTest()
   {
            Opportunity o = new Opportunity(Id='006T0000008kKcc', Name = 'praveen', StageName='B - Prove', CloseDate = Date.today());
            update o;

            Pricebook2 standardPB = [select id from Pricebook2 where isStandard=true];

            Pricebook2 pb = new Pricebook2(Id='01s30000000A8FCAA0', Name= 'PSC Price Book', IsActive = true);
            update pb;
           
            Product2 prod = new Product2(Name = 'Orbix Product',IsActive = true);
            insert prod;
   
            // added code
            Opportunity opp = [Select id,CurrencyIsoCode from Opportunity where Id = '006T0000008kKcc'];
           
           
           
            PricebookEntry standardPrice = new PricebookEntry(Pricebook2Id = standardPB.Id,CurrencyIsoCode = opp.CurrencyIsoCode,Product2Id = prod.Id, UnitPrice = 10000, IsActive = true, UseStandardPrice = false);
        insert standardPrice;
       
        PricebookEntry pbe = new PricebookEntry(Pricebook2Id = pb.Id,CurrencyIsoCode = opp.CurrencyIsoCode,Product2Id = prod.Id, UnitPrice = 10000, IsActive = true, UseStandardPrice = false);
        insert pbe;

       
        //PricebookEntry pbe = new PricebookEntry(Pricebook2Id = '01s30000000A8FCAA0', Product2Id = prod.id, UnitPrice=100, UseStandardPrice = false, IsActive = true);
         //insert pbe ;
   
           
            //OpportunityLineItem ol = new OpportunityLineItem(OpportunityId = o.id , Quantity = 20 , TotalPrice = 100,PricebookEntryId = pbe.id );
            //insert ol;
   
            OpportunityLineItem oli = new OpportunityLineItem(opportunityId = o.Id, PricebookEntryId= pbe.Id, Quantity = 1, UnitPrice = 7500);
            insert oli;

List<OpportunityLineItem> olis = [Select Id From OpportunityLineItem Where OpportunityId =: o.Id];
        update olis[0];

           
 }
}

 

 

Let me know if this works. :)

Rahul SharmaRahul Sharma

An advise, Never use Hardcored Id in testmethod and create test data in the same.

Try this:

 

@isTest
private class TestOpportunityProductLineCheck
{
  static testMethod void myTest()
  { 
    Test.startTest();
    Account objAccount1= new Account(Name = 'Test Account 1');
    Insert objAccount1;
    
    Opportunity objOpportunity1 = new Opportunity(StageName = 'Closed Won', 
    CloseDate = system.today(), Name = 'Test Opportunity 1', AccountId = objAccount1.Id);
    Insert objOpportunity1;
    
    OpportunityLineItem oli = new OpportunityLineItem(opportunityId = objOpportunity1.Id, PricebookEntryId = [Select Id from PricebookEntry limit 1].Id, Quantity = 1, UnitPrice = 7500);
    insert oli;
    Test.stopTest();
  }
}

 Hope it helps.