[SalesForce] 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,
Eric

Best Answer

1) Check your API version of the class

2) Can you try oli.product2.productcode.

As per docs

https://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_opportunitylineitem.htm

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

Related Topic