Integration

Custom Apex WebService using REST

Custom Apex WebService using REST- Salesforce

Custom Apex WebService using REST


We can create custom WebServices either by using REST or SOAP. This post will explain, how to create Custom Apex WebService using REST and how to test it using REST Explorer.

If you are new to REST API then refer 

http://www.worldofsalesforce.com/2017/04/salesforce-rest-api-with-example.html

 before proceeding.


If you are interested in knowing more about SOAP webServices then refer 

http://www.worldofsalesforce.com/2016/05/creating-custom-webservice-in-salesforce.html

.Lets start with creating a apex class and exposing it as WebService.
Expose a Class as a REST Service:   To expose a class as rest resource you need to define you apex class as Global and define underlying methods as Global static. The class should be annotated with @RestResource(urlMapping='/xxx/')  and method with individual Rest methods eg:- @HttpGet. 

Copy and past the below code to create a Rest Web service class which is responsible for creation ,deletion and query of Account record.


@RestResource(urlMapping='/AccountRest/*')
global with sharing class AccountWebService {

    @HttpGet
    global static Account getAcccById() {
        RestRequest request = RestContext.request;
        // grab the AccountId from the end of the URL
        String AccId = request.requestURI.substring(
          request.requestURI.lastIndexOf('/')+1);
        Account result =  [SELECT Id,Name
                        FROM Account
                        WHERE Id = :AccId];
        return result;
    }

    @HttpPost
    global static ID createAccount(String AccName) {
        Account thisAcc = new Account(
            Name=AccName);
        insert thisAcc;
        return thisAcc.Id;
    }   

    @HttpDelete
    global static void deleteAccount() {
        RestRequest request = RestContext.request;
        String AccId = request.requestURI.substring(
            request.requestURI.lastIndexOf('/')+1);
        Account thisAcc = [SELECT Id FROM Account WHERE Id = :AccId];
        delete thisAcc;
    }   

}


Testing Rest WebService To test the Apex custom Rest WebService you can use Rest Explorer utility built
by me. Click on the below link and login to the instance where you have created the apex class. 

http://www.worldofsalesforce.com/p/world-of-salesforce.html

After logging In you would be presented a screen as shown below to start the rest Test.
Salesforce Rest Explorer

Now we will create a record in salesforce using POST method call. Select POST Radio button on the explorer. Change the URI from /services/data/v39.0 to /services/apexrest/AccountRest/.  
Here AccountRest is the URL mapping define in the Apex class @RestResource annotation.

Now in the method body pass the parameter name of the account as JSON body As. 
{
"AccName": "Testing Rest Webservice"
}

and press execute . See the snapshot below for more clarity.

Salesforce Rest Explorer

Copy the response Id and check in salesforce if the record is created .

Now lets try to fetch the details using GET method.
Click on GET Radio button and append the URI with the ID you want to search account for.
Example: /services/apexrest/AccountRest/0012800001HfIsZAAV 
Replace id in green with your org's account record id. 

Press execute to see the result. This would hit the getAccById method of our class and return the matching account name. 
See the snapshot below: 


Salesforce Rest Explorer

Let me know if you face any issue through comments
  

About Saurabh Dua

6 comments:

  1. Overwhelming and disillusioning information can be found concerning this matter here profile worth to see it. https://vograce.com

    ReplyDelete
  2. Your content is nothing short of brilliant in many ways. I think this is engaging and eye-opening material. Thank you so much for caring about your content and your readers. check this out

    ReplyDelete
  3. I would like to say that this blog really convinced me to do it! Thanks, very good post. Kanados viza internetu

    ReplyDelete
  4. Excellent website! I adore how it is easy on my eyes it is. I am questioning how I might be notified whenever a new post has been made. Looking for more new updates. Have a great day! iarratas ar víosa india

    ReplyDelete
  5. Excellent website! I adore how it is easy on my eyes it is. I am questioning how I might be notified whenever a new post has been made. Looking for more new updates. Have a great day! visa india en línea

    ReplyDelete
  6. I discovered your blog post site online and check a few of your early posts. Always keep within the really good operate. I additional up your RSS feed to my MSN News Reader. Looking for forward to reading more from you later on!
    visa canadien

    ReplyDelete

Powered by Blogger.