Subscribe
Exposing your methods as a web service
by Jon Mountjoy on September 3, 2008 at 03:05 AM
I'm writing a small article - an introduction to Apex - and so I'm of course learning a few things about Apex while doing so. I've just learned how to create web services.
I want to write some code in Apex that can be called from some external client. In other words, I want to write a web service end-point that other folk could invoke using their language of choice.
You might think this would be a lot of work - in a previous life I've set up a web services stack, hacking several XML configuration files, simply to expose one end-point. Here, the Force.com Platform does it for me, which is rather nice.
So here's the code:

Needless to say, the section about web services in my article is rather short!
global class MyWebService {
webservice static void createEvent(String mName, Integer howFar){
Mileage__c m = new Mileage__c (name = mName, miles__c = howFar);
m.date__c = Date.today();
Database.insert(m);
}
}
That's all there is to it! This exposes a web service method, createEvent(), which can be invoked from some external client. The method, in turn, creates and persists a Mileage sObject - which I grabbed from the workbook, populating some of the parameters from the incoming web service call.
The only important bits are:
- the webservice keyword, used on the (static) method
- the global access modifier, which declares that the class is visible to all Apex code, everywhere. All classes defining these types of web services are required to so annotated.

Needless to say, the section about web services in my article is rather short!

Comments
Posted by Andreas Hofmann on January 6, 2009 04:03 AM:
How do you handle authentication?
When I want to invoke your web service, I assume that I need to provide some authentication information, right?
Let's say I want to expose a web service for creating an event. Any of my partners out there in the wild should be able to use this web service to create an event for one of my sales reps. Can I expose this web service so my partners don't need to provide any login credentials for invoking the web service (since they should be able to feed in their "desire" to get in touch with my sales reps, and in salesforce I will then implement some code for checking the requests and assigning the events to my sales reps)?