restsalesforceapexapex-trigger

Automate user creation and deletion through external API requests


I have 0 experience in coding in APEX so I would greatly appreciate your help and support with this question!

I would like to figure out a way to automate the deletion of an Aircall User if an SF user is deleted. Let us assume that every SF user has an Aircall ID that is present in their User profiles, stored in a field called 'Aircall ID'. This is what I will need to form the delete request.

I want that when a user is deleted on Salesforce, it triggers a delete request to Aircall sending the value that was previously stored in the Aircall ID field to the specific endpoint in question.

I need help figuring out how to write an APEX trigger that sends the Aircall ID to the class (to be triggered after the user is deleted) and finally how to automatically trigger the execution of this class after the ID has been received in order to complete the User deletion on Aircall's platform.

public class deleteAirCallUser {

    Http http = new Http();
    HttpRequest request = new HttpRequest();
    
    request.setMethod('DELETE');
    
    string encodedCredentials = 'apikey';
    String authorizationHeader = 'Basic ' + encodedCredentials;
    request.setHeader('Content-Type', 'application/json;charset=UTF-8');
    request.setHeader('Authorization', authorizationHeader);
    string AircallUserId = //should be the Aircall userID from the deleted profile
    request.setBody(AircallUserId);
    request.setEndpoint('https://api.aircall.io/v1/users/'+ Aircall userID);
    
    HttpResponse response = http.send(request);
    
    if (response.getStatusCode() == 200) {
       
        
        Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
        
        System.debug(results);}
    
    else{
        
        Map<String, Object> results_2 = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
        
        System.debug(results_2);
        
    }
          }

Thank you for your help!


Solution

  • https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_user.htm

    "You can’t delete a user in the user interface or the API. You can deactivate a user in the user interface; and you can deactivate or disable a Customer Portal or partner portal user in the user interface or the API. Because users can never be deleted, we recommend that you exercise caution when creating them."

    For deactivations you'll need something like this. (It's not written to best practices, ideally the trigger would be "thin" and actual processing offloaded to helper class. Also it assumes you mass update max 10 users at a time because that's the limit of callouts.

    trigger UserTrigger on User (after update){
    
        Set<String> toSend = new Set<String>();
        for(User u : trigger.new){
            User oldUser = trigger.oldMap.get(u.Id);
            // have we deactivated them?
            if(!u.isActive && oldUser.isActive && String.isNotBlank(u.AirCallId__c)){
                toSend.add(u.AirCallId__c);
            }
        }
        if(!toSend.isEmpty()){
            sendAirCallDeletes(toSend);
        }
        
        // This should be in a helper class, it looks bizarre to have functions defined in trigger's body
        @future
        static void sendAirCallDeletes(Set<String> toSend){
            Http http = new Http();
            HttpRequest request = new HttpRequest();
            request.setMethod('DELETE');
            String encodedCredentials = 'apikey';
            String authorizationHeader = 'Basic ' + encodedCredentials;
            request.setHeader('Content-Type', 'application/json;charset=UTF-8');
            request.setHeader('Authorization', authorizationHeader);
            for(String airCallId : toSend){
                request.setBody(airCallId);
                request.setEndpoint('https://api.aircall.io/v1/users/'+ airCallId);
                try{
                    HttpResponse response = http.send(request);
                    System.debug(response.getStatusCode());
                    System.debug(response.getBody());
                    System.debug((Map<String, Object>) JSON.deserializeUntyped(response.getBody());
                } catch(Exception e){
                    System.debug(e);
                }
            }
        }
    }
    

    You might want to read up about "named credentials" (don't store the api keys etc in code), why we need "@future" trick when we want to make callout from a trigger, how to check for limit of calls you can make in single transaction... But should be a start?