---
schema_version: "1"
id: a435b3c1b2d5a8ada9fcbd8d39cec7ede67f92f9455cc4e8e6e98844ae105270
title: Use Apex to Create a Callout to an External Service
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/platform/264-0-0/external_services_apex_create_a_callout.md
canonical_url: https://help.salesforce.com/s/articleView?id=platform.external_services_apex_create_a_callout.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: d6d5960579969ab6a99edaa1258c6c43f09817e1bd283c3b205abbca55f897bb
description: Design and test the automation that sends a user’s information from Salesforce to the external employee banking system. Using the examples provided, create the variables for user phone numbers, and combine different phone numbers into one piece of data. Then use the external service action to create the user.
products:
  - Salesforce Platform
  - Security
release: "264"
---
# Use Apex to Create a Callout to an External Service

Design and test the automation that sends a user’s information from Salesforce to the external employee banking system. Using the examples provided, create the variables for user phone numbers, and combine different phone numbers into one piece of data. Then use the external service action to create the user.

### Required Editions

<table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small edition"><colgroup><col></colgroup><tbody><tr class=""><td class="slds-cell-wrap" style="vertical-align:top;">Available in: Lightning Experience</td></tr><tr class=""><td class="slds-cell-wrap" style="vertical-align:top;">Available in: <strong>Enterprise</strong>, <strong>Performance</strong>, <strong>Unlimited</strong>, and <strong>Developer</strong> Editions</td></tr></tbody></table>

1. Define a named credential
    
    For this simplified example, use "legacy" Named Credentials. Instead of clicking New, select **New Legacy** from the dropdown menu next to the New button. For the named credential that your org uses to access the banking system, assign the Bank label. Assign a placeholder URL, such as https://api.example.com. Use example.com because you _paste in_ the schema at registration time, instead of using a URL to point to an API spec.
    
    In the case where you use a URL, the named credential URL corresponds to the declared host of the API spec and one of its transfer protocol schemes or URL protocols. The URL can point to a different endpoint as long as it hosts the same external service. The base path is added to the named credential URL on callout.
