I'm creating a test class right now and one of the things I need to test is whether the ProductCode of a quotelineItem for a certain Quote starts with 'MA' or not. However, when I create a new test class, I can't specify the product code for a QuoteLineItem. How should I go about doing this so that I can properly test out my class?

Here is what I'm attempting to do right now but I'm getting an error saying "Field is not writeable: QuoteLineItem.product_Code__c".

    Product2 testProduct1 = TestUtil.initProduct2(); //This is my test utility class where I initialize any and all objects that I will need during a test class.

   List<QuoteLineItem> qlis=[Select Id,QuoteId from QuoteLineItem where QuoteId=:quoteId];//new List<QuoteLineItem>{qlitem,qlitem2};
        for (QuoteLineItem q:qlis){
            if (q.Product_Code__c==null){
                String qProductCode='MAR-3232-2323';
// and I have also tried
//both of these methods do not work when trying to set the Product Code for a quoteLineItem.

Whats the proper way of going about this? Thanks!


Is your custom field product_code__c a formula field? If so, you can't set it in the testmethod.

QuoteLineItem references its product code via field PricebookEntryId that in turn references a Product2 SObject. If you want to set up test data in QuoteLineItem, you will need to set QuoteLineItem.pricebookEntryId to be the ID of a PricebookEntry for the given combination of Pricebook2 - Currency - Product2 that you wish to test with

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.