Sep 25, 2012

Creating Public Web-Service in Salesforce

We all are familiar with web services and we often use them. Now let's talk about other side of using web services by making them public so I don't need any authentication/passwords/SessionIds etc.

Recently I need to create a web service so it can used in a mobile app. Most of us know about Mobile SDK, REST, oAuth etc.. but in all I need to intake the credentials of salesforce organization to get access token or if am using web services then to get the session Id. But think about it, why end user will be concerned with the back-end/database? So all we need is a public web service which can be called to process our request.

So follow these simple steps:

1) Create a web service like this

global class DemoClass
{
    webService static string Method()
    {
        return 'DoSomething' ;
    }
}

2) Go to Site > Setup > App Setup > Develop > Sites > Select your Site > Give access of class "DemoClass" to site profile

3) Extract WSDL of your class, go to your class and then click "Generate WSDL". Now all we need to change the SOAP address.

Lets say, this is a snippet of my WSDL



Now we need to change the highlighted code like this:

This is the location:
https://ap1-api.salesforce.com/services/Soap/class/ankit/DemoClass

And our site URL is:

So our final location will be:
http://ankitarorasite-developer-edition.ap1.force.com/services/Soap/class/ankit/DemoClass

Now you can use this location in your WSDL and there will be no need of getting any access tokens or session Ids. Happy Coding.