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

Available in: Lightning Experience
Available in: Enterprise, Performance, Unlimited, and Developer Editions
  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 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.
  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 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();