Accessing OpportunityLineItem ProductCode field

Currently I’m having issue saving my code:

public static void myMethodMyMethod(Map<Id,OpportunityLineItem> OLIs, String action){

        for(OpportunityLineItem oli:OLIs.values()){
            system.debug('oli.ProductCode '  + oli.ProductCode);


I keep getting this error: Invalid field ProductCode for SObject OpportunityLineItem

I was able to print out other fields information from sObject oli (i.e. OpportunityId, UnitPrice, etc). For some reason ProductCode does not work…

Any advice?

Thank you,


1) Check your API version of the class

2) Can you try oli.product2.productcode.

As per docs

ProductCode :This read-only field is available in API version 30.0 and
later. It references the value in the ProductCode field of the related
Product2 record

Source : Link , Question Author : Eric Luu , Answer Author : Rao

Leave a Comment