+ Start a Discussion
Poorna DeveloperPoorna Developer 

Update a record in future method with JSON data

Hi,
From process builder getting record id and pass that in to apex.

User-added image
Apex class to update the record:

public class BacktoQuantum
{
     @InvocableMethod(label='getdata' description='Get data from saltedge')
  public static void toquantum(List<String> ids)
    {
       BacktoQuantum.fromdata(ids);
       //System.debug('callback');
      
  }
   @future(callout=true)
    public static void fromdata(List<String> ids)
   {
         Http http = new Http();
            HttpRequest request = new HttpRequest();
            request.setEndpoint('http://ussd.apmuyea.software.com/CustomerAdd_SaltEdge_CallBack.aspx');
            request.setMethod('GET');
            //request.setHeader('Accept','application/json');
            request.setHeader('Content-Type', 'text/html; charset=utf-8');
            request.setBody('{"data": {Saltedge Data }}');
       
            HttpResponse responseg = http.send(request);  
            System.debug(responseg.getBody());
        String ac = responseg.getBody(); //Return JSON data
      List<Loan_Application__c> app =  [Select Amount__c,Rate__c,Term_Months__c from Loan_Application__c where Id=:ids];
       if(!app.isEmpty())
       {
                // app[0];
// To update the JSON data in the first record in the list
       }
        //Loan_Application__c app =  new Loan_Application__c();
       //app.SaltedgeData__c = ac;
       //System.debug('sample json' +app);
         } 
}

The above apex code return a JSON data, I need to store that JSON data in salesforce field called Saltedge__c.
I'm stuck in the update.

can anyone help to me update the record?

Thanks.
sreenath reddy 21sreenath reddy 21
Hi poorna 
You convert JSON into string and store the result in salesforce field
app.Saltedge__c = (String)JSON.deserializeUntyped(ac);
ShivankurShivankur (Salesforce Developers) 
Hi Poorna,

You can use the JSONParser class methods to parse JSON-encoded content. These methods enable you to parse a JSON-formatted response that's returned from a call to an external service, such as a web service callout.

Following link provides you examples for each:
1. Parsing a JSON Response from a Web Service Callout
2. Parse a JSON String and Deserialize It into Objects

Reference:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_json_jsonparser.htm

You can use as per your use case.

Hope above information helps, Please mark as Best Answer so that it can help others in the future.

Thanks.