function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
NSK000NSK000 

How to get data into salesforce from JSON?

I need to get the data from JSON into salesforce object(custom object).  
Following is the JSON - 
{"EMPL_TYPE":" ","GRADE":"19","STD_HOURS":40.00,"EMPLID":"JCSD"}

I have a custom object in salesforce and I need to get the above values into their respective fields when i click a button on the custom object.
My code is as follows-

public class Empdetails {
public String EMPL_TYPE;
public String GRADE;
public Double STD_HOURS;
public String EMPLID;
public static Empdetails parse(String EMPLID) {
        Http httpProtocol = new Http();
        HttpRequest request = new HttpRequest(); 
        request.setEndpoint('xyz'+EMPLID);
        request.setMethod('GET'); 
        HttpResponse response = httpProtocol.send(request);
        system.debug(response.getBody());
        String jsonString = response.getBody();
        // Parse JSON response to get all the Employee record details
        JSONParser parser = JSON.createParser(response.getBody());
        return (Empdetails) System.JSON.deserialize(EMPLID, Empdetails.class);
    }
}

Actually I'm new to this concept. Am I doing correct ? Please suggest me some inputs.
Thank you 
Patcs_1Patcs_1
Hi

You have to create a wrapper class and then declare the variable in that class, use the wrapper class to deserialize the JSON. I have written the sample code.

Public class Empdetails {
public static EmpDetaiswrapper parse(String EMPLID) {
        Http httpProtocol = new Http();
        HttpRequest request = new HttpRequest(); 
        request.setEndpoint('xyz'+EMPLID);
        request.setMethod('GET'); 
        HttpResponse response = httpProtocol.send(request);
        system.debug(response.getBody());
        String jsonString = response.getBody();
        // Parse JSON response to get all the Employee record details
        JSONParser parser = JSON.createParser(response.getBody());
        return (EmpDetaiswrapper) System.JSON.deserialize(request.requestBody.toString(), EmpDetaiswrapper.class);
    }
public class EmpDetaiswrapper{
public String EMPL_TYPE{get;set;}
public String GRADE{get;set;}
public Double STD_HOURS{get;set;}
public String EMPLID{get;set;}
}

}

Hope this help!

Thanks
 
NSK000NSK000
Thank you for your response. 

return (EmpDetaiswrapper) System.JSON.deserialize(request.requestBody.toString(), EmpDetaiswrapper.class); I'm getting an error here. Will you please look into it. 
Do you have any idea how to call this controller from a button ?
Patcs_1Patcs_1
Hi

Can you please let me know what error you are getting.

Thanks