0

I am using the following class to deserialise a JSON object:

 //Method to deserialize the response body
    public static responseResource deserialize(httpResponse res){
      return (responseResource)JSON.deserialize(res.getBody(),responseResource.class);
    }

I then want to access the returned deserialised object in a separate function and assign its elements to salesforce fields.

So, for example if one of the deserialised fields is 'Description', how do I access it in another class?

The question is about this part of the last class:

  string description = <the 'Description' field from the deserialise (res) object returned by 'responseResource Query' class I don't know how to describe in the script>

Let me use the following to explain further:

First, helper function:

public static responseResource APIQuery(string domain){
    httpResponse res = callout(domain);
    if ( res.getStatusCode() == 200 && string.isNotBlank(res.getBody())){
        return deserialize(res);
    }
    return null;
}

And the function to access the variable:

    @future (callout=true)
        public static void FutureCallBasicUpdater(string domain)
            {

string description = <the 'Description' field from the deserialise (res) object returned by 'responseResource APIQuery' class I don't know how to describe in the script>
            }
2
  • When you are calling deserialize method, just take the output of that method in an instance of responeResouce class, and you can pass it anywhere and use the variable description. Commented Jun 5, 2017 at 16:33
  • Thank you Nitish, could you please explain with an example? I am new to Salesforce and at the early stages of learning APEX. Commented Jun 5, 2017 at 16:47

1 Answer 1

1

You access like any normal variable. "Utility" in this example is the name of the class that contains APIQuery.

@future (callout=true)
public static void FutureCallBasicUpdater(string domain) {
    Utility.responseResource res = Utility.APIQuery(domain);
    string description = res.Description;
}

Since you're trying to access the data across classes, the class and the Description value must be public:

public class resourceResponse {
    public String Description;
    // more attributes here, as needed
}

You must log in to answer this question.

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