• kpn
  • NEWBIE
  • 20 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 2
    Replies
I just wanted to remove the null values from the response, how can I achieve it?

For exmaple:
 
global class ResponseHandler {
  
    public String Status {get; set;}
    public List<sObject> result {get;set;}
    public String Message {get;set;}
    public String ErrorCode {get; set;}
 
}
 
@RestResource(urlMapping='/testapi/*')
global class OyeCodeRestAPI {
@HttpGet 
    global static ResponseHandler GET()
    {
        ResponseHandler response = new ResponseHandler();
        Contact  returnContact = getContact();
        
        if(returnContact!=null)
        {
            response.Status = 'Success';
            List<sObject> thesObjectList = new List<sObject>();
            thesObjectList.add((sObject)returnContact);
            response.Data = thesObjectList;
        }
        
        else
        {
            response.ErrorCode = 'Error Code -0002';
            response.Status = 'error';
            response.Message = 'Fail : No Record Found';
            
        }
        
        return response;
    }

    public class NoRecordException extends Exception {}
    
    public static Contact getContact()
    {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String ContactId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1); 
        Contact result;
        try{
            result = [SELECT Id, lastname, firstName, phone, email FROM Contact WHERE Id = :ContactId];
        }
        Catch(System.QueryException e)
        {
            throw new NoRecordException('Unable to find the record maching Id : '+ContactId);
        }
        return result;
    }
}

So on success, the response still has the Error Code & Message set to NULL. I just need to remove the same from the response to keep only what is required, how to achieve this?
  • July 20, 2016
  • Like
  • 0
I just need to provide mock responses for the all the REST API's I'm developing and I'll be sendingmock=true in the request parameter. If mock=true, then it should bypass the logic in apex code and just send the response. what is the best way to achieve this?

What I'm doing right now is, I'm just looking for the mock parameter and if true, sending the response back. So I'm storing the dummy JSON in a variable to send it back. Is there any other approach?
  • July 18, 2016
  • Like
  • 0
@RestResource(urlMapping='/v1/organizations')
global with sharing class OrganizationInfoClass {

	@HttpGet
    global static void doGet() {
    	RestResponse response = RestContext.response;

 		List<Organization> OrgList = [SELECT Id, Name, IsSandbox  FROM Organization limit 1];
 		response.statusCode = 200;
 		response.responseBody = Blob.valueOf(JSON.serialize(OrgList));
    }    
}

I wanted to get the response as XML? How should I do this?
 
@RestResource(urlMapping='/v1/organizations')
global with sharing class OrganizationInfoClass {

	@HttpGet
    global static List<Organization> doGet() {
    	RestResponse response = RestContext.response;

 		List<Organization> OrgList = [SELECT Id, Name, IsSandbox  FROM Organization limit 1];
 		return OrgList;
    }    
}

In the above snippet, the return type is List<sObject> and when I do the API request with .json at the end, I'm getting JSON response and if with .xml at the end, then I'm getting the XML response.

i.e., /services/apexrest/v1/organizations.json - Gives JSON response, 
/services/apexrest/v1/organizations.xml - gives XML response.

Why the same is not working in the first snippet (where the return type is Void and setting the response to the responseBody)?
  • July 11, 2016
  • Like
  • 0
@RestResource(urlMapping='/v1/organizations')
global with sharing class OrganizationInfoClass {

	@HttpGet
    global static void doGet() {
    	RestResponse response = RestContext.response;

 		List<Organization> OrgList = [SELECT Id, Name, IsSandbox  FROM Organization limit 1];
 		response.statusCode = 200;
 		response.responseBody = Blob.valueOf(JSON.serialize(OrgList));
    }    
}

I wanted to get the response as XML? How should I do this?
 
@RestResource(urlMapping='/v1/organizations')
global with sharing class OrganizationInfoClass {

	@HttpGet
    global static List<Organization> doGet() {
    	RestResponse response = RestContext.response;

 		List<Organization> OrgList = [SELECT Id, Name, IsSandbox  FROM Organization limit 1];
 		return OrgList;
    }    
}

In the above snippet, the return type is List<sObject> and when I do the API request with .json at the end, I'm getting JSON response and if with .xml at the end, then I'm getting the XML response.

i.e., /services/apexrest/v1/organizations.json - Gives JSON response, 
/services/apexrest/v1/organizations.xml - gives XML response.

Why the same is not working in the first snippet (where the return type is Void and setting the response to the responseBody)?
  • July 11, 2016
  • Like
  • 0