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

rest resource concept
Hi
I have rest resource thru which I want to create multiple account and for each account I want to create multiple contacts.
Its working for single accout and multiple contacts.
Pls help me in understanding how to create multiple accounts and how to frame the json query for multiple account.
I have rest resource thru which I want to create multiple account and for each account I want to create multiple contacts.
Its working for single accout and multiple contacts.
Pls help me in understanding how to create multiple accounts and how to frame the json query for multiple account.
@RestResource(urlMapping='/BulkInsertaccountcontact/*') global class Bulkinsert_accountcontact { global class RequestBody { Account a; //create single account List<Contact> cList; } global class myWrapper { List<Account> a; List<Contact> cList; public string status; public string message; } @HttpPost global static myWrapper doPost(RequestBody rb) { RestRequest req=RestContext.request; RestResponse res=RestContext.response; myWrapper response=new myWrapper(); //this is object of wrapper class List<Account> myList=new List<Account>(); try { myList.add(rb.a); Insert myList; response.a=myList; for(Contact con:rb.cList) { con.AccountID=rb.a.ID; } //we need to insert contact List also Insert rb.cList; response.cList=rb.cList; response.status='Success'; response.message='Created Successfully'; } catch(exception ex) { response.a=null; response.cList=null; response.status='ERROR'; response.Message='could not create record'+ ex.getMessage(); } return response; } } workbench: ========== { "rb":{ "a":{ "Name":"LG1", "SLAExpirationDate__c":"2016-4-21", "Active__c":"Yes", "SLA__c":"Platinum", "SLASerialNumber__c":"200" }, "cList":[ { "FirstName":"K1", "LastName":"K2" }, { "FirstName":"K3", "LastName":"K4" } ] } }
global class Bulkinsert_accountcontact
{
global class RequestBody
{
List<AccountDetails> adList;
}
global class AccountDetails
{
Account a; //create single account
List<Contact> cList;
}
global class myWrapper
{
List<Account> a;
List<Contact> cList;
public string status;
public string message;
}
@HttpPost
global static myWrapper doPost(RequestBody rb)
{
RestRequest req=RestContext.request;
RestResponse res=RestContext.response;
myWrapper response=new myWrapper(); //this is object of wrapper class
List<Account> myAList=new List<Account>();
List<Contact> myCList=new List<Contact>();
try
{
for(AccountDetails ad : rb.adList)
myAList.add(ad.a)
for(Contact con:ad.cList)
{
con.AccountID=rb.a.ID;
myCList.add(con);
}
Insert myAList;
Insert myCList;
//we need to insert contact List also
}
catch(exception ex)
{
response.a=null;
response.cList=null;
response.status='ERROR';
response.Message='could not create record'+ ex.getMessage();
}
return response;
}
}
workbench:
==========
{
"rb":{
adList:[ "a":{
"Name":"LG1",
"SLAExpirationDate__c":"2016-4-21",
"Active__c":"Yes",
"SLA__c":"Platinum",
"SLASerialNumber__c":"200"
},
"cList":[
{
"FirstName":"K1",
"LastName":"K2"
},
{
"FirstName":"K3",
"LastName":"K4"
}
]
}]
}
This is some rough code... but surely it will give idea
I have worked on the code , the account List is working but I am having an issue with the contact object.
Cud u pls go through and let me know.
Thanks
sonali verma