Thursday, 15 September 2016

Exposing an Apex Class as REST Web Service

Hi ,

We are going to learn how to write a REST webservice in salesforce.com.

We can expose our Apex classes and methods so that external applications can access our code

and our application through the REST architecture.


 @RestResource annotation is used to expose a class as REST resource .

 Similarly we have to add annotations to methods to expose them throug REST like @HttpPost,@HttpGet etc.,

 If we add "@HttpGet" annotation for a method to expose it as a Rest resource then that can be called by Http GET request
 from external applications.

Sample Post Service:
 ---------------------
@RestResource(urlMapping='/DoctorService/*')
global without sharing class StudentCreationcls {
    @HttpPost
    global static String createStudentRecord(DoctorServiceParser doctObj){
        Doctor__c doctorObj = new Doctor__c();
        doctorObj.Name =doctObj.Name;
        doctorObj.ConsultationFee__c=doctObj.ConsultationFee;
        doctorObj.FirstName__C=doctObj.FirstName;
        doctorObj.LastName__c=doctObj.LastName;
        doctorObj.Salary__c = doctObj.Salary;
        doctorObj.Gender__c = doctObj.Gender;            
     
        Database.saveResult saveResult = database.insert(doctorObj,false);
        if(saveResult.isSuccess()){
            System.debug('Record Id:'+saveResult.getId());
        }
        else{
            System.debug('saveResult:'+saveResult.getErrors());
        }
        //Response
        JSONGenerator gen=JSON.createGenerator(true);    
        gen.writeStartObject();
        gen.writeStringField('message','Doctor record is created Successfully');
        gen.writeEndObject();    
        String responseString= gen.getAsString();
        return responseString;
    }
}

Parser Class:
------------------
This class will be used for parsing the details in the request
global class DoctorServiceParser {

    global String Name{get;set;}
    global String FirstName{get;set;}
    global decimal consultationFee{get;set;}
    global String LastName{get;set;}
    global decimal Salary{get;set;}
    global String Gender{get;set;}
}

Our service endpoint will be :https://hostnamesalesforce/services/apexrest/DoctorService


Request Format for this:
--------------------------
 {"doctObj":{
"Name":"Balaji",
"Gender":"M",
"FirstName":"Balaji",
"LastName":"M",
"ConsultationFee":2000,
"Salary":500000
}
 }

We can execute this in rest Explorer of workbench for validating before we are going to give for exteranl applications.
















References:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_rest_code_sample_basic.htm
https://developer.salesforce.com/page/Creating_REST_APIs_using_Apex_REST
https://developer.salesforce.com/docs/atlas.en-us.202.0.api_rest.meta/api_rest/quickstart_prereq.htm

4 comments:

  1. i am final year student,i wish to executing my projects on cloud computing.So i have bookmarked the collection of information but your article also a very good interesting information which helps to improve my career path.


    Salesforce Training in Chennai

    ReplyDelete
  2. thank you for sharing such a unique information.very glad to leave a comment here.we appriciate your support.one of the recommanded blog in Sales force

    Sales force online training in hyderabad
    Sales force online training

    ReplyDelete
  3. Great article thank you for sharing. Salesforce online training and enhance your skills in Salesforce

    ReplyDelete