
I am making an api call as shown in the code.
The code is working fine but I want to insert the data into force.com.I have created TestObject and all related fields in force.com and now I need to load all records into force.compls let me know hiw it is done.pooja biswaspublic class my_callout
{
public String Output{get;set;}
public List<WrapText> wrap{get;set;}
public class WrapText
{
public Integer UserID{get;set;}
public Integer ID{get;set;}
public String Title{get;set;}
public String Body{get;set;}
public WrapText(Integer u, Integer i,String t, String b)
{
UserID=u;
ID=i;
Title=t;
Body=b;
}
}
public void makeCall() //making a request to the remote site and get an response.
{
HttpRequest req=new HttpRequest();
req.setEndpoint('http://jsonplaceholder.typicode.com/posts'); //url of the request
req.setMethod('GET');
Http h = new Http();
HttpResponse res=h.send(req);
Output=res.getBody(); //will contain complete json body
//now deserialize it
wrap=(List<WrapText>)JSON.Deserialize(res.getBody(),List<WrapText>.class);
}
}
Hey Pooja, Record insertion must be failing because of some reason. I have added debug statments to check that.
List<TestObject__c> insertList = new List<TestObject__c>();
for(WrapText wrp : wrap){
TestObject__c tstObj = new TestObject__c();
tstObj.FieldName__c = wrp.Body;
// populate other fields here
insertList.add(tstObj);
}
Database.SaveResult[] srList = Database.insert(insertList,false);
// Iterate through each returned result
for (Database.SaveResult sr : srList) {
if (!sr.isSuccess()) {
// Operation failed, so get all errors
for(Database.Error err : sr.getErrors()) {
System.debug('The following error has occurred.');
System.debug(err.getStatusCode() + ': ' + err.getMessage());
System.debug('Fields that affected this error: ' + err.getFields());
}
}
}