I have written an Restful GET API call to get 10 records based on some condition. In that I am returning all records as a string, while executing that method from Workbench I am not getting proper format. But in debug logs I am getting proper JSON response. Did I missed something?


global with sharing class RestWebshowMappingscls {
    global static string getMeetInviteDetails(){
        List<Meeting__c> metLst=new List<Meeting__c>();
        List<Mainwrapper> mainWrapLst;
        string finalqryvalue;
        metLst=[select id,name,Event__r.name,Meeting_ID__c,Meeting_with__c,Meeting_UUID__c,(select Meeting__r.name,Attendee__r.name,Unique_ID__c,Attendee_Status__c,Attendee_Name__c from Invites__r) from Meeting__c where Meeting_ID__c!=null limit 10];
        system.debug('metLst value' +metLst.size());
        if(metLst.size()>0 && metLst!=null){
              for(Meeting__c met:metLst){ 
                mainWrapLst=new List<Mainwrapper>();
                Mainwrapper mainWrap=new Mainwrapper();

                List<meeting> meetingWrapLst=new List<meeting>();
                meeting meetingWrap=new Meeting();             

                List<invite> inwrap;
                List<invites__c> invlst;
                for(Invites__c inv : met.invites__r){
                    inwrap=new List<invite>();  
                    invite invt=new invite();   
                    inwrap.add(invt) ;                           
                mainWrap.message='Details of Meetings';

                RestContext.response.responseBody = Blob.valueOf(JSON.serialize(mainWrapLst));
                finalqryvalue = JSON.serialize(mainWrapLst); 
                system.debug('finalqryvalue @@' +finalqryvalue);

           } catch(exception ex){
               System.debug('exception type' +ex.getTypeName());
        return finalqryvalue;

//Wrapper to diplay values///
    global class Mainwrapper{
        public List<meeting> data;
        public integer statusCode;
        public string status;
        public string message;                  
    global class meeting{
        public string meetingwith;
        public string eventName;
        public string mid;
        public string meetinguuid;
        public List<invite> invites;
    global class invite{
        public string invitestatus;
        public string inviteUniqueid;


Workbench screenshot:enter image description here

  • Welcome to SFSE. When you post your code, please make sure to use the Code Formatting tool {} or Ctrl-K to format your code. Otherwise it is unreadable. I've fixed it here. – David Reed Jul 4 '18 at 12:13

Here is the issue:

RestContext.response.responseBody = Blob.valueOf(JSON.serialize(mainWrapLst));
finalqryvalue = JSON.serialize(mainWrapLst); 
system.debug('finalqryvalue @@' +finalqryvalue);

What you're debugging is in fact JSON. But your service is returning (i.e., its actual return value) a string, which is escaped automatically by Salesforce to produce JSON. Your web service return value is converted to JSON by Salesforce automatically; when this happens, your string - which is already JSON - gets escaped, and Workbench shows you that escaped string.

You don't need to set the response body and also return a value, and you actually don't need to do any explicit serialization at all. Just return your serializable structure:

global static List<Mainwrapper> getMeetInviteDetails() {
    // ...
    return mainWrapLst;

Salesforce will take care of marshalling it into JSON for you.


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.