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
pranavshahpranavshah 

Apex Web Service- JSON Parsing for inserting multiple Records


Hi All,

I need to parse below code in JSON format to insert multiple records at a time... i have done for single record to be inserted...For inserting multiple records through web service , how it will be done



@RestResource(urlMapping='/showAccountDetails/*')
global with sharing class NewCustomerCreation
{
 @HttpPost

 global static string doPost(String name)
 {
  RestRequest req = RestContext.request;
  RestResponse res = RestContext.response;
  res.addHeader('Content-Type', 'application/json');
  String jsonResponse = '';
  String accountName = req.params.get('account_name');
  Integer PInstallations = Integer.ValueOf(req.params.get('Potential_Installations__c')); 
  String Muncipaly = req.params.get('Municipaly__c');
  String CustSegment = req.params.get('Customer_Segment_Lookup__c'); 
  String CustHousingType = req.params.get('Housing_Type_Lookup__c'); 
  String CustHousingOwnership = req.params.get('Housing_Ownership_Lookup__c');  

  //Inserting records into Account
  
  Account acc= new Account();
  acc.name=accountName;
  acc.Potential_Installations__c= PInstallations;
  acc.Municipaly__c='Muncipaly';
  acc.Customer_Segment_Lookup__c ='CustSegment';
  acc.Housing_Type_Lookup__c ='CustHousingType';
  acc.Housing_Ownership_Lookup__c = 'CustHousingOwnership';
  insert acc;
  return acc.id;
      
}


below is the required format


{
"Account" :[{
 "cableunit_name":"",
              "cableunit_number":"",
              "potential_installation":"",
              "municipality":"",
              "postcode":"",
              "customer_segment":"",
              "Housing_type":"",
              "building_type":"",
              "housing_ownership":""
}
      
    },{
     "cableunit_name":"",
              "cableunit_number":"",
              "potential_installation":"",
              "municipality":"",
              "postcode":"",
              "customer_segment":"",
              "Housing_type":"",
              "building_type":"",
              "housing_ownership":""
}
    },{
    "attributes" :  "cableunit_name":"",
              "cableunit_number":"",
              "potential_installation":"",
              "municipality":"",
              "postcode":"",
              "customer_segment":"",
              "Housing_type":"",
              "building_type":"",
              "housing_ownership":""
}
    },{
    "cableunit_name":"",
              "cableunit_number":"",
              "potential_installation":"",
              "municipality":"",
              "postcode":"",
              "customer_segment":"",
              "Housing_type":"",
              "building_type":"",
              "housing_ownership":""
}
    }]
}


please help

Thanks & Regards
Pranav Shah
Best Answer chosen by pranavshah
Shruti SShruti S
So can you confirm your JSON - 
{
    "Account": [
        {
            "cableunit_name": "",
            "cableunit_number": "",
            "potential_installation": "",
            "municipality": "",
            "postcode": "",
            "customer_segment": "",
            "Housing_type": "",
            "building_type": "",
            "housing_ownership": ""
        },
        {
            "cableunit_name": "",
            "cableunit_number": "",
            "potential_installation": "",
            "municipality": "",
            "postcode": "",
            "customer_segment": "",
            "Housing_type": "",
            "building_type": "",
            "housing_ownership": ""
        },
        {
            "cableunit_name": "",
            "cableunit_number": "",
            "potential_installation": "",
            "municipality": "",
            "postcode": "",
            "customer_segment": "",
            "Housing_type": "",
            "building_type": "",
            "housing_ownership": ""
        },
        {
            "cableunit_name": "",
            "cableunit_number": "",
            "potential_installation": "",
            "municipality": "",
            "postcode": "",
            "customer_segment": "",
            "Housing_type": "",
            "building_type": "",
            "housing_ownership": ""
        }
    ]
}
will be like as shown above ?

All Answers

Shruti SShruti S
Just to be clear - so you will be receiving the below mentioned JSON from an external application into Salesforce.

Is that correct ?
Shruti SShruti S
So can you confirm your JSON - 
{
    "Account": [
        {
            "cableunit_name": "",
            "cableunit_number": "",
            "potential_installation": "",
            "municipality": "",
            "postcode": "",
            "customer_segment": "",
            "Housing_type": "",
            "building_type": "",
            "housing_ownership": ""
        },
        {
            "cableunit_name": "",
            "cableunit_number": "",
            "potential_installation": "",
            "municipality": "",
            "postcode": "",
            "customer_segment": "",
            "Housing_type": "",
            "building_type": "",
            "housing_ownership": ""
        },
        {
            "cableunit_name": "",
            "cableunit_number": "",
            "potential_installation": "",
            "municipality": "",
            "postcode": "",
            "customer_segment": "",
            "Housing_type": "",
            "building_type": "",
            "housing_ownership": ""
        },
        {
            "cableunit_name": "",
            "cableunit_number": "",
            "potential_installation": "",
            "municipality": "",
            "postcode": "",
            "customer_segment": "",
            "Housing_type": "",
            "building_type": "",
            "housing_ownership": ""
        }
    ]
}
will be like as shown above ?
This was selected as the best answer