Quickstart: Exposing Service & Support Data to Other Systems using Apex Web Services

Available in: Salesforce Classic

This is part of Developing with the Service Cloud.

Use Case

While salesforce.com's Service & Support is a great system for call centers, it's rarely the only system in place. Often there are other systems within customer environments that need to gain access to the data held within salesforce.com. Unlike older legacy systems, salesforce.com has several avenues for delivering it's data to external systems and applications. One of the easiest ways to expose custom data sets is Apex Web Services.

Background

Support for exposing web services is built into the Apex language. All you have to do is define a static method with the keyword "webService" and that method is now callable over the Internet as a web service. Any parameters to that method are treated as part of the web service request, and the return value of the function is treated as the web service response. Therefore, individual records or sets of records can be easily sent back from a web service, allowing automated data retrieval by another system or application.

By default, code that calls this web service would need to authenticate itself via a session ID obtained via the salesforce.com API. Alternatively, Force.com Sites can be used to expose that web service to the internet in an unauthenticated manner.

Sample Code: Exposing Cases for an Account

The following sample illustrates a simple web service. The service accepts the name of a queue as a string, and returns a collection of Cases that are currently assigned to that queue. This is done by defining a "global" class and a "webService" method.

global class CasesWebService {
	
    webService static Case[] getCasesForQueue(String queueName) {
    	QueueSobject q = [SELECT Id FROM QueueSobject WHERE Queue.Name = :queueName];
    	Case[] cases = [SELECT Id, Subject, Reason, Status FROM Case WHERE OwnerId = :q.Id];
        return cases;
    }
    
}

Further Reading