+ Start a Discussion
Kumar GKumar G 

Field is not writeable: OpportunityLineItem.ProductCode

dd

Please find the error which i am getting in test class : Field is not writeable: OpportunityLineItem.ProductCode at line 30 column 35  and suggest me how to resolve this.
Suraj TripathiSuraj Tripathi
Hi,
Please send your full test class code here.
Or you can simply comment line 30 where you are updating ProductCode.
Like:
//Comment with double slash before the code start.
Regards
suraj
Amol ChAmol Ch
Hi Kumar,

As error indicates "OpportunityLineItem.ProductCode" is a Read only field. You could not write data in this field either in code or manually.

In your code you have written the field like  OpportunityLineItem.ProductCode="1234". Remove this and try. 

Thanks,
Amol
Kumar GKumar G
Hi Suraj,

Please find my Test class code in code sample.
@isTest(seeAllData=true)
private class Test_OpportunityLineItemCheck 
{
  static testMethod void Checkduplicate() 
  { 
        Test.startTest();

         Pricebook2 pb22 = new Pricebook2(Name='testNonDIE');
           insert pb22;

         Product2 pro2 = new Product2(Name='BXCDXXX',isActive=true,ProductCode = 'TEST');
           insert pro2;
         
         pro2.productcode='RTR2000';
           update pro2;
         
         PricebookEntry[] pbes = [Select Id, UnitPrice, CurrencyIsoCode, Pricebook2Id from PricebookEntry where 
            IsActive = true 
            AND UnitPrice > 0 
            AND CurrencyIsoCode = 'USD' AND Name like 'UK%'];

         Account accP = new Account(Name = 'Partner1', Account_Type__c = 'VAR - MANAGED', RecordTypeId='01280000000Ln6i', Business_Unit__c = 'CBU');
           insert accP;
            
         Opportunity opp4 = new Opportunity(Name= 'Opp31',Pricebook2Id = pbes[0].Pricebook2Id,RecordTypeId = '01280000000Lnks', CloseDate = System.Today().addMonths(3));
           insert opp4;
                        
        OpportunityLineItem oli = new OpportunityLineItem(OpportunityId = opp4.Id,PricebookEntryId = pbes.get(0).Id,Product2Id=pro2.id,Quantity = 1 );        
        OpportunityLineItem oli1 = new OpportunityLineItem(OpportunityId = opp4.Id,PricebookEntryId = pbes.get(0).Id,Product2Id=pro2.id,Quantity = 1 );

        OpportunityLineItem[] olilist=new OpportunityLineItem[]{oli,oli1};

        insert olilist;
        
        OpportunityLineItemCheckOperations.OppWrapper empW = new OpportunityLineItemCheckOperations.OppWrapper();
        empW .OppID = opp4.id;
        empW .productcode = 'FA311';
        empW.compareTo(empW);  

        Test.stopTest();
  }  
  
 }

 
Kumar GKumar G
please find the corresponding class in code sample , i am struggling with code coverage currently this class having 69% , please suggest me how to improve test coverage.
public class OpportunityLineItemCheckOperations {

    public static void checkOnOpportunityLineItems(List<OpportunityLineItem> opportunityProductList){
        
        List<OppWrapper> lstOpps = new List<OppWrapper>();
        Set<String> oppIds = new Set<String>();
        Set<String> productcodeids = new Set<String>();
        
        for (OpportunityLineItem oppline : opportunityProductList){
             productcodeids.add(oppline.productcode);
             oppIds.add(oppline.OpportunityId);
        }
        system.debug('+++++++++++++++++++++'+opportunityProductList[0].opportunityid);
        system.debug('+++++++++++++++++++++'+opportunityProductList[0].productcode);
        List<Opportunity> lstOppos = [SELECT Id, (SELECT Id, opportunityid, productcode , Product2Id FROM OpportunityLineItems WHERE productcode in :productcodeids)
                                            FROM Opportunity WHERE Id in :oppIds ];
        
        
        for (Opportunity oppor : lstOppos){
        
            for (OpportunityLineItem oppline : oppor.OpportunityLineItems){
                OppWrapper oppo = new OppWrapper();         
                oppo.OppID = oppor.Id;
                oppo.productcode = oppline.productcode;
                lstOpps.add(oppo);
            }
        }
        
        lstOpps.sort();
        
        for (OpportunityLineItem oppline: opportunityProductList){
            
            for(OppWrapper oppwrap :lstOpps){
                if (oppline.OpportunityId == oppwrap.OppID && oppline.productcode == oppwrap.productcode)
                    oppline.addError('This product is already added to this opportunity.You cannot add the same product again.');
            }
            
        }
        
        
    }
    
    public class OppWrapper implements Comparable{
        public String OppID;
        public String ProductID;
        public String productcode;
        
        public Integer compareTo(Object compareTo){
            OppWrapper compareToCopy = (OppWrapper) compareTo;
            
            Integer returnValue = 0;
            
            if (this.OppID > compareToCopy.OppID)
                returnValue = 1;
                
            else if (this.OppID < compareToCopy.OppID)
                returnValue = -1;
                
            else if (this.OppID == compareToCopy.OppID){
                if (this.ProductID > compareToCopy.ProductID)
                    returnValue = 1;
                    
                else if (this.ProductID < compareToCopy.ProductID)
                    returnValue = -1;
            }
            
            return returnValue;     
        
        }
    
    }

}

Uncovered Code
Ajay K DubediAjay K Dubedi
Hi kumar
 OpportunitylineItem.productCode [read-only field ] references the value in the ProductCode field of the related Product2 record.
But  You can change the product code using Product2ID.productCode.

Example:
public class chnge_product_code {
    
    public static void m(){
        
        list<OpportunitylineItem> olilist=new list<OpportunityLineItem>();
        set<id> productid=new set<id>();
        olilist=[select OpportunityId,Productcode,Product2Id from OpportunitylineItem limit 5];
        for(opportunitylineItem o:olilist)
        {
           productid.add(o.Product2ID);
        }
        list<Product2> product_list=new list<Product2>();
        product_list=[select productcode from Product2 where ID IN:productid];
        for(Product2 p:product_list){
            
            p.ProductCode='p85';
        }update product_list;
        system.debug(olilist);
    }

}

This code will update the Product code of OpportunityLineItem Because  OpportunitylineItem.productCode references the value in the ProductCode field of related Product record.
If it  helps You then mark this solution as the best answer.