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
Pallavi ShendePallavi Shende 

System.SObjectException: Field is not writeable: "OpportunityLineItem.PricebookEntryId"

Hi Expert, I am getting "System.SObjectException: Field
@isTest
public class OppNamesTest {
    static testmethod void testclass(){
        
    //insert product
    Product2 prod = new Product2(Name = 'Laptop X200',Family = 'Hardware');
    insert prod;
    
  	 Id pricebookId = Test.getStandardPricebookId();
        
     //create a new Opportunity
	Opportunity opp1 = new  Opportunity();
    opp1.Name = 'Test trigger';
    opp1.CloseDate = Date.today();
    opp1.StageName = 'Sourcing Demand';
    insert opp1;
        
    //create a new Opportunity2
	Opportunity opp2 = new  Opportunity();
    opp2.Name = 'Test trigger1';
    opp2.CloseDate = Date.today();
    opp2.StageName = 'Sourcing Demand';
    insert opp2;
        
    //insert pricebook
    PricebookEntry pbe = new PricebookEntry();
    pbe.Pricebook2Id=pricebookId;
    pbe.Product2Id = prod.Id;
    pbe.isActive=True;
   	pbe.UnitPrice = 0.70;
  	pbe.UseStandardPrice=false;
	 insert pbe;
        

        
        //create OLI
       	OpportunityLineItem opli = new OpportunityLineItem();
    	opli.UnitPrice = 57;
    	opli.Quantity = 1;
    	opli.OpportunityId=opp1.Id;
       // opli.PricebookEntryId= pbe.Id;
    	opli.Product2Id = prod.Id;
       	insert opli;
        
        //create OLI
       	OpportunityLineItem opli1 = new OpportunityLineItem();
    	opli1.UnitPrice = 40;
    	opli1.Quantity = 1;
    	opli1.OpportunityId=opp2.Id;
        opli.PriceBookEntryId=pbe.Id;
    	opli1.Product2Id = prod.Id;
       	insert opli1;	
        
        map<ID,string> MapofNames = new map<ID,string>();
        Product2 prd = new Product2();
        
        MapofNames.put(opli.OpportunityID,opli.Product2Id);
        MapofNames.put(opli1.OpportunityID,opli1.Product2Id);
        
        string namesofOpp1 = [select name from opportunity where Id IN: MapofNames.keySet()].name;
        prd = [select Id,Opportunity_Names__c from Product2 where Id in: MapofNames.values()];
        string allnames=(string) prd.get('Opportunity_Names__c');
        
        prd.Opportunity_Names__c = allnames+';'+namesofOpp1;// concanated the names previous and current
        update prd;
        
        System.assertEquals('Test trigger;Test trigger1', prd.Opportunity_Names__c);
    }
}

is not writeable: "OpportunityLineItem.PricebookEntryId" this error in test class.
Best Answer chosen by Pallavi Shende
Ravi Dutt SharmaRavi Dutt Sharma
You cannot specify Product2Id (line 42 and line 51 in your code needs to be commented) while creating an OLI. Only specify the PricebookEntryId.

All Answers

Steven NsubugaSteven Nsubuga
You have a typo on line 50, you have opli.PriceBookEntryId=pbe.Id; BUT it should be opli1.PriceBookEntryId=pbe.Id;
sd
@isTest
public class OppNamesTest {
    static testmethod void testclass(){
        
    //insert product
    Product2 prod = new Product2(Name = 'Laptop X200',Family = 'Hardware');
    insert prod;
    
  	 Id pricebookId = Test.getStandardPricebookId();
        
     //create a new Opportunity
	Opportunity opp1 = new  Opportunity();
    opp1.Name = 'Test trigger';
    opp1.CloseDate = Date.today();
    opp1.StageName = 'Sourcing Demand';
    insert opp1;
        
    //create a new Opportunity2
	Opportunity opp2 = new  Opportunity();
    opp2.Name = 'Test trigger1';
    opp2.CloseDate = Date.today();
    opp2.StageName = 'Sourcing Demand';
    insert opp2;
        
    //insert pricebook
    PricebookEntry pbe = new PricebookEntry();
    pbe.Pricebook2Id=pricebookId;
    pbe.Product2Id = prod.Id;
    pbe.isActive=True;
   	pbe.UnitPrice = 0.70;
  	pbe.UseStandardPrice=false;
	 insert pbe;
        

        
        //create OLI
       	OpportunityLineItem opli = new OpportunityLineItem();
    	opli.UnitPrice = 57;
    	opli.Quantity = 1;
    	opli.OpportunityId=opp1.Id;
       // opli.PricebookEntryId= pbe.Id;
    	opli.Product2Id = prod.Id;
       	insert opli;
        
        //create OLI
       	OpportunityLineItem opli1 = new OpportunityLineItem();
    	opli1.UnitPrice = 40;
    	opli1.Quantity = 1;
    	opli1.OpportunityId=opp2.Id;
        opli1.PriceBookEntryId=pbe.Id;
    	opli1.Product2Id = prod.Id;
       	insert opli1;	
        
        map<ID,string> MapofNames = new map<ID,string>();
        Product2 prd = new Product2();
        
        MapofNames.put(opli.OpportunityID,opli.Product2Id);
        MapofNames.put(opli1.OpportunityID,opli1.Product2Id);
        
        string namesofOpp1 = [select name from opportunity where Id IN: MapofNames.keySet()].name;
        prd = [select Id,Opportunity_Names__c from Product2 where Id in: MapofNames.values()];
        string allnames=(string) prd.get('Opportunity_Names__c');
        
        prd.Opportunity_Names__c = allnames+';'+namesofOpp1;// concanated the names previous and current
        update prd;
        
        System.assertEquals('Test trigger;Test trigger1', prd.Opportunity_Names__c);
    }
}

 
SANDEEP CHITTINENISANDEEP CHITTINENI
If you are inserting the record please check that the user is having permission to create or else if you're updating the record check if the user is having an edit permission.
 
Thank you,
Sandeep Chittineni.
Ravi Dutt SharmaRavi Dutt Sharma
You cannot specify Product2Id (line 42 and line 51 in your code needs to be commented) while creating an OLI. Only specify the PricebookEntryId.
This was selected as the best answer