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
sonali  vermasonali verma 

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.
 
@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"

         }

      ]
      }
             
}

 
Sujit Anil NirkheSujit Anil Nirkhe
@RestResource(urlMapping='/BulkInsertaccountcontact/*')
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
 
sonali  vermasonali verma
Hi
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.
 
@RestResource(urlMapping='/BulkInsertaccountcontact/*')
global class Bulkinsert_accountcontact
{
   global class RequestBody 
   {
       List<Account> adList; 
       List<Contact> cList;
       
   }
   
     
   global class myWrapper
   {
      List<Account> aList;
      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>();
      
      
      try
      {
        for(Account acn:rb.adList)  
        {
          myAList.add(acn);
        }
       
        Insert myAList;
        
        response.aList=myAList;
                
        for(Contact con:rb.cList)
        {
          con.AccountID=rb.adList.ID;  //ERROR OCCURS HERE.
                                       //Initial term of field expression must be a concrete SObject
        }
        
        //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.aList=null;
         response.cList=null;
         response.status='ERROR';
         response.Message='could not create record'+ ex.getMessage();
      }
      
      return response;
   }
   
}

Thanks
sonali verma