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

multiple accounts with associated contacts
Hello
I have created a rest resource which will insert a single account record and can insert multiple contact records.
Its working fine a but my requirement is to to create multiple account records in JSON body and each account can more than 1 contact records.
I am using workbench to test the rest resource.
I think I need to make a change as follows.
global class RequestBody
{
List<Account> a;
List<Contact> cList;
}
global class myWrapper
{
List<Account> a;
List<Contact> cList;
public string status;
public string message;
}
Please let me know how I can proceed further.
divya
I have created a rest resource which will insert a single account record and can insert multiple contact records.
@RestResource(urlMapping='/BulkInsertaccountcontact/*') global class Bulkinsert_accountcontact { global class RequestBody { Account a; //create single account List<Contact> cList; } global class myWrapper { 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 try { Insert rb.a; response.a=rb.a; 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; } }
Its working fine a but my requirement is to to create multiple account records in JSON body and each account can more than 1 contact records.
I am using workbench to test the rest resource.
I think I need to make a change as follows.
global class RequestBody
{
List<Account> a;
List<Contact> cList;
}
global class myWrapper
{
List<Account> a;
List<Contact> cList;
public string status;
public string message;
}
Please let me know how I can proceed further.
divya
Try changing RequestBody so that it has a collection of custom object records. E.g.