1

I have got a web service provided to me, which I use & insert a record into the external application through Salesforce "Task" object. As a response, the following is returned by the web service {"status": "ok","message": "Successfully added new note.","data": [],"id": "12345"}.

Now, I got the requirement to capture the response "id" and insert into Salesforce "Task" custom field "noteId". And My Main web service class is

public with sharing class InsertNote_WebService 
{   
@future(callout=true)
public static void createNoteRecord(String memberId,String enteredBy,String description,string notecategory)
{
    Http h = new Http();
    HttpRequest req = new HttpRequest(); 
    HttpResponse res = new HttpResponse();
    String BaseUrl = 'http://testsite.com/service/id/key'; //My external website url
    JSONGenerator_AC jsgenrator = new JSONGenerator_AC();
    string Message = jsgenrator.generateJSONContent(memberId,enteredBy,description,notecategory);                                 
    req.setEndpoint(BaseUrl);
    req.setMethod('POST');
    req.setHeader('Content-Type','text/html');        
    req.setBody(Message);
    try{
        res = h.send(req);
    }
    catch(Exception e){
        System.debug(e);
    }
} 

My JSONGenerator_AC class is:

public with sharing class JSONGenerator_AC 
{
    public class Noteclass 
    { 
        String member_id;
        String entered_by;
        String note_text;
        String category_id;

        public Noteclass(String memberid, String entered_by, String description, string notecategory) 
        { 
            this.member_id = memberid;
            this.entered_by = entered_by;
            this.note_text = description;
            this.category_id = notecategory;
        }
    }
    Noteclass note;
    public String generateJSONContent(String memberid, String entered_by, String description, string notecategory) 
    {
        // Create a JSONGenerator object.
        // Pass true to the constructor for pretty print formatting.       
        JSONGenerator jsonGen = JSON.createGenerator(true);

        // Create an object to write to the JSON string.        
        note = new Noteclass (memberid, entered_by, description, notecategory);

        // Write data to the JSON string.
        jsonGen.writeStartObject();
        jsonGen.writeStringField('member_id', memberid);
        jsonGen.writeStringField('entered_by', entered_by);
        jsonGen.writeStringField('note_text', description);
        jsonGen.writeStringField('category_id', notecategory);
        jsonGen.writeEndObject(); 

        // Get the JSON string.
        String jsonstring = jsonGen.getAsString();
        System.debug('Hello Sample JSON'+ jsonstring);
        return jsonstring;
    }
    /*
    *@decsription: Utility Method to generate response JSON
    */
    public static String generateResponseJSON(Map<String,String> responseValues)
    {
        JSONGenerator jsonGen = JSON.createGenerator(true);
        jsonGen.writeStartObject();
        jsonGen.writeFieldName('response');
        jsonGen.writeStartObject();
        for(String key:responseValues.keySet())
        {
            jsonGen.writeStringField(key,responseValues.get(key));
        }
        jsonGen.writeEndObject();
        jsonGen.writeEndObject();
        return jsonGen.getAsString();
    }

    /*
    *@decsription: Utility Method to generate fault JSON
    */
    public static String generateFaultJSON(String faultCode, String faultMsg)
    {
        JSONGenerator jsonGen = JSON.createGenerator(true);
        jsonGen.writeStartObject();
        jsonGen.writeFieldName('response');
        jsonGen.writeStartObject();
        jsonGen.writeStringField('errorCode',faultCode);
        jsonGen.writeStringField('message',faultMsg);
        jsonGen.writeEndObject();
        jsonGen.writeEndObject();
        return jsonGen.getAsString();
    }
}
1

You need to deserialize your response and get the ID property which you can do simply like this:

// '{"status": "ok","message": "Successfully added new note.","data": [],"id": "12345"}'
String responseBody = res.getBody();
Map <String, Object> resultObject = (Map <String, Object>)JSON.deserializeUntyped(responseBody);
String recordId = (String)resultObject.get('id');
system.debug(recordId);

Which results in:

16:41:53.054 (54419953)|USER_DEBUG|[3]|DEBUG|12345

Then you can use the recordId variable (which has the ID value) to do whatever is next, to store it into the noteId field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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