Cleaning all Internal user Roles

In my Current Project, I'm working on Role-Hierarchy automation, as part of testing, I wanted to do below 2 activities frequently:

1. Set User Role to blank.

2. Delete Role-Hierarchy. 

Below class will address the above two activities:

public class CleanRoles
{    
    public static void cleanData()
    {
        List<User> updateUsersWithBlankRole = new List<User>();
        for(User usr: [Select Id,UserRoleId From User where LastModifiedDate=Today])
        {
            usr.UserRoleId = null;
            updateUsersWithBlankRole.add(usr);
        } 
        Update updateUsersWithBlankRole;
        List<UserRole> DeleteRoles = [SELECT Id FROM UserRole where LastmodifiedDate=today order by LastModifiedDate Desc];
        Delete DeleteRoles;        
    }
}

Invoke the above logic from anonymous block with the below code snippet:
CleanRoles.cleanData();

tHiNk gooD and dO thE bEsT.........MANJU NATH 🌝

Comments