Vinod Sebastian – B.Tech, M.Com, PGCBM, PGCPM, PGDBIO

Hi I'm a Web Architect by Profession and an Artist by nature. I love empowering People, aligning to Processes and delivering Projects.

Advertisements




Salesforce: Creating SOAP Web Service

Overview

In Salesforce, you can create SOAP (Simple Object Access Protocol) web services using Apex classes. SOAP web services allow external applications to interact with Salesforce by calling exposed methods via SOAP requests. This guide will walk you through the steps to create a SOAP web service in Salesforce.

Steps to Create a SOAP Web Service in Salesforce

1. Define a Global Apex Class

To create a SOAP web service in Salesforce, you need to define a global Apex class with methods that you want to expose to external systems.

global class AccountService {
    // Expose a method to create an Account
    webService static Id createAccount(String name, String phone) {
        Account acc = new Account(Name = name, Phone = phone);
        insert acc;
        return acc.Id;
    }

    // Expose a method to fetch Account details
    webService static Account getAccount(Id accountId) {
        return [SELECT Id, Name, Phone FROM Account WHERE Id = :accountId LIMIT 1];
    }
}

Explanation:

  • Use the global access modifier to make the class accessible outside Salesforce.
  • Use the webService keyword to expose methods as web services.

2. Generate the WSDL

WSDL (Web Services Description Language) is a standard XML format for describing web services and how to access them. In Salesforce, you can generate the WSDL for your web service.

Steps to generate the WSDL:

  1. Go to Setup.
  2. Navigate to Apex Classes.
  3. Click on your class.
  4. Click on “Generate WSDL”.

Download the generated WSDL file. External systems use this file to understand and interact with your SOAP web service.

3. Consume the Service

External applications that want to interact with your SOAP web service need to import the WSDL into their development environment, such as Java or .NET. They can then make SOAP requests to call the exposed methods like createAccount and getAccount.

4. Example SOAP Request

An example SOAP request to create an Account might look like:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:acc="http://soap.sforce.com/schemas/class/AccountService">
   <soapenv:Header/>
   <soapenv:Body>
      <acc:createAccount>
         <name>Test Account</name>
         <phone>1234567890</phone>
      </acc:createAccount>
   </soapenv:Body>
</soapenv:Envelope>

5. Authentication

External clients interacting with your Salesforce SOAP web service must authenticate themselves. This can be done through the login() SOAP call or OAuth. After successful authentication, the session ID is passed in the SOAP header (SessionHeader) for subsequent calls to maintain a secure connection.

Key Concepts

Here are some key concepts to remember when creating SOAP web services in Salesforce:

  • global keyword: It makes the class and methods visible outside Salesforce.
  • webService keyword: Marks methods as callable via SOAP.
  • WSDL file: Acts as a contract between Salesforce and external systems.
  • Session ID: Ensures secure communication between Salesforce and external systems.
  • Error Handling: Exceptions thrown in Apex are reflected in the SOAP fault response.

Conclusion

Creating SOAP web services in Salesforce using Apex classes allows you to integrate external systems with your Salesforce instance. By following the steps outlined in this guide, you can expose custom functionality or CRUD operations as SOAP methods, enabling seamless interaction between Salesforce and external applications.

Sources: Salesforce Developer Guide sample code