Skip to main content

Public RESTful Web Services

Hey Guys,

So this post will focus on how one can invoke any web service class( REST / SOAP ) without using any Authentication/Username & Password.

Of course, people who are working on Public domains will understand why we need to web services without any authentication.

Simple Example:

There is a Data sitting in your org (say SFDC ) which is not harmfull if exposed to any requester. ( Examples of such Public Web services are UBER, google Maps).

IMPORTANT : You must have created your own site.

Now as a demo you will visit my website built on force.com Site and fill in the form.
Once you land on the page, click Rest Example Tab.

Lets hit this.

later you will click this link appending the same Email id which you gave to create the record.
once you click the above link, paste the email id at the end of the URL in the browser.

You just hit a public Restful web-service.

You created a record in someone's anonymous site and checked how many records you actually created there.

The count is what coming from the class.

I will not talk about what you did on the form. But brief you about that.

The form is actually a Page created on Force.com Site and the detail which you entered are stored in one of the object in my salesforce instance. Thats it.

Now about Rest,

It is a simple method :

@RestResource(URLMapping='/publicRestServiceClass')
Global Class publicRestServiceClass{

    @HttpGet
    global static Void invokePublicRestMethod(){
             String userEmail= RestContext.request.params.get('UserEmail');
             List<Public_Site_Record__c> PSRList= [Select Id from Public_Site_Record__c where email__c =:userEmail ];
             Integer countOfRecs = PSRList.size();
               
               RestContext.response.addHeader('Content-Type', 'application/json');           
               RestContext.response.responseBody   = Blob.valueOf('You have total '+countOfRecs+' records in total with the email '+userEmail);           
          
    }

In the Above method, i am expecting only 1 parameter from you  and that is UserEmail.

You can see how that is obtained in the class using RestContext.request.params.get('UserEmail');

RestContext.response.addHeader : If you don't specify the response type, the response will be in XML format

Example: 
<response>
You have total 7 records in total with the email chanz707@gmail.com
</response>

To have this kind of output you need to modify the above class.

  1. Change the return type to String
  2. Create a new variable of String type (Say " String responseString ")and fill in the details what you want to send back to the requester.
  3. finally Return responseString;

RestContext.response.responseBody : This is a Blob Primitive data type. So what ever response you need to send back the requester, you need to send in Blob.

Once the response is retrieved, it will be requester's job to see how to use it.

If you form a Json Object and send the response, the requester will receive a Json String and using Json parser, they can break down the each element in the object.

The final output of this class will be a plain String .


" You have total 7 records in total with the email chanz****@gmail.com "

At this stage, i am assuming that you have already created a site in your org.

Now its time to create a class mentioned above.

Now you need to give the profile to access this class. Lets see how.

Setting -> Sites ->Click on the Site Label -> Click on Public Access Setting Button.
This will open up the Guest profile which requester will be accessing your web service class with.
Go to " Enabled Apex Class Access " section and give the access.


Once the setup is done, its time to check it.

Once you click on the Site Label in the above step, copy the Domain name of your site.


Copy the Domain name and append it with" /services/apexrest/YourResrURLMappingName?UserEmail=AnyParameterYouWantToPassItToYourRestClass"

That is it !! You have just accepted a complete ananymous request and processed the same and sent them back something.

Comments

Popular posts from this blog

Salesforce - Invoke Apex from Visual Flows

This post will brief you about how one can invoke an Apex class from Visual Flows. Important Note: The method which you would like to invoke should be marked as  @InvocableMethod  and the variables which you would like use in and out of Flow, we need to mark them as  @InvocableVariable Best Practices : Make 2 wrapper classes. One for getting the values from Flow to Apex and other wrapper class is to get the values from Apex to Flow. The below sample code explains how you can get a detail in and out of the flow. Use Case : We ask user to enter the Account ID which they want to update and New Name which they want to update on the entered Account ID. Note: Since this is for the demo purpose, we are asking user to enter the Account ID (SFDC 15/18 digit ID ). In Ideal scenario we can invoke the Flow from custom button/links. (  Refer This ) Here we created a Small Visual Flow which has 2 screens ( 1. For asking details from user and other for Showing the success/fa

Salesforce to Salesforce Integration using OAuth 2.0 and Named Credentials

Hello Everyone, After investing lot of time, I was able to Integrate with my own sandbox( you can say one Salesforce Instance to Other Salesforce instance ) I had a scenario where i had to call a rest method created and Exposed in my own sandbox. So I started digging on this as you guys are now. In order to perform OAuth with salesforce you need to perform 3 important steps. 1.        Create Connected App 2.        Create Auth. Provider 3.        Create Named Credentials Once you are authorized, you just need to write 5- 6 lines of code to invoke your REST method. So let’s gets started. As you know  Salesforce uses OAuth 2.0 now. So our job is to set up OAuth. Importantly, OAuth requires 4 important details and they are, Client ID, Secrete Key, User Name and Password. So how do we get them? Client ID and Secrete Key (AKA Client Secrete) can be obtained from Creating a Connected app. But what about User Name and Password?? User Name and Password can

Lightning - Passing value from Parent Component to Child Component

Hey Guys, This post I will try to cover some of the basics of Lightning Event. So if you have reached this post, I am assuming you have got some basic knowledge of Lightning framework. So, In brief Lightning Framework (LF ) works based on component and event driven modal. So when I say component  based, you might have seen that most of the codes are broken in to component in lightning development. You will understand more when you start creating one. There are few scenarios I would like to explain. Scene 1: Parent Component has one child and i want to pass some value from Parent to it's Child. Scene 2:Vice Versa Now .  I want to pass some value from Child Comp to it's parent components. How will you do ? There are 2 ways For Scene 1 and they are Way 1. Passing the value as Attribute Way 2. Sending the values using Lightning Methods. Way 1 is pretty simple. You will create a Attribute inside component 2(Child Comp) and will pass the required va