0

I have a JSON response object that contains an array, the array only ever contains one value. I want to map the result of to a field in a salesforce object.

I have a response class with an Array called State :

public class JobResponse{     
    public string uuid {get; set;}
    public datetime startedOn {get; set;}
    public string description {get; set;}
    public datetime completedOn {get; set;}
    public string jobId {get; set;}
    public JobLead jobLead {get; set;}
    public datetime scheduledOn {get; set;}
    public datetime createdOn {get; set;}
    public list <State> state {get; set;}
    public Signature signature {get; set;}
    public customField customFields {get; set;}
    public Location location {get; set;}
}
public class State {
    public string state{get; set;}
}

but i'm not sure how to map the result of the array to a field:

        JobResponse jobresp =  (JobResponse)JSON.deserialize(res.getBody(), JobResponse.class);

     sc.FieldAwareId__c = jobresp.uuid;
     sc.FA_Job_ID__c = jobresp.jobId;
     sc.State__c = jobresp.State.state;

How would I map the jobresp.State.state response to a the sc.State__c field?

My example gives me an error: Initial term of field expression must be a concrete SObject: List

1
  • 1
    you have to retrieve the 1st element from the list and then read the property.. sc.State__c = jobresp.State[0].state; Sep 20, 2016 at 12:38

1 Answer 1

2

If the response contains a list then you'll need to do something like:

List<JobResponse> jobresplist =  (List<JobResponse>)JSON.deserialize(res.getBody(), List<JobResponse>.class);

JobResponse jobresp = jobresplist[0];
sc.FieldAwareId__c = jobresp.uuid;
sc.FA_Job_ID__c = jobresp.jobId;
sc.State__c = jobresp.State.state;
2
  • If the response doesn't tell you the amount of objects in the Array is there some way to count the objects to iterate through the array? Sep 20, 2016 at 12:45
  • Yea definitely, you could use jobresplist.size() for example. Sep 20, 2016 at 12:46

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.