I have a JSON payload I'm processing from a REST API web service in Salesforce. I converted the JSON to a DTO object in APEX with the following:

WorkOrderDTO Class

global class WorkOrderDTO extends DTO {

    public List<LineItem> OrderItems {get; set;}

    public class LineItem {
        public string Service {get; set;}

WorkOrderAPI Class

public class WorkOrderAPI {

    WorkOrderDTO WO = (WorkOrderDTO) JSON.deserialize(RestContext.request.requestBody.toString(), WorkOrderDTO.class);

    for(Object lineItem :  WO.OrderItems){
        String service = String.valueOf(lineItem.Service);

Unfortunately when I attempt a simple dot notation like through the lineItem inner class, I get the following error message:

Variable does not exist: Service

I believe I had this working at one time previously so I feel like I'm just missing a small detail. Appreciate the help.


You should typecast lineItem variable as well before using as a plain object like below:

WorkOrderDTO.LineItem li = (WorkOrderDTO.LineItem)lineItem;
String service = String.valueOf(li.Service);

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.