Quantcast
Channel: Thoughts From a Cloudy Mind
Viewing all articles
Browse latest Browse all 8

Trigger to Deactivate Users

$
0
0

Kevin O’Hara posed a question on twitter:

Elizabeth Davidson liked the idea and posed another question:

After some back and forth on this topic Kevin mentioned:

So I decided to implement it.  Feel free to steal the code below.  The code does assume two three things.

  1. You have a profile called ‘DEACTIVATED’ – I made this the most barebones (ie useless) profile I could
  2. You have a field called Notes (Notes__c)
  3. You have an available license to create the user in the test code [Updated: Thanks Jay!]

The trigger simply looks for a user update and checks if the Profile has been changed to DEACTIVATED.  If so, it will set the users IsActive flag to false and add the name of the old profile to Notes__c. If you do this from the UI (rather than, say, the Data Loader) you will be redirected to the “Remove user from all Opportunities, etc.” screen.

So here is what I have

The trigger [updated, there was a typo due to copy-paste; Thanks Kevin!]:

trigger DeactivatedUser on User (before update) {

    //We need the Id of our 'DEACTIVATED' profile
    List<Profile> profileList = [select id from Profile where name = 'DEACTIVATED' limit 1];

    //If the Deactivated profile exists, process records
    if(!profileList.IsEmpty()){
        Id deactivatedProfile = profileList[0].Id;

        //Create a map of all profiles.  We will need this to add the old profile name
        //to the Notes__c field
        Map<Id, Profile> profileMap = new Map<Id, Profile>();
        profileMap.putAll([select Id, Name from Profile]);

        //Loop through users in trigger and process accordingly
        for(Integer i=0; i<Trigger.new.size(); i++){
            User u = Trigger.new[i];
            //process user if profile is the DEACTIVATED profile
            if(u.ProfileId == deactivatedProfile){
                u.IsActive = false;
                //set the Notes__c field accordingly
                if(trigger.old[i].ProfileId <> trigger.new[i].ProfileId){
                    if(u.Notes__c <> null)
                        u.Notes__c += '--Old Profile: ' + profileMap.get(Trigger.old[i].ProfileId).Name;
                    else
                        u.Notes__c = 'Old Profile: ' + profileMap.get(Trigger.old[i].ProfileId).Name;
                }
            }
        }
    }
}

And the test Class:

@isTest
private class testDeactivatedUser {

    static testMethod void myUnitTest() {
        Id initialProfile =  [Select Id from Profile where Name != 'DEACTIVATED' limit 1].Id;
        Id deactivatedProfile =  [Select Id from Profile where Name = 'DEACTIVATED'limit 1].Id;
        //Create user
        User u = new User(LastName='TestUser',
                          Alias='testAlia',
                          Email='someone@email.com',
                          Username='someone@email.com.dev',
                          CommunityNickname='some.one',
                          profileId=initialProfile,
                          TimeZoneSidKey='America/New_York',
                          LocaleSidKey='en_Us',
                          EmailEncodingKey='ISO-8859-1',
                          LanguageLocaleKey='en_US');
        insert u;
        //update user profile to deactivated we expect the user to be deactivated and
        //the old profile name to be added to the notes
        u.profileId=deactivatedProfile;
        update u;
        //Query for the user details
        u = [select Id, IsActive, Notes__c from User where id = :u.id];
        //verify that user is inactive and notes are not null
        System.assertEquals(false, u.IsActive);
        System.assertNotEquals(null, u.Notes__c);

        //test that the Notes aren't updated when the profile isn't changed
        Integer notesLength = u.Notes__c.length();
        update u;
        u = [select Id, IsActive, Notes__c from User where id = :u.id];
        System.assertEquals(notesLength, u.Notes__c.length());

        //set profile back to initial profile so we can trigger the deactive one more time
        //and test that the notes are updated when notes already exist
        u.ProfileId=initialProfile;
        u.IsActive = true;
        update u;
        //deactivate user again
        u.ProfileId=deactivatedProfile;
        update u;
        u = [select Id, IsActive, Notes__c from User where id = :u.id];
        System.assert(notesLength < u.Notes__c.length());
    }
}


Viewing all articles
Browse latest Browse all 8

Trending Articles