You need to sign in to do that
Don't have an account?

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"}
]
}
}
}
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"}
]
}
}
}
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.
You can populate this, alone with the Account name in a single request.
Regards,
Daniel
All Answers
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/).
Now change the signature of your method to take an argument of type Rqst.
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
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
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.
You can populate this, alone with the Account name in a single request.
Regards,
Daniel