2

i'm trying to convert the json response as sObject(Case). but i'm little confusion to map external sys fields with sobject fields

this is my JSON response :

{"result":[{"short_description":"Can't read email","sys_id":"9c573169c611228700193229fff72400","incident_state":"7","urgency":"1","impact":"1","active":"false"}]}

this is what i have done :

res = http.send(req);
System.debug(res.getBody());     
String jsonText =res.getBody();
Map<String, Object> cObjMap = (Map<String, Object>) JSON.deserializeUntyped(jsonText);
String cObjJson = JSON.serialize(cObjMap.get('Data'));
// Why again :
(Map<String, Object> cObjMapFurious = (Map<String, Object>) JSON.deserializeUntyped(cObjJson);
String cObjJsonDrunk = JSON.serialize(cObjMapFurious);
try
   {
     SObject customObject = (SObject)JSON.deserialize(cObjJsonDrunk, Sobject.class);
     System.debug(' Accomplished: '+customObject);
                }
                catch(Exception ex)
                {
                    System.debug(' @@@@@ Don\'t visible '+ex.getMessage());
                }

This is my wrapper class, how should I map with case?

global class Deserialization{

     public String short_description;
     public String sys_id;
     public String incident_state;
     public String urgency;
     public String impact;
     public String active;

     global Deserialization(String short_description1,String sys_id1,String incident_state1,String urgency1,String impact1,String active1){

          short_description = short_description1;
          sys_id = sys_id1;
          incident_state = incident_state1;
          urgency =  urgency1;
          impact =  impact1;
          active = active1;
     }

} 
  • 1
    You should create a wrapper class same as the response returned by the http request and from the wrapper data you can create Case no need to do repeated things you are doing here. – itzmukeshy7 Mar 28 '17 at 2:31
  • this is my wrapper class how should i map with case? – sushanth Mar 28 '17 at 2:46
  • uh ill take a stab at it, one second – EricSSH Mar 28 '17 at 2:49
3

You can delete you answer, but you need to build the object that you are expecting.. I use Json to Apex, I take that as a starting point, I don't build it exactly how it tells me, I have quirks I like to do. But this will work for you. I'll leave it in this format so you can execute anon it..

String deserlize = '{"result":[{"short_description":"Cant read email","sys_id":"9c573169c611228700193229fff72400","incident_state":"7","urgency":"1","impact":"1","active":"false"}]}';

ResponseResult theresult = (ResponseResult)JSON.deserialize(deserlize,  ResponseResult.class);

System.debug('result' + theresult);

public class ResponseResult
{
    public ResponseResult()
    {
        result = new List<ResponseData>();
    }

    public List<ResponseData> result;

}

public class ResponseData 
{
    public String short_description {get;set;}  //Can't read email
    public String sys_id  {get;set;}    //9c573169c611228700193229fff72400
    public String incident_state  {get;set;}    //7
    public String urgency  {get;set;}   //1
    public String impact  {get;set;}    //1
    public String active  {get;set;}    //false
}

This will return an object looking like this.. You will then need to loop through this object and build whatever else you need out of it

ResponseResult:[result=(ResponseData:[active=false, impact=1, incident_state=7, short_description=Cant read email, sys_id=9c573169c611228700193229fff72400, urgency=1])]

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.