You need to sign in to do that
Don't have an account?
Tom Simmons
Help with JSON deserialized - Creating Account record
Need help. Using Class below, I have deserialized JSON response however I`m not able to figure out how should I create an account in Salesforce using this deserialized data. Can someone please help?
Deserialized JSON:
11:11:02:208 USER_DEBUG [69]|DEBUG|deserializeResults====: candidateParser:[rut=cls_rut:[candidates=cls_candidates:[candidate=(cls_candidate:[candidate_code=AA12, candidate_id=3, description=Steven S.], cls_candidate:[candidate_code= AA13, candidate_id=4, description= Brad Hess], cls_candidate:[candidate_code= AA14, candidate_id=5, description=Brad Jones], cls_candidate:[candidate_code= AA14, candidate_id=6, description=Sample candidate], cls_candidate:[candidate_code= AA16, candidate_id=7, description=ross. k],...)]]]
I`m trying below but I keep getting null response on account name. Please help -
public class candidateParser{ public cls_rut rut; public class cls_rut { public cls_candidates candidates; } public class cls_ candidates { public cls_candidate [] candidate; } public class cls_candidate { public String candidate_id; //3 public String candidate_code; //AA12 public String description; // Steven S. } public static candidateParser parse(String json){ return (candidateParser) System.JSON.deserialize(json, candidateParser.class); }}
Deserialized JSON:
11:11:02:208 USER_DEBUG [69]|DEBUG|deserializeResults====: candidateParser:[rut=cls_rut:[candidates=cls_candidates:[candidate=(cls_candidate:[candidate_code=AA12, candidate_id=3, description=Steven S.], cls_candidate:[candidate_code= AA13, candidate_id=4, description= Brad Hess], cls_candidate:[candidate_code= AA14, candidate_id=5, description=Brad Jones], cls_candidate:[candidate_code= AA14, candidate_id=6, description=Sample candidate], cls_candidate:[candidate_code= AA16, candidate_id=7, description=ross. k],...)]]]
I`m trying below but I keep getting null response on account name. Please help -
candidateParser.cls_candidate deserializeResults2 = (candidateParser.cls_candidate)JSON.deserialize(replaceJson, candidateParser.cls_candidate.class); System.debug('deserializeResults2====: '+deserializeResults2); Account Acc = New Account (); Acc.Name = deserializeResults2.candidate_code; Insert Acc;
If you have already deserialized response without any errors then I would imagine your deserialized structure would look like the below(based on your class structure)
candidateParser.cls_candidate deserializeResults2 = (candidateParser.cls_candidate)JSON.deserialize(replaceJson, candidateParser.cls_candidate.class);
List<cls_ candidates> candidateList = deserializeResults2.candidates;
List<Account> accountList = new List<Account>();
for(cls_ candidates candidateSObj : candidateList)
{
Account acc = New Account ();
acc.Name = candidateSObj.candidate_code;
accountList.add(acc);
}
insert accountList;
I have written this on text editor. So might/might not need to make changes to compile.
Try this and let me know if its fine.
Kind regards,
CloudSavvyProg
All Answers
If you have already deserialized response without any errors then I would imagine your deserialized structure would look like the below(based on your class structure)
candidateParser.cls_candidate deserializeResults2 = (candidateParser.cls_candidate)JSON.deserialize(replaceJson, candidateParser.cls_candidate.class);
List<cls_ candidates> candidateList = deserializeResults2.candidates;
List<Account> accountList = new List<Account>();
for(cls_ candidates candidateSObj : candidateList)
{
Account acc = New Account ();
acc.Name = candidateSObj.candidate_code;
accountList.add(acc);
}
insert accountList;
I have written this on text editor. So might/might not need to make changes to compile.
Try this and let me know if its fine.
Kind regards,
CloudSavvyProg
@cloudSavvyProg - As per you suggestion I tried this however I noticed that it won`t work since statement below itself if retuning null.
17:29:20:068 USER_DEBUG [56]|DEBUG|deserializeResults2====: cls_candidate:[candidate_code=null, candidate_id=null, description=null]
Could you please help where I`m going wrong? is above statement not correct?
If I use below statement, it do get a debug log with values. How can I take this forward?