+ Start a Discussion
Rima KACIRima KACI 

REST Web Service

Hi,

Can anyone tell me how to expose REST webserive.
How to write classe and method which return List<Task> from salesforce 

retreiveTaskByDates(DateTime start, DateTime end)

thanks for help
Best Answer chosen by Rima KACI
Deepali KulshresthaDeepali Kulshrestha
Hi Rima,

Yes, you can use HttpPost annotation and HttpGet annotation as per your requirement and In both POST and GET you can use parameters in URL. 
In the following code, we pass parameters (DateStart and DateEnd) in URL and debug these parameters in the rest class, you can use them as per your requirement.
Try this Url for Hit rest class:'/services/apexrest/taskRestClass?DateStart=08/07/2019&DateEnd=10/07/2019':
 
@RestResource(urlMapping='/taskRestClass/*')
global with sharing class TaskRestClass {
    @HttpGet
    global static void methoadExampleRestApi(){
        RestResponse response = RestContext.response;
        RestRequest request = RestContext.request;
        response.addHeader('Content-Type','applicatin/json');
        try {
            String fromDate = RestContext.request.params.get('DateStart');
            String toDate = RestContext.request.params.get('DateEnd');
            String fromDate1 = String.valueOf(fromDate);
            String toDate1 = String.valueOf(toDate);
            Date DateStart  = Date.parse(fromDate1);
            Date DateEnd = Date.parse(toDate1);
            system.debug('StartDate->'+DateStart);
            system.debug('EndDate->'+DateEnd);
            
            String taskInfor;
            List<Task> taskList=new List<Task>();
            taskList=[SELECT Id,Status,Subject FROM Task  LIMIT 10000];
            if(taskList.size()>0){
                taskInfor = JSON.serializePretty(taskList);
                response.responseBody = Blob.valueOf(taskInfor);
                response.addHeader('Content-Type', 'application/json');
                response.statusCode = 200; //set return status to true 
            }
            else{
                response.responseBody = Blob.valueOf('No Task record found.');
                response.addHeader('Content-Type', 'application/json');
                response.statusCode = 400; //set return status to false
            }
        }
        catch(Exception ex){
            system.debug('LineNo->'+ex.getLineNumber()+'Msg->'+ex.getMessage());
            /*retreiveTaskByDates(DateTime start, DateTime end)*/
        }
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha

All Answers

Deepali KulshresthaDeepali Kulshrestha
Hi Rima,

Try the following rest class, it may be helpful for you:
Try this Url:'/services/apexrest/taskRestClass':
@RestResource(urlMapping='/taskRestClass/*')
global with sharing class TaskRestClass {
    @HttpGet
    global static void methoadExampleRestApi(){
        RestResponse res = RestContext.response;
        RestRequest req = RestContext.request;
        try {
            String taskInfor;
            List<Task> taskList=new List<Task>();
            taskList=[SELECT Id,Status,Subject FROM Task LIMIT 10000];
            if(taskList.size()>0){
                taskInfor = JSON.serializePretty(taskList);
                res.responseBody = Blob.valueOf(taskInfor);
                res.addHeader('Content-Type', 'application/json');
                res.statusCode = 200; //set return status to true 
            }
            else{
                res.responseBody = Blob.valueOf('No Task record found.');
                res.addHeader('Content-Type', 'application/json');
                res.statusCode = 400; //set return status to false
                }
        }
            catch(Exception ex){
            system.debug('LineNo->'+ex.getLineNumber()+'Msg->'+ex.getMessage());
            /*retreiveTaskByDates(DateTime start, DateTime end)*/
        }
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
Ajay K DubediAjay K Dubedi
Hi Rima,

I hope you are doing well, You can expose your Apex classes and methods so that external applications can access
your code and your application through the REST architecture.

These link will help you to know how to implement a simple REST API in Apex.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_rest_code_sample_basic.htm
https://trailhead.salesforce.com/en/content/learn/modules/apex_integration_services/apex_integration_webservices
https://trailhead.salesforce.com/en/content/learn/modules/apex_integration_services/apex_integration_webservices

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi
Rima KACIRima KACI
Thanks for your answers :-)
@Deepali, can I use the same code with Httppost annotation?
I've seconde question about method's parameters (DateStart and DateEnd) these two parameters are sending from third-party system.
how can'I recover this parameters from request sending by third-system call?

thanks for your help
Deepali KulshresthaDeepali Kulshrestha
Hi Rima,

Yes, you can use HttpPost annotation and HttpGet annotation as per your requirement and In both POST and GET you can use parameters in URL. 
In the following code, we pass parameters (DateStart and DateEnd) in URL and debug these parameters in the rest class, you can use them as per your requirement.
Try this Url for Hit rest class:'/services/apexrest/taskRestClass?DateStart=08/07/2019&DateEnd=10/07/2019':
 
@RestResource(urlMapping='/taskRestClass/*')
global with sharing class TaskRestClass {
    @HttpGet
    global static void methoadExampleRestApi(){
        RestResponse response = RestContext.response;
        RestRequest request = RestContext.request;
        response.addHeader('Content-Type','applicatin/json');
        try {
            String fromDate = RestContext.request.params.get('DateStart');
            String toDate = RestContext.request.params.get('DateEnd');
            String fromDate1 = String.valueOf(fromDate);
            String toDate1 = String.valueOf(toDate);
            Date DateStart  = Date.parse(fromDate1);
            Date DateEnd = Date.parse(toDate1);
            system.debug('StartDate->'+DateStart);
            system.debug('EndDate->'+DateEnd);
            
            String taskInfor;
            List<Task> taskList=new List<Task>();
            taskList=[SELECT Id,Status,Subject FROM Task  LIMIT 10000];
            if(taskList.size()>0){
                taskInfor = JSON.serializePretty(taskList);
                response.responseBody = Blob.valueOf(taskInfor);
                response.addHeader('Content-Type', 'application/json');
                response.statusCode = 200; //set return status to true 
            }
            else{
                response.responseBody = Blob.valueOf('No Task record found.');
                response.addHeader('Content-Type', 'application/json');
                response.statusCode = 400; //set return status to false
            }
        }
        catch(Exception ex){
            system.debug('LineNo->'+ex.getLineNumber()+'Msg->'+ex.getMessage());
            /*retreiveTaskByDates(DateTime start, DateTime end)*/
        }
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
This was selected as the best answer
Rima KACIRima KACI
thanks so much for help