Working on the Trailhead Apex Super Badge....

There is a section that requires writing a rest callout that returns a chunk of JSON that you are to use to update Product2 records with.

Reduced Chunk of JSON:

[{"_id":"55d66226726b611100aaf741","replacement":false,"quantity":5,"name":"Generator 1000 kW","maintenanceperiod":365,"lifespan":120,"cost":5000,"sku":"100003"},
{"_id":"55d66226726b611100aaf742","replacement":true,"quantity":183,"name":"Cooling Fan","maintenanceperiod":0,"lifespan":0,"cost":300,"sku":"100004"},
more records...]

A couple of issues... First trying to deserialize into a wrapper class with a field named _id will not compile. It gives the following error:

no viable alternative at character '_'

So I did a no-no just to get going and replaced all _id with id to get it to compile and deserialize.

Second issue (after reforming the JSON structure) is that none of the JSON records have a name. I can deserialize them into a list of generic Object's, but I then am unable to cast them to anything. It gives the following error:

Invalid conversion from runtime type

So again I cheated the JSON and added a jsonString = '{"recordsList":' + jsonString + '}'; so that I could generate a JSON2Apex wrapper.

I feel like either I am missing some basic concepts or whoever came up with this didn't actually have anybody come in and try and complete it behind them???

  • Shouldn't they be OpportunityLineItem records anyway? Doesn't seem like Product2 should have any field pertaining to Quantity.
    – Adrian Larson
    Commented Jun 29, 2016 at 17:53
  • Edit*** The Product2 in this scenario holds an 'on hand quantity'. Salesforce Trailhead designed it (was part of the managed package to do this badge). It does have a child record that is the actual product (Work_Part__c) that gets related to the Product2 Commented Jun 29, 2016 at 19:26

2 Answers 2


I'm a conflicted in answering this. If I give you too much detail it will make it too easy for others to complete the module (I.e. I shouldn't just dump my entire WarehouseCalloutService implementation). If I give you too little it won't be sufficient to answer your question.

My suggestions that hopefully find the right balance:

  1. Use https://json2apex.herokuapp.com/ to get the definition of the Equipment that you can parse from the JSON.
  2. Examine what is generated by the API and figure out what you do and don't need. You might not need all the fields the JSON returns. Cough Maybe even ones that are a bit problematic Cough.
  3. There are some really good examples of how to parse a JSON string and Deserialize it into objects in the docs.

From my pass through the badge I didn't encounter the problem you are having with the name. Maybe double check your code that is altering the _id values? I didn't need to do any post processing on the JSON response. It went fine as direct input into the JSON parsing.

  • Thanks, I had already completed it. Just was looking for some understanding of why I was getting the error with the _ (which I discovered later on was what you hinted at above). I also just tweaked the incoming response body a bit instead of writing out a parser as mentioned in #3. Good answer though. Thank you! Commented Jun 30, 2016 at 12:01
  • Hey this is great conversation for me, I have written call out Logic that is working as expected, and i'm struggling to write the test logic for the same, please help. Here is the stack exchange post: link , @Daniel
    – RaJu
    Commented Sep 19, 2016 at 6:28

try this link to auto generate the json parsing classes you'll need:


  • 1
    yeah that doesn't solve the problem, that is what I use to create wrappers from JSON. Commented Jun 29, 2016 at 17:28
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – Adrian Larson
    Commented Jun 29, 2016 at 17:49
  • @AdrianLarson That's not a link of the usual "link-only" answer type, This one is like being given a link to WSDL2APEX. Its a link to visit that would hopefully generate the classes needed to solve the issue. Apparently it didn't solve them for the OP.
    – crmprogdev
    Commented Jun 29, 2016 at 20:08
  • But it is still just a link and should be a comment and not an answer me thinks
    – Eric
    Commented Jun 30, 2016 at 5:13

You must log in to answer this question.

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