You need to sign in to do that
Don't have an account?
Simon234
Callout REST HttpPost: how to create an object with all fields?
I wrote my Callout, but it create an object only with Id. How can I create an object with all fields?
HttpPost:
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;} }
It must be creating the record with the provided field values but you need to put a SOQL to fetch the other field values.
Regards,
Ashish Kr.