+ Start a Discussion
Meena25Meena25 

REST API Post method

request.setBody('"lead":{"leadId":" "},{"firstName":"FN1"},{"lastName":"LN2"},{"ssn":"2"}');

I need to send values from salesforce to endpoint, am able to make the connection, however i see null values. I need to be able to pass more fields and corresponding values of lead object. Could someone help me how to use(send) multiple parameters in request.setBody?
Anant KamatAnant Kamat
You need to create Wrapper Classes for the same in the REST API class. Then you need to pass the values accordingly as below
List < LeadWrapper> lstLeadWrapper = new List < LeadWrapper> (); // Create a list if you want to send buld data
LeadWrapper leadWrapper = new LeadWrapper(l);
lstLeadWrapper.add(leadWrapper); // Iterate through your records and add to the list
Map < String, Object > objMap = new Map < String, Object > ();
objMap.put('lead', lstLeadWrapper );
String reqBody = JSON.Serialize(objMap);
system.debug('reqBody....' + reqBody);
req.setBody(reqBody);

 public class LeadWrapper{
        public String leadId;
        public String firstName;
        public String lastName;
        public String ssn;
       
        public LeadWrapper(Lead l){ 
            this.leadId = l.id.
            this.firstName = l.firstName;
            this.lastName = l.lastName;
            this.ssn = l.ssn
        }
}

Let me know if this helps you.