1

I was trying to avoid posting this on here with so many threads on this topic, but I am really hoping someone can help me figure out what I'm missing here.

I keep receiving an "Illegal Value for Primitive" error on my Class but I can't figure out where. I did try to use the JSON2APEX heroku app but due to the Strings "1" and "2" located in the QuerySalesResult Object the class it creates doesn't actually work so I had to try to make it from scratch.

I have provided the Class I built, an example JSON Response, and the line I'm using to parse the response.

Any and all assistance will be greatly appreciated!

APEX Class - Case Details

public class CaseDetails {

    public class First {
        public String USR_USERNAME;
    }
    
    public class Second {
        public String USR_USERNAME;
    }

    public class QuerySalesResult {
        public First First;
        public Second Second;
    }

    public String SYS_LANG;
    public String SYS_SKIN;
    public String SYS_SYS;
    public String APPLICATION;
    public String PROCESS;
    public String TASK;
    public Integer INDEX;
    public String USER_LOGGED;
    public String USR_USERNAME;
    public String APP_NUMBER;
    public String PIN;
    public String ServiceRep;
    public String SubmitDate;
    public String CustomerName;
    public String Company;
    public String ContactName;
    public String CustomerContactNumber;
    public String CustomerEmailAddress;
    public String CustomerAddress;
    public String ApartmentSelection;
    public String City;
    public String State;
    public String Zipcode;
    public String SubdivisionSelection;
    public String RequestTypeMain;
    public String ServiceTypeRequested;
    public String NextRoutingGroup;
    public String Source;
    public String Rep;
    public String UserEmail;
    public String SalesSupportMemberBegan;
    public QuerySalesResult QuerySalesResult;
    public String UsersFoundMessage;
    public String VarChanged;
    
    public static CaseDetails parse(String json) {
        return (CaseDetails) System.JSON.deserialize(json, CaseDetails.class);
    }
}

JSON - Example Response

{
    "SYS_LANG": "en",
    "SYS_SKIN": "neoclassic",
    "SYS_SYS": "workflow",
    "APPLICATION": "64637120363067e726c30d2039563209",
    "PROCESS": "81243839356426812f20202086265219",
    "TASK": "735078682564268abd74659056301709",
    "INDEX": 1,
    "USER_LOGGED": "48346802763066bd151bba3065154523",
    "USR_USERNAME": "",
    "APP_NUMBER": "38149",
    "PIN": "6SH2",
    "ServiceRep": "Bob Stewart",
    "SubmitDate": "08-24-2022",
    "CustomerName": "Big Guy",
    "Company": "CompanyRKHL",
    "ContactName": "Bob Stewart",
    "CustomerContactNumber": "123-456-789",
    "CustomerEmailAddress": "[email protected]",
    "CustomerAddress": "101 Fake Address Lane",
    "ApartmentSelection": "AptSelectionApartment",
    "City": "Charleston",
    "State": "StateSC",
    "Zipcode": "29401",
    "SubdivisionSelection": "SubSelectionNA",
    "RequestTypeMain": "RequestTypeNewConstruction",
    "ServiceTypeRequested": "ServiceTypeRequestedTelephone",
    "NextRoutingGroup": "Assignment",
    "Source": "SalesForceBus",
    "Rep": "SalesForceUser",
    "UserEmail": null,
    "SalesSupportMemberBegan": "No",
    "QuerySalesResult": {
        "1": {
            "USR_USERNAME": "abcdefg"
        },
        "2": {
            "USR_USERNAME": "JessicaDrew"
        }
    },
    "UsersFoundMessage": "Users were found in group 'Sales Support'.",
    "__VAR_CHANGED__": "UserEmail,Rep,CaseNumber,SalesSupportMemberBegan,QuerySalesResult,SalesSupportMemberBegan,UsersFoundMessage,SubjectInsideIf,return,APPLICATION,return,APPLICATION,EmailSalesUsersFound"
}

APEX Call

CaseDetails cd1 = CaseDetails.parse(res.getBody());

2 Answers 2

1

Not entirely sure how the specific error you're getting comes to be, but I think that you have narrowed it down to the problematic element(s).

Identifiers (class names, method names, variable names) in Apex cannot start with a number. So any JSON attribute that starts with a number is going to be problematic.

If you can modify the JSON at the source (or in Apex, before you deserialize) to change

"QuerySalesResult": {
        "1": {
            "USR_USERNAME": "abcdefg"
        },
        "2": {
            "USR_USERNAME": "JessicaDrew"
        }
    },

to use a list/array instead

"QuerySalesResult": [
        {
            "USR_USERNAME": "abcdefg"
        },
        {
            "USR_USERNAME": "JessicaDrew"
        }
    ],

Then you'd be able to get a new deserialization class from the JSON2Apex tool that should work (for typed deserialization). If you don't need the QuerySalesResult information, you may be able to get away with simply removing public QuerySalesResult QuerySalesResult; from the deserialization class (which may cause the deserialization to skip trying to process it).

Otherwise, this is going to be something that you need to use JSON.deserializeUntyped() on (with all of the extra typecasting it entails).

2
  • Confirmed that simply removing public QuerySalesResult QuerySalesResult; does work. Though in doing so I learned that the original code isn't giving me any error (when modified to be able to be executed via anonymous apex). "first" and "second" simply don't get populated.
    – Derek F
    Oct 7, 2022 at 14:03
  • Thanks for you assistance! Is there anything special you had to do when you removed the QuerySalesResult variable? I don't need this information but I was under impression that I needed to map all of the response variables in the deserialization class. I commented out public QuerySalesResult QuerySalesResult; and the corresponding inner classes, but I still receive the "illegal value for primitive" error when I try to run the CaseDetails.parse(res.getBody()) command from an Anon Apex window.
    – nigeldendy
    Oct 7, 2022 at 15:51
1

You can make the QuerySalesResult member a Map.

Change:

public QuerySalesResult QuerySalesResult;

To:

public Map<String, UserDetail> QuerySalesResult;

Where UserDetail is:

public class UserDetail {
  public String USR_USERNAME;
}

This would allow you to do something like:

CaseDetails detail = CaseDetails.parse(jsonString);
for(UserDetail theUser: detail.QuerySalesResult.values()) {
  System.debug(theUser.USR_USERNAME);
}

I do agree that it'd be better to change your JSON, as Derek suggested, but if you really don't want to change your JSON, this is a suitable alternative.

1
  • I ended up getting the developer to change the JSON, thanks so much for your help!
    – nigeldendy
    Oct 15, 2022 at 13:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .