Recently, I was working on a project, wherein the organization was undergoing complete restructuring (merger, re-organization) and added to that re-vamp of CRM to improve Sales operations. As an integral part of data security. There was a need to review and redesign role hierarchy. Long story short, we ended up with a large list of roles (400+).
We could have assigned someone to manually create these roles, but due to obvious reasons, I wanted to get this handled via script. I want to share a quick code snippet which can help you generate roles en-masse, without having to put in boring and gruesome hours of manually creating roles.
[Important: This solution will work when you have unique role names]
Create the top level role manually at desired hierarchy level. For e.g. in our sample script, we used "CEO" role to build new hierarchy under CEO role
DATA PREPARATIONFirstly, as an approach of user onboarding we followed following steps:
- Created a list (Excel file) of users to be onboarded with their details and reporting. File contains at minimum following columns
- Employee id (or any other unique reference for employee)
- Manager Id (same type of value as Employee id)
Now, with this data as starting point, we will perform following steps to mass generate roles
- Copy code from Gist (mentioned below) and create class RoleGenerator (using this code) within your developer org.
- Edit line 15 in RoleGenerator class and replace 'CEO' with your top role (under which new hierarchy is to be created)
- Within the data file created in DATA PREPARATION, add a column Code in excel file to generate code with formula template mapRoleNames.put(
for e.g. assuming your excel sheet has column D as Role name and column E as Manager Role name, formula can be:
="mapRoleNames.put('" & D2 & "','" & E2 & "');"
This should generate the value in the column, for e.g. If it doesn't, your formula needs to be correctedmapRoleNames.put('VP - Director 1', 'VP');
- Now, with this, apply this formula to all rows
- Copy all values in column Code and replace lines 13-17 in RoleGenerator class (created in step 1)
- Your class should look similar to the original code, replaced with your roles. If there are any syntax errors, please fix it yourself
- Run this script in your developer org using anonymous apex (I couldn't run the code directly in anonymous block within Developer Console due to known reasons)RoleGenerator gen = new RoleGenerator();gen.generateRoles();
- Verify script runs without errors and roles are created successfully
On execution script, there were few issues that I faced:
- Role limit - we already had lot of roles, so had to purge some roles to make space
- Cyclic error - some records were incorrectly setup wherein the employee role and manager role had same value, causing failure for role creation (cyclic reference). This was fixed manually
- Special characters - if you are using special characters within role name, please make sure the code handles replaces those special characters correctly, as Salesforce doesn't allow special character in Role's DeveloperName field
Post a Comment