function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sagar MauryaSagar 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.
Best Answer chosen by Sagar Maurya
Davy HuijgensDavy Huijgens
global class CaseMessage{
    String id;
    String subject;

    public CaseMessage(String id, String subject){
        this.id = id;
        this.subject = subject;
    }
​}

and you can use this class for your message
 
CaseMessage message = new CaseMessage(c.id, c.subject);

HttpRequest req = new HttpRequest();
req.setBody(JSON.serialize(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

Davy HuijgensDavy Huijgens
You are trying to serialize your HttpRequest, this is not correct.

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.
Sagar MauryaSagar Maurya
@davy huijgens can you share any example for the above mentioned solution?
Davy HuijgensDavy Huijgens
global class CaseMessage{
    String id;
    String subject;

    public CaseMessage(String id, String subject){
        this.id = id;
        this.subject = subject;
    }
​}

and you can use this class for your message
 
CaseMessage message = new CaseMessage(c.id, c.subject);

HttpRequest req = new HttpRequest();
req.setBody(JSON.serialize(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.
This was selected as the best answer