When implementing rest api methods in Salesforce, I often have to write code like this:

@HttpPost global static String CreateLead(
    String LastName, String Company, String Email, String Country,
    String State,String FirstName, String City, String Street, String Phone, String PostalCode)
            system.debug('###### Attempting to create new lead');
            Lead l = new Lead();
            l.LastName = LastName;
            l.Email = Email;
            l.Country = Country;
            l.State = State;
            l.FirstName = FirstName;
            l.City = City;
            l.Street = Street;
            l.Phone = Phone;
            l.PostalCode = PostalCode;
            l.Company = Company;

            system.debug('######Attempting to insert lead');
            insert l;

            return l.Id;

This is a little inefficient in that I have to specify each parameter. And in some cases I have to pass more parameters than possible for certain objects (36 parameter max). Is there a way to do something like...

@HttpPost global static String CreateLead(
        Lead MyLead)
//do stuff 

And then from my external implementations just pass whatever JSON I want without throwing exceptions if parameters do not exactly match?


You can surely do that provided you have flexibility to marshall the incoming JSON

Lets take a simple JSON and see what happens

"MyLead": {
    "LastName": "TestAccount1",
    "Email": "msrivastav13@gmail.com",
    "Country": "India",
    "FirstName": "mohith"

Now let me draft the code for this as you wished and loved

 global class SampleRest{

 global static String CreateLead(Lead MyLead){
   insert MyLead;
   return MyLead.id;

Last bit is testing it up ,workbench REST Utility is God (Hate CURL )

enter image description here

  • The "MyLead:" was the answer! Thank you! – Darkenor Jul 15 '14 at 20:22

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.