You need to sign in to do that
Don't have an account?
Trevor Viall
Create multiple post records
I am trying to create a post that can take in more than one record at a time. I am hitting a bit of a brick wall and was hoping someone could help me with an answer. how would I allow this to take in multiple employee records together.
public static HttpResponse PostEmployeeCallout(){
http http = new http ();
HttpRequest request = new HttpRequest();
//Set the Endpoint of the Post callout
request.setEndpoint('https://sfdnewhires.herokuapp.com/api/v3/employees');
//Set the method to be a post callout
request.setMethod('POST');
//declairing the header type and the format of the response
request.setHeader('Content-Type', 'application/json;charset=UTF-8');
list <object> employees = (List<Object>) results.get('employees');
request.setBody(body);
HttpResponse response = http.send(request);
}
public class body{
String Name {get;set;}
String hire_Date {get;set;}
String position {get;set;}
String email {get;set;}
Integer phone {get;set;}
String functional_area {get;set;}
String Location {get;set;}
String type {get;set;}
}
public body(String Name, String hire_Date, String position, String email, Integer phone, String functional_area, String Location, String type){
this.name = name;
this.hire_date = hire_Date;
this.position = position;
this.email = email;
this.phone = phone;
this.functional_area = functional_area;
this. location = Location;
this.type = type;
}
}
public static HttpResponse PostEmployeeCallout(){
http http = new http ();
HttpRequest request = new HttpRequest();
//Set the Endpoint of the Post callout
request.setEndpoint('https://sfdnewhires.herokuapp.com/api/v3/employees');
//Set the method to be a post callout
request.setMethod('POST');
//declairing the header type and the format of the response
request.setHeader('Content-Type', 'application/json;charset=UTF-8');
list <object> employees = (List<Object>) results.get('employees');
request.setBody(body);
HttpResponse response = http.send(request);
}
public class body{
String Name {get;set;}
String hire_Date {get;set;}
String position {get;set;}
String email {get;set;}
Integer phone {get;set;}
String functional_area {get;set;}
String Location {get;set;}
String type {get;set;}
}
public body(String Name, String hire_Date, String position, String email, Integer phone, String functional_area, String Location, String type){
this.name = name;
this.hire_date = hire_Date;
this.position = position;
this.email = email;
this.phone = phone;
this.functional_area = functional_area;
this. location = Location;
this.type = type;
}
}
You should convert the list of sObjects in to JSON and than pass it to the body. Use the below method: Now pass this JSON string in body.
Let me know if you need any further help on ths.
Thanks,
Abhishek Bansal.