Created
August 6, 2021 17:28
-
-
Save rygramer/f2cfefd365e6ff5a4799c9e4b1be708e to your computer and use it in GitHub Desktop.
Pass the record you would like to check, and we will update the record with the fallback User if the original owner is inactive (returning only the active User).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @Name RecordOwnerVerification | |
| * @Author Kicksaw Consulting, Ryan Mercer | |
| * @Date 2021-06-14 | |
| * @Description Pass the record you would like to check, and we will update the record with the fallback User if the original owner is inactive (returning only the active User). | |
| */ | |
| global class RecordOwnerVerification{ | |
| /** | |
| * Main method doing the heavy lifting | |
| */ | |
| @InvocableMethod( | |
| label = 'Verify the Record Owner is Active' | |
| description = 'Pass the records you would like to check, and we will update the record with the fallback User if the original owner is inactive (returning only the active User).' | |
| category= 'Kicksaw' | |
| ) | |
| global static List<OutputParameters> isOwnerActive(List<InputParameters> inputs){ | |
| //Retrieve the owners of the input records and establish the map. | |
| Set<Id> userIdSet = new Set<Id>(); | |
| for(InputParameters input : inputs){ | |
| userIdSet.add((Id)input.recordToCheck.get('OwnerId')); | |
| } | |
| Map<Id,User> userMap = new Map<Id,User>([SELECT Id, IsActive FROM User WHERE Id IN :userIdSet]); | |
| //Retrieve the fallback user in case the record's owner is inactive. | |
| User fallbackUser = [SELECT Id | |
| FROM User | |
| WHERE Automation_Fallback_Owner__c = TRUE | |
| AND IsActive = TRUE | |
| ORDER BY CreatedDate Desc | |
| LIMIT 1]; | |
| //The method's main logic. | |
| Set<sObject> recordSetToUpdate = new Set<sObject>(); | |
| List<OutputParameters> outputs = new List<OutputParameters>(); | |
| for(InputParameters input : inputs){ | |
| OutputParameters output = new OutputParameters(); | |
| User currentRecordOwner = userMap.get((ID)input.recordToCheck.get('OwnerId')); | |
| //Is the current owner active? | |
| if(currentRecordOwner.IsActive){ | |
| //If yes, add that user to the return list. | |
| output.verifiedActiveUser = currentRecordOwner; | |
| }else{ | |
| //If no, add the record and fallback user to the update list and return list. | |
| sObject recordToUpdate = input.recordToCheck.Id.getSObjectType().newSObject(input.recordToCheck.Id); | |
| recordToUpdate.put('OwnerId',fallbackUser.Id); | |
| recordToUpdate.put('Id',input.recordToCheck.Id); | |
| recordSetToUpdate.add(recordToUpdate); | |
| output.verifiedActiveUser = fallbackUser; | |
| } | |
| outputs.add(output); | |
| } | |
| //If the record owner is inactive, update the record with the fallback user. | |
| List<sObject> recordListToUpdate = new List<sObject>(recordSetToUpdate); | |
| if(!recordListToUpdate.isEmpty()){ | |
| List<Database.SaveResult> updateResults = Database.update(recordListToUpdate, false); | |
| for(Database.SaveResult result : updateResults){ | |
| if(!result.isSuccess() && !result.getErrors().isEmpty()){ | |
| Database.Error error = result.getErrors().get(0); | |
| //The debug log will note any errors encountered when updating the record with the fallback user. | |
| system.debug(error.getMessage()); | |
| } | |
| } | |
| } | |
| //Return the active user. | |
| return outputs; | |
| } | |
| /** | |
| * Wrapper class for input parameters | |
| */ | |
| global class InputParameters { | |
| @InvocableVariable(label='Record to Check (single)' required=TRUE) | |
| global SObject recordToCheck; | |
| } | |
| /** | |
| * Wrapper class for output parameters | |
| */ | |
| global class OutputParameters { | |
| @InvocableVariable(label='Verified Active User (single)') | |
| global User verifiedActiveUser; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment