Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer 1

up vote 4 down vote accepted

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

@RestResource(urlmapping='/leadcreate')
 global class SampleRest{

 @HttpPost 
 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

share|improve this answer
    
The "MyLead:" was the answer! Thank you! –  Darkenor Jul 15 at 20:22
    
@Darkenor Thanks nps .Glad it helped you –  Mohith Shrivastava Jul 15 at 20:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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