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