You need to sign in to do that
Don't have an account?
Sagar Maurya
serialization in callout
I have a requirement of doing a callout on case creation and based on the response(JSON) received update the case record. I need to do a callout and send case Id and case subject in the request body to external system. I am facing problem in serializing the request. Below is the code for the same. Please let me know what is wrong with this code:
Trigger-
trigger caseTrigger on Case (after insert,after update) {
if(trigger.isInsert){
for(case c : trigger.new){
checkCaseClass.checkCase(c.id,c.subject);
}
}
}
Helper class-
public class checkCaseClass{
@future (callout=true)
public static void checkCase(Id CaseId, string subject){
for(case c:[select id,subject,description from case where id =: caseId]){
Http http = new Http();
HttpRequest req = new HttpRequest();
req.setHeader('Content-Type','application/json');
String endpoint = 'any end point';
req.setMethod('POST');
req.setEndpoint(endpoint);
req.setBody('CaseId='+EncodingUtil.urlEncode(CaseId, 'UTF-8')+'subject='+EncodingUtil.urlEncode(subject, 'UTF-8');
String s= JSON.serialize(req);
HTTPResponse response = http.send(req);
system.debug('response -->'+response);
}
}
}
When above code is executed, I get error - System.JSONException: Apex Type unsupported in JSON: System.HttpRequest.
Please let me know the possible solution for this.
Trigger-
trigger caseTrigger on Case (after insert,after update) {
if(trigger.isInsert){
for(case c : trigger.new){
checkCaseClass.checkCase(c.id,c.subject);
}
}
}
Helper class-
public class checkCaseClass{
@future (callout=true)
public static void checkCase(Id CaseId, string subject){
for(case c:[select id,subject,description from case where id =: caseId]){
Http http = new Http();
HttpRequest req = new HttpRequest();
req.setHeader('Content-Type','application/json');
String endpoint = 'any end point';
req.setMethod('POST');
req.setEndpoint(endpoint);
req.setBody('CaseId='+EncodingUtil.urlEncode(CaseId, 'UTF-8')+'subject='+EncodingUtil.urlEncode(subject, 'UTF-8');
String s= JSON.serialize(req);
HTTPResponse response = http.send(req);
system.debug('response -->'+response);
}
}
}
When above code is executed, I get error - System.JSONException: Apex Type unsupported in JSON: System.HttpRequest.
Please let me know the possible solution for this.
and you can use this class for your message
Combine this with your code, if neccesary check the documentation on the salesforce classes, also do not forget to read up on bulkifying your code.
If this helped you please select it as the best answer.
All Answers
You have several options. You can create an apex class with your return values and serialize that class, before setting it as the body of your httprequest. Or you could just contruct the JSON in a string and set that as a body.
Please do note that your code is not bulkified and will fail when a larger number of reocrds is imported.
and you can use this class for your message
Combine this with your code, if neccesary check the documentation on the salesforce classes, also do not forget to read up on bulkifying your code.
If this helped you please select it as the best answer.