2. Register the employee banking system’s external service, using the steps described in [Register an External Service](https://help.salesforce.com/s/articleView?id=platform.external_services_register.htm&release=264.0.0&type=5 "Registering an external service takes only a few steps and no code.") as a guide. Use the BankService name and the Bank named credential, and then copy the schema from the “External Services API Spec Example 2”, found in the topic [External Services OpenAPI 2.0 Schema](https://help.salesforce.com/s/articleView?id=platform.external_services_schema_examples.htm&release=264.0.0&type=5 "This section features External Services OpenAPI 2.0 schema examples.").
3. Create the Apex class `BankService`:
    
    ```
    
                public class BankService {
    }
              
    ```
4. To store the user details, add a member variable:
    
    ```
    public class BankService {
     private ExternalService.BankService_z0User user;
    }
    ```
5. To initialize an empty user with an empty list of phones, add a method:
    
    ```
    public class BankService {
        private ExternalService.BankService_z0User user;
    
     private void setEmptyUser() {
            if (this.user == null) {
                this.user = new ExternalService.BankService_z0User();
                this.user.phones = new List<ExternalService.BankService_Phone>();
            }
        }
    }
    ```
6. To assign the _work_ phone value, add a method:
    
    ```
    public class BankService {
        private ExternalService.BankService_z0User user;
    
        public void setWorkPhone(String phoneNumber) {
            setEmptyUser();
            ExternalService.BankService_Phone phone = 
                new ExternalService.BankService_Phone();
            phone.phone = phoneNumber;
            phone.typeofphone = 'Work';
            this.user.phones.add(phone);
        }
    
        private void setEmptyUser() {
            if (this.user == null) {
                this.user = new ExternalService.BankService_z0User();
                this.user.phones = new List<ExternalService.BankService_Phone>();
            }
        }
    }
    ```
7. To assign the _cell_ phone value, add a method:
    
    ```
    public class BankService {
        private ExternalService.BankService_z0User user;
    
        public void setWorkPhone(String phoneNumber) {
            setEmptyUser();
            ExternalService.BankService_Phone phone = 
                new ExternalService.BankService_Phone();
            phone.phone = phoneNumber;
            phone.typeofphone = 'Work';
            this.user.phones.add(phone);
        }
    
     public void setCellPhone(String phoneNumber) {
            setEmptyUser();
            ExternalService.BankService_Phone phone = 
                new ExternalService.BankService_Phone();
            phone.phone = phoneNumber;
            phone.typeofphone = 'Cell';
            this.user.phones.add(phone);
        }
    
        private void setEmptyUser() {
            if (this.user == null) {
                this.user = new ExternalService.BankService_z0User();
                this.user.phones = new List<ExternalService.BankService_Phone>();
            }
        }
    }
    ```
8. To assign a user, add a method:
    
    ```
    public class BankService {
        private ExternalService.BankService_z0User user;
    
     public void setUser(Integer userId, String userName) {
            setEmptyUser();
            this.user.id = userId;
            this.user.name = userName;
        }
    
        public void setWorkPhone(String phoneNumber) {
            setEmptyUser();
            ExternalService.BankService_Phone phone = 
                new ExternalService.BankService_Phone();
            phone.phone = phoneNumber;
            phone.typeofphone = 'Work';
            this.user.phones.add(phone);
        }
    
        public void setCellPhone(String phoneNumber) {
            setEmptyUser();
            ExternalService.BankService_Phone phone = 
                new ExternalService.BankService_Phone();
            phone.phone = phoneNumber;
            phone.typeofphone = 'Cell';
            this.user.phones.add(phone);
        }
    
        private void setEmptyUser() {
            if (this.user == null) {
                this.user = new ExternalService.BankService_z0User();
                this.user.phones = new List<ExternalService.BankService_Phone>();
            }
        }
    }
    ```
9. Create a custom exception class, since you can’t throw built-in Apex exceptions.
    
    ```
    Public class BankServiceException extends Exception{}
    ```
    
    For more information, see [Create Custom Exceptions](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_exception_custom.htm) in _Apex Developer Guide_.
10. To create users on the external bank system, add a method to call the external BankService:
    
    ```
    public class BankService {
        private ExternalService.BankService_z0User user;
    
        public void createUser() {
           if (this.user == null || this.user.id == null) {
               throw new BankServiceException('Set the user to create first');
           }  
    
           ExternalService.BankService bankService = new ExternalService.BankService();
           try {
               ExternalService.BankService.postUsers_Request request = new ExternalService.BankService.postUsers_Request();
               request.z0user = this.user;
               bankService.postUsers(request); 
           } catch (ExternalService.BankService.postUsers_ResponseException e) {
               throw new BankServiceException('Couldn\'t create user with ID: ' + this.user.id);
           }
       }
        public void setUser(Integer userId, String userName) {
            setEmptyUser();
            this.user.id = userId;
            this.user.name = userName;
        }
    
        public void setWorkPhone(String phoneNumber) {
            setEmptyUser();
            ExternalService.BankService_Phone phone = 
                new ExternalService.BankService_Phone();
            phone.phone = phoneNumber;
            phone.typeofphone = 'Work';
            this.user.phones.add(phone);
        }
    
        public void setCellPhone(String phoneNumber) {
            setEmptyUser();
            ExternalService.BankService_Phone phone = 
                new ExternalService.BankService_Phone();
            phone.phone = phoneNumber;
            phone.typeofphone = 'Cell';
            this.user.phones.add(phone);
        }
    
        private void setEmptyUser() {
            if (this.user == null) {
                this.user = new ExternalService.BankService_z0User();
                this.user.phones = new List<ExternalService.BankService_Phone>();
            }
        }
    }
    ```
11. Open the developer console and debug by calling the `BankService` with some sample values with a code snippet in an anonymous execution class. Have the debug logs open and compare with this sample callout log:
    
    ```
    BankService bankService = new BankService();
    bankService.setWorkPhone('1234567890');
    bankService.setCellPhone('0987654321');
    bankService.setUser(1234, 'Maria');
    bankService.createUser();
    ```