0

I have created REST service which is fetching prospect data from pardot and now I want to store it into Salesforce's custom object. I have some fields inthis object which I need to map from the response returned.

Response:

"result":{
 "prospect":[   {"id":1704281,"campaign_id":969,"user_id":null,"salutation":"Ms","first_name":"t
 est","last_name":"Akin"}]}

In the REST class I am de-serializing the data as below but getting "Attempt to de-refrence null"

 if (res.getStatusCode() == 200) {
 Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
 List<Object> prospects = (List<Object>) results.get('prospect');
  for (Object prospectob: prospects) {  // Getting Error - Attempt to de-ref null
     System.debug('*******'+prospectob);
   }
}

I found that the below line is the cuprit, it is not fetching the data from the response returned by the service.

 List<Object> prospects = (List<Object>) results.get('prospect');

Can somebody please point out what's wrong in here.

0

The top-level object is result:

Map<String, Object> result = 
  (Map<String, Object>)JSON.deserializeUntyped(res.getBody()).get('result');
Object[] prospects = (Object[])result.get('prospect');

It's usually worth trying JSON2Apex so you can get the correct object names.

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.