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

12 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
  4. Thank you for sharing with unique informationSalesforceOnline Training

    ReplyDelete
  5. Apex class as Rest web service clear explanation saleforce training

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Hiiii....Thank you so much for sharing Great information....Nice post...Keep move on.....
    Best Salesforce Training in Hyderabad

    ReplyDelete
  8. Thanks, foг ones marvelous posting! I genuinely enjoyed reading it, you miggһt Ƅe a great author. I wiⅼl made certain to booҝmark your blog and ѡill often come back sometime soon. I want to encoᥙrage yourself to continue your great job, have a nice evening!
    Web Development Course
    best web development courses
    web development classes
    web development course near me
    Web Development Training
    training on web development
    web development training program
    web development training course
    web development summer training

    ReplyDelete
  9. I definitely enjoying every little bit of it. It is a great website and nice share. I want to thank you. Good job! You guys do a great blog, and have some great contents. Keep up the good work. Protocol

    ReplyDelete
  10. I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site. As a result of checking through the net and meeting techniques that were not productive, Same as your blog I found another one Oracle APEX .Actually I was looking for the same information on internet for Oracle APEX and came across your blog. I am impressed by the information that you have on this blog. Thanks once more for all the details.

    ReplyDelete
  11. Thanks for a wonderful share. Your article has proved your hard work and experience you have got in this field. Brilliant .i love it reading. photographe publicitaire

    ReplyDelete
  12. I have read all the comments and suggestions posted by the visitors for this article are very fine,We will wait for your next article so only.Thanks! link

    ReplyDelete

How to include a screen flow in a Lightning Web Component

 Hi, Assume  you have a flow called "Quick Contact Creation" and API Name for the same is "Quick_Contact_Creation". To i...