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
Tom SimmonsTom 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?
 
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;

 
Best Answer chosen by Tom Simmons
cloudSavvyProgcloudSavvyProg
Hi,

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

Vivek DVivek D
Try direct way then probably it will make sence
acc.Name = rut.candidates.candidate[0].candidate_code;
// You need to loop on candidate if there are multiple record and handle exception if there is no record

 
cloudSavvyProgcloudSavvyProg
Hi,

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
This was selected as the best answer
Tom SimmonsTom Simmons
@Vivek - Thank you, I will try this approach.

@cloudSavvyProg - As per you suggestion I tried this however I noticed that it won`t work since statement below itself if retuning null.
 
candidateParser.cls_candidate deserializeResults2 = (candidateParser.cls_candidate)JSON.deserialize(replaceJson, candidateParser.cls_candidate.class);
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?

 
candidateParser deserializeResults2 = (candidateParser)JSON.deserialize(replaceJson, candidateParser.class);


 
Tom SimmonsTom Simmons
Guys, I was able to get this fixed. Thank you both for your help.