# How do developers expose custom Apex business logic as a SOAP web service for legacy enterprise consumers?

**Topic:** Integration · **Difficulty:** Medium · **Tags:** Integration, Apex SOAP, WSDL Generation

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-developers-expose-custom-apex-business-logic-as-a-soap-web-service-for-legacy-enterprise-consumers

## Answer

**Basics:** Apex SOAP services serve legacy systems that only speak SOAP/WSDL. You write ordinary Apex, add the `webservice` keyword to a static method, and Salesforce creates the SOAP operation and contract. Newer integrations should prefer Apex REST, but SOAP remains valid for legacy consumers.

**How it works:**
1. Write `global with sharing class OrderService` and a `webservice static` method.
2. Open Setup > Apex Classes > the class > **Generate WSDL** (the button appears for classes with webservice methods).
3. Give the consuming system the WSDL; it generates client stubs.
4. The client authenticates (OAuth access token or session ID) and sends it in a `SessionHeader`, then posts to `/services/Soap/class/<ClassName>`.
5. Salesforce runs the method and returns an XML response.
6. Grant the integration user access to the Apex class.

**Advanced / deep dive:** Method parameters and return types are limited to types SOAP can describe: primitives, sObjects, lists, and user-defined global inner classes with `webservice` fields. Maps and Sets are not allowed in the signature and methods cannot be overloaded, so design with List wrappers. The same Apex governors apply; there is no extra bulk behavior, so accept `List<>` input to let callers batch and avoid per-record calls. Sharing is controlled by the class keyword; FLS is not enforced automatically for DML in Apex. Because the WSDL is a contract, changing a signature breaks clients; prefer adding new methods or a new class for new versions. You can also consume external SOAP services from Apex using WSDL2Apex (Setup > Apex Classes > Generate from WSDL), which is the reverse direction.

**Code / config example:**
```apex
global with sharing class OrderService {
    global class Result { webservice Boolean success; webservice String message; }

    // Accept a list so the caller can send bulk data
    webservice static List<Result> closeOrders(List<Id> orderIds) {
        List<Result> out = new List<Result>();
        List<Order__c> orders = [SELECT Id, Status__c FROM Order__c WHERE Id IN :orderIds];
        for (Order__c o : orders) { o.Status__c = 'Closed'; }
        for (Database.SaveResult sr : Database.update(orders, false)) {
            Result r = new Result(); r.success = sr.isSuccess();
            if (!sr.isSuccess()) r.message = sr.getErrors()[0].getMessage();
            out.add(r);
        }
        return out;
    }
}
```

**Hidden gotchas & must-remember points:**
- The class must be `global`; the method must be `static`.
- Cannot overload webservice methods; Map and Set are not allowed as parameters.
- The WSDL has to be regenerated and redistributed when signatures change.
- The integration user needs class access; otherwise the call is refused.
- Governor limits apply as normal.
- `webservice` methods cannot be called from a trigger or another web service callout path in some contexts (check docs).

**Best practice:** Use this only when a consumer requires SOAP; design coarse-grained, list-based methods and version through new classes.

---
Source: ForceChallenges · last updated 2026-11-02
