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
shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com 

Insert Account+Contact using Rest Apex

Hi Everyone,
     I am taking baby steps towards REST apex  functionalliy and have developed some code (which fortunately woorks). Now I need your help in enhancing its operations. Please find the summary given below:

Using a json (given below) I managed to create an account record in salesforce:

{
   "acct":
   {
     "Name":"Jag"
   }

}

Apex Code:
@RestResource(urlMapping='/v1/accounts/*')
global with sharing class MyRestResource {
@HttpPost
    global static AccountWrapper doGet(Account acct) {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        AccountWrapper response= new Accountwrapper();
        try{
         insert acct;
         response.message='Mogamgo Khush Hua :)';
        }
        catch(Exception exc){
         response.message='Mogamgo Dukhi Hua :('; 
        }
       return response;
    }
      global class AccountWrapper {
      public Account acct;
      public String status;
      public String message;
      public AccountWrapper(){}

    }

}
Now I want to modify my apex code so that it can take a json with 1 account and 3 contacts in it and insert them in salesforce. Sample json is given below , can anyone please guide me with the modifications that are to be done in my apex class?


{
  "rqst":{
        "acct":{
         "Name":"Jag"
        }
        "conlist":{
         [
           {"FirstName":"Test","LastName":"Test"},
           {"FirstName":"Test1","LastName":"Test1"}
         ]
        }
  }

}
 
Best Answer chosen by shrey.tyagi88@tcs.com
Daniel BallingerDaniel Ballinger
Hi Shrey,

If you look at the generated Rqst class you will notice that it has a member that is a collection of Contact first and last names.
 
public List<Conlist> conlist;

You can populate this, alone with the Account name in a single request.

Regards,
Daniel

All Answers

Daniel BallingerDaniel Ballinger
You can create an object that has the same structure as the inbound JSON.

This process can be simplified by using a JSON2Apex tool (http://json2apex.herokuapp.com/). Note that I first needed to get your JSON to be valid (https://jsonformatter.curiousconcept.com/).
{
   "rqst":{
      "acct":{
         "Name":"Jag"
      },
      "conlist":[
         {
            "FirstName":"Test",
            "LastName":"Test"
         },
         {
            "FirstName":"Test1",
            "LastName":"Test1"
         }
      ]
   }
}
 
//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//

public class JSON2Apex {

    public Rqst rqst;

    public class Rqst {
        public Acct acct;
        public List<Conlist> conlist;
    }

    public class Acct {
        public String Name;
    }

    public class Conlist {
        public String FirstName;
        public String LastName;
    }

    
    public static JSON2Apex parse(String json) {
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
    }
}

Now change the signature of your method to take an argument of type Rqst.
 
global static AccountWrapper doGet(JSON2Apex.Rqst acct) {

You might like to tidy this up be moving the inner classes from the JSON2Apex class into your MyRestResource class.
 

The alternative is to parse the raw JSON request from the RestRequest.requestBody
shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com
Hi Daniel,
    Thanks a lot for responidng to my post . I have learnt a lot from your response. I have one question though, how would I add the contact list as well as account in  with one method (AccountWrapper).

global static AccountWrapper doGet(JSON2Apex.Rqst acct) -- This takes only account varaible as input

Do I need to write another method like this:

global static ContactWrapper doGet(JSON2Apex.Rqst Conlist)

Is there a way to write only 1 method and do all the @POST actions in 1? Would this line given below be wrong?

global static AccountWrapper doGet(JSON2Apex.Rqst rqst)

Regards
Shrey Tyagi

 
Daniel BallingerDaniel Ballinger
Hi Shrey,

If you look at the generated Rqst class you will notice that it has a member that is a collection of Contact first and last names.
 
public List<Conlist> conlist;

You can populate this, alone with the Account name in a single request.

Regards,
Daniel
This was selected as the best answer
Akash Singh 77Akash Singh 77
I have the same problem, I tried JSON Formatter by Curious Concept But It Does Not Work For Me, This (https://goonlinetools.com/json-formatter/) Work for me but i can't fix that error