0

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

HttpPost:

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;  
}

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

Callout:

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.setBody(request);
    ourRequest.setMethod('POST');
    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.writeStartObject();
        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.setHeader('Content-Type','application/json');
        finalRequest.setHeader('accept','application/json');
        finalRequest.setBody(jsonString);

        finalRequest.setMethod('POST');
        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. – pranil nimase Oct 8 '18 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. – MichaelLev19 Oct 8 '18 at 9:59
  • See the SOQL is getting record with the values. And everything is fine. Bro – pranil nimase Oct 8 '18 at 10:02
2

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

{"title":someTitle,"description":someDescription,"skills":someSkills}

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:

{"Title__c":someTitle,"Description__c":someDescription,"Skills__c":someSkills}

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);
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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