Enhancing Salesforce Apex with Powerful WebServices: Unleashing the Potential


WebServices are the Custom Business Logic Classes, Which can be accessible from the Third Party Applications.
(Ex: From Banking Applications, Finance Applications, Insurance Applications, SAP Applications,...etc.)

By using Web Services, we can expose the Business Logics, Data, and Functionalities to the Third party Applications.
Web Services will make the Multiple Disparate Systems can communicate with other, in-order to exchange the data / business logic / functionalities.
Web Services are Purely Platform Independent and Language Independent.
We have the below 2 types of Web Services.
  1. REST Based Web Services (RestFul).
  2. SOAP Based Web Services.
REST Based Web Services:

REST --> Representational State Transfer.
REST is an Architectural Style, Which has been purely implemented based on the HTTP Protocol.
It is using the Old HTTP Protocol to exchange the data / functionalities / business logic between the disparate systems.
They are using the old HTTP Protocol, and added a set of enhancements on Top of HTTP Protocol to exchange the data between the multiple Third Party Applications.
In Restful Web Services, each Business Logic/ Functionality / Data will be referred as a "Resource".
For Each recourse, we can provide the resource name. We can access each resource by using the "URI (Uniform Resource Identifier)".
Syntax:
http://www.sample.com/<resourceName>;
To access the resources over the HTTP Protocol, we have to use the below HTTP Methods.
HTTP Protocol provides the below 5 Methods to bs used to interact with the Resources to perform the operations.
1. GET : Used to Get the Records from the resource
2. DELETE : Used to Remove the records from the Resource
3. POST : Used to insert the New Records into the Resource
4. PUT : Used to Update the Existing records in the resource
5. PATCH : Used to Perform the Upsert Operation on the records. (i.e. Update + Insert).Restful Web.

Service will expose the response in the form of either "JSON / XML / TEXT".
Most of the Mobile Applications, Cloud Applications, Web Applications are supporting the RestFul Web Services". Because RestFul Web Service are very lightweight, easy to implement, and easy to parse.
By using Apex Programming, we can build our own RestFul Webservice and can expose the Web Service to the Third Party Systems.

To Prepare the RestFul Web Services, Apex provides the below Annotations.

1. @RestResource(). 2. @HttpGet(). 3. @HttpDelete(). 4. @HttpPost(). 5. @HttpPatch(). 6. @HttpPut().

@RestResource(): ================ This annotation is used to expose a Business Logic Class to the Third Party System. So that it can consume the Web Service, to exchange the data.
Each Web Service Class should be defined with the "Global" access specifier. Each Web Service Class should be identified by using a "Resource Name", which can be provided by using the "@RestResource()" annotation. i.e. Each RestFul Web Service class should be pre-fixed with "@RestResource()" annotation.

Syntax: @RestResource(URLMapping='/<ResourceName>/*') Global Class <className> { // Write the Business Logic.. }

Note: URLMapping parameter is used to specify the resource name, through which the Web Service class can be accessible.

Ex: @RestResource(URLMapping='/AccountsService/*') Global Class AccountsManager { // Write the Business Logic.. } Ex: @RestResource(URLMapping='/CaseManagerService/*') Global Class CaseHelper { // Write the Business Logic.. }

Accessing the RestFul Web Service: (EndPoint URL).

Each RestFul WebService can be accessible by the Third Party Systems with the help of "URI" as below.
Syntax: https://<ServerNumber>.Salesforce.com/Services/Apexrest/<ResourceName> Ex: https://ap16.salesforce.com/services/apexrest/AccountsService. https://ap16.salesforce.com/services/apexrest/CaseManagerService.

@HttpGet() Method: ==================
HTTPGet Method is used to get required resources / information / data from the Provider / Server. This annotation can be applicable only on the Method Level. Note: All the Web Service Methods should be defined with "Static" keyword and should be defined with "Global" access specifier.

Syntax: @HttpGet() Global static <ReturnType> <MethodName>(<Parameters List>) { // Write the Business Logic.. } Note: We can't define Two Methods inside the Web Service Class with the Same Annotation. (i.e. We can't define Two Methods with the "HTTPGet()" annotation) Ex: @RestResource(URLMapping='/AccountsService/*') Global Class AccountsHelper { @HttpGet() Global static List<Account> GetAllAccounts() { // Write the Business Logic, to Get All Account Records.. } }

Accessing the Web Service:

Ex:
https://ap16.salesforce.com/services/apexrest/AccountsService
We can invoke the WebService from the below tools.

        1. Workbench Tool.
        2. Postman Tool
        3. Through Apex Programming
        4. By using Integration Tools (Ex: Mulesoft, Tibco, Webmethods,...etc)
        5. By using Third Party Applications.


Example Coming Soon...….stay Tuned













1 تعليقات

  1. This was an incredibly insightful dive into WebServices within the Salesforce Apex framework! For developers working with Salesforce or looking to integrate other applications with it, understanding WebServices is absolutely crucial, especially given the platform-independent nature of these services. I appreciate the comprehensive breakdown of RESTful web services and their methods, which are becoming increasingly prevalent in modern application development.

    ردحذف

إرسال تعليق

Post a Comment

أحدث أقدم