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
king s 8king s 8 

Apex Test Class For Opportunityline item

Throwing this Error :"System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Product ID: id value of incorrect type: 01s2800000DYoWpAAL: [Product2Id]"
 
@isTest
private class updt_opty_testclsss{
     @isTest static void testCallout() {
      //Opportunity opty=[select id,Name,Amount,Business_UnitId__c,Opty_num__c From Opportunity Limit 1];
      Opportunity opty1=new opportunity();
      opty1.name='testing';
      opty1.CloseDate=system.today()+1;
      opty1.StageName='Closed Won';
      insert opty1;
     OpportunityLineItem optyline=new OpportunityLineItem();
     
     Product2 prod = new Product2(Name = 'Laptop X200', Family = 'Hardware');
insert prod;
     
     Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook', isActive=true);
insert customPB;
     
     PricebookEntry customPrice=new PricebookEntry(unitprice=1,Product2Id=customPB.Id,
                                         isActive=true,UseStandardPrice = false);
 insert customPrice;
     
     optyline.Opportunityid=opty1.id;
     optyline.Quantity=1;
     optyline.Totalprice=10000;
     optyline.PricebookEntryId =customPrice.id;
     insert optyline;
     
     optyline.Opportunityid=opty1.id;
     optyline.Quantity=1;
     optyline.Totalprice=10000;
     optyline.PricebookEntryId =customPrice.id;
     update optyline;
     
     }
}

Please Help me on this

Regards
kullayappa
BALAJI CHBALAJI CH
Hi Kullayappa,

There is a small mistake in the code. At line 18, you are assigning Pricbook Id to Product Id. That's the exception is " FIELD_INTEGRITY_EXCEPTION".
Please find below link which might help you:
http://stackoverflow.com/questions/9164986/how-do-i-avoid-standard-price-not-defined-when-unit-testing-an-opportunitylineit/15259316#15259316
https://developer.salesforce.com/forums/?id=906F0000000AeOcIAK
Let us know if that helps you.

Best Rgards,
BALAJI
king s 8king s 8
Hi Banali ,

I got the Isseue Thanks For your Reply,
@IsTest(SeeAllData=true) 
public with sharing class TriggerTestClass {
    static testMethod void validateTrigger() {
       Account acc= new Account(Name = 'testAcc',BillingStreet='testStreet',BillingCity ='tectcity',BillingState='testState',BillingPostalCode='123',
                                   BillingCountry='testcountry',Description='testdesc');
       insert acc;

        //Case record in Test method. 
        Contact conObj = new Contact();
        conObj.lastname = 'testcon';
        conObj.AccountId = acc.id;
        insert conObj;
        
        Case caseObj = new Case();
        caseObj.ContactId = conObj.Id;
        caseObj.AccountId = acc.Id;
        caseObj.Status = 'New Entry';
        caseObj.ContactId = conObj.Id;
        caseObj.Origin = 'Email';
        insert caseObj;
    
       Opportunity opp= new Opportunity(AccountId=acc.id,Amount=1234.00,Description='testdesc',Name='testOpp',
                                         StageName='Prospecting',CloseDate = System.Today());
       insert opp;
       
       PricebookEntry priceBookEntryNew = new PricebookEntry();
        Product2 product = new Product2(); 
        PriceBook2 pb2 = new PriceBook2 (Name='Standard priceBook',Description = 'test');
        insert pb2;
        List pricebookList = [Select id from PriceBook2 where  IsStandard = TRUE ]; 
        PriceBook2 pricebooktest = new PriceBook2 ();
        if(pricebookList != null && pricebookList.size()>0)
            pricebooktest = pricebookList.get(0);
        product.name = 'Test';
        insert product;
        
        priceBookEntryNew.Product2Id = product.Id;
        priceBookEntryNew.PriceBook2Id = pricebooktest.Id;
        priceBookEntryNew.UnitPrice = 20.00;
        priceBookEntryNew.UseStandardPrice = false;
        priceBookEntryNew.isactive = true;
        insert priceBookEntryNew;
        
                       
        OpportunityLineItem oli = new OpportunityLineItem
            (
                OpportunityId = opp.Id,
                PricebookEntryId = priceBookEntryNew.Id,
                Quantity = 1,
                UnitPrice = priceBookEntryNew.UnitPrice, 

                ServiceDate = System.today()
            );
      insert oli;
    }
}

Regards
kullayappa
BALAJI CHBALAJI CH
Great to hear that...
Please close this thread so that it can be useful to someone else with similar issue.

Best Regards,
BALAJI