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

Saturday, 3 September 2016

Excellent C ,C++ ,Java ,Photo Shop Tutorias

Hi Guys,

Please find the below url for excellent tutorials of C,C++,Java ,Photo Shop Tutorials.


http://60minutestutorials.com/


Contact Details :

Sri Sai Computer Education

Phone:+91 9885094573,7386972889 ,

Kadapa,
Andhra Pradesh (state),
India(country) -- 516001.