1

all, need help with below class. I want to insert multiple leads but currently this class accepts only 1 lead. How can I send multiple JSON list to this class? Does Rest API support multiple inserts? How can I optimize this code??

    @RestResource(urlMapping='/v1/createleads/*')

global with sharing  class createleads_V1 {

    @HttpPost
global static LeadInWrapper doPost(LeadInWrapper[] leads) {

            RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        LeadInWrapper response = new LeadInWrapper ();


        List<Lead> leadsToInsert = new List<Lead>();
        for(LeadInWrapper leadWrapper : leads){
            Lead l = new Lead ();
            l.FirstName= leadWrapper.FirstName;
            l.LastName= leadWrapper.LastName;
            l.Company_Name__c = leadWrapper.Company;                                 
            l.Street = leadWrapper.Street;
            l.City= leadWrapper.City;
            l.State= leadWrapper.State;
            l.Postalcode= leadWrapper.Postalcode;
            l.Country= leadWrapper.Country;
            l.LeadSource = leadWrapper.LeadSource; 
            l.OwnerId = '0055B000000FGkw';
            l.Email = leadWrapper.Email;
            l.Phone = leadWrapper.Phone ;

            leadsToInsert.add(l);
        }

        insert leadsToInsert;

            response.status = 'Success';
        response.StatusCode = 201;

        response.message = 'successful.';
        return response;

}    
    global class LeadInWrapper {
        public String status;
        public String message;
        public integer StatusCode;
         public String FirstName;
        public String LastName;
        public String Company;
        public String Street;
        public String City;
        public String State;
        public String Postalcode;
        public String Country;
        public String Email;
        public String Phone;
        public String LeadSource;


    }


}

Code works fine with 1 lead but When I try to insert two Leads with JSON below, it fails with this error:

[ { "message": "Extra JSON in request found after deserialization at: [line:15, column:3]", "errorCode": "JSON_PARSER_ERROR" } ]

JSON i`m trying to pass:

    {
"FirstName" : "Test name 1",
"LastName" : "Test name 2",
"Company" : "test company",
"Street" : "Abc street",
"City" : "somecity",
"State" : "somestate",
"Postalcode" : "1111",
"Country" : "United States",
"Email" : "name.test@test.com"
},
{
"FirstName" : "Test name 1",
"LastName" : "Test name 2",
"Company" : "test company",
"Street" : "Abc street",
"City" : "somecity",
"State" : "somestate",
"Postalcode" : "1111",
"Country" : "United States",
"Email" : "name.test@test.com"
},
1

You can use a wrapper class and an array of this new class as parameter of your doPost method as below:

Wrapper

global class LeadInWrapper {
        public String FirstName;
        public String LastName;
        public String Company;
        public String Street;
        public String City;
        public String State;
        public String Postalcode;
        public String Country;
        public String Email;
        public String Phone;
        public String LeadSource;
    }

Method

global static LeadWrapper doPost(LeadInWrapper[] leads) {
        List<Lead> leadsToInsert = new List<Lead>();
        for(LeadInWrapper leadWrapper : leads){
            Lead l = new Lead ();
            l.FirstName= leadWrapper.FirstName;
            l.LastName= leadWrapper.LastName;
            l.Company_Name__c = leadWrapper.Company;                                 
            l.Street = leadWrapper.Street;
            l.City= leadWrapper.City;
            l.State= leadWrapper.State;
            l.Postalcode= leadWrapper.Postalcode;
            l.Country= leadWrapper.Country;
            l.LeadSource = leadWrapper.LeadSource; 
            l.OwnerId = '0055B000000FGkw';
            l.Email = leadWrapper.Email;
            l.Phone = leadWrapper.Phone ;

            leadsToInsert.add(l);
        }

        insert leadsToInsert;

Json to pass

    {
   "leads":[
      {
         "FirstName":"Test name 1",
         "LastName":"Test name 2",
         "Company":"test company",
         "Street":"Abc street",
         "City":"somecity",
         "State":"somestate",
         "Postalcode":"1111",
         "Country":"United States",
         "Email":"name.test@test.com"
      },
      {
         "FirstName":"Test name 1",
         "LastName":"Test name 2",
         "Company":"test company",
         "Street":"Abc street",
         "City":"somecity",
         "State":"somestate",
         "Postalcode":"1111",
         "Country":"United States",
         "Email":"name.test@test.com"
      }
   ]
}
  • Thank you Martin, I update the code but still getting below error (see updated code above). Can you point out what is not correct? Error: [ { "message": "Unexpected parameter encountered during deserialization: FirstName at [line:2, column:16]", "errorCode": "JSON_PARSER_ERROR" } ] – Wendy Feb 15 '17 at 12:46
  • I updated my answer with the json to pass to your method. – Martin Lezer Feb 15 '17 at 12:52
  • You're welcome. Glad I could help ! – Martin Lezer Feb 15 '17 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.