I wrote my Callout, but it create an object only with Id. How can I create an object with all fields?


public static App__c ParseRequest(RestRequest req) {
    App__c app = new App__c();
    String body = req.requestBody.toString();
    app = (App__c)JSON.deserialize(body, App__c.class);
    return app;  

global static Id doPost() 
    RestRequest req = RestContext.request;        
    App__c app = ParseRequest(req);
    insert app;
    return app.id;


public static void postCallout() {

    Settings__c settings = [SELECT ConsumerKey__c, ClientSecret__c, Username__c, Password__c, SecurityToken__c
                            FROM Settings__c
                            WHERE Name = 'OurSettings'];  
    String consumerKey = settings.ConsumerKey__c;
    String consumerSecret = settings.ClientSecret__c;
    String username = settings.Username__c;
    String password = settings.Password__c + settings.SecurityToken__c;
    String request = 'grant_type=password&client_id=' + consumerKey +'&client_secret=' + consumerSecret +
                     '&username=' + username + '&password='+password;

    HttpRequest ourRequest = new HttpRequest();
    ourRequest.setEndpoint(System.Label.Job_Advertisement_URL + '/services/oauth2/token');

    Obj__c obj = [SELECT Name, Description__c, Skills__c
                       FROM Obj__c WHERE Name = 'Object'];

    HttpResponse response = ourHttp.send(request);      
    OAuth2 objAuthenticationInfo = (OAuth2)JSON.deserialize(response.getbody(), OAuth2.class);
    System.debug('BODY: ' + response.getBody());

    if(objAuthenticationInfo.ACCESS_TOKEN != null){

        JSONGenerator gen = JSON.createGenerator(true);    
        gen.writeStringField('title', obj.Name);
        gen.writeStringField('description', obj.Description__c);
        gen.writeStringField('skills', obj.Skills__c);

        String jsonString = gen.getAsString();
        System.debug('jsonMaterials: ' + jsonString);

        Http finalHttp = new Http();
        HttpRequest finalRequest = new HttpRequest();

        finalRequest.setHeader('Authorization','Bearer ' + objAuthenticationInfo.ACCESS_TOKEN);

        finalRequest.setEndpoint(System.Label.URL + '/services/apexrest/AppEndpoint');
        HttpResponse finalResponse = finalHttp.send(finalRequest);
        System.debug('RESPONSE BODY: '+ finalResponse.getBody());

public class OAuth2{
    public String ACCESS_TOKEN{get;set;}    
  • May be you are while testing the process, you are sending only id. If you are not passing all field with their values. Commented Oct 8, 2018 at 9:39
  • I just return Id, but I send a body in JSON Format. And I don't know, why not all fields are there. Commented Oct 8, 2018 at 9:59
  • See the SOQL is getting record with the values. And everything is fine. Bro Commented Oct 8, 2018 at 10:02

1 Answer 1


You are parsing SObject here

public static App__c ParseRequest(RestRequest req) {
        App__c app = new App__c();
        String body = req.requestBody.toString();
        app = (App__c)JSON.deserialize(body, App__c.class);
        return app;  

But the json you send is like below


You can not parse JSON as SObject if field api names do not match. Either use a wrapper to convert JSON to Sobject or send fields' api names like below:


With wrapper it would be like:

global class AppWrapper
    global string title;
    global string description;
    global string skills;

public static App__c ParseRequest(RestRequest req) {
        AppWrapper app = new AppWrapper();
        String body = req.requestBody.toString();
        app = JSON.deserialize(body, AppWrapper.class);
        return new App__c(Title__c=app.title,Description__c=app.description,Skills__c=app.skills);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .