You need to sign in to do that
Don't have an account?

System.SObjectException: Field is not writeable: "OpportunityLineItem.PricebookEntryId"
Hi Expert, I am getting "System.SObjectException: Field
is not writeable: "OpportunityLineItem.PricebookEntryId" this error in test class.
@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.
All Answers
sd