Skip to main content

Salesforce : Quick Tips: Mass Upload Roles

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


Firstly, as an approach of user onboarding we followed following steps:

  1. Created a list (Excel file) of users to be onboarded with their details and reporting. File contains at minimum following columns
    • Name
    • Employee id (or any other unique reference for employee)
    • Manager Id (same type of value as Employee id)
  2. Add a column Role Name. Defined role names based on pre-determined nomenclature. [Important: We ensured that the role name of an employee is derived from manager role for e.g. reportee of VP is VP - Sales Manager]
  3. Add another column Manager Role Name. Using VLOOKUP we derived the role of reporting manager for an employee (using manager employee id column as reference)


Now, with this data as starting point, we will perform following steps to mass generate roles
  1. Copy code from Gist (mentioned below) and create class RoleGenerator (using this code) within your developer org.
  2. Edit line 15 in RoleGenerator class and replace 'CEO' with your top role (under which new hierarchy is to be created)
  3. 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 corrected
    mapRoleNames.put('VP - Director 1', 'VP');
  4. Now, with this, apply this formula to all rows
  5. Copy all values in column Code and replace lines 13-17 in RoleGenerator class (created in step 1)
  6. Your class should look similar to the original code, replaced with your roles. If there are any syntax errors, please fix it yourself


  1. 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();
  2. Verify script runs without errors and roles are created successfully


On execution script, there were few issues that I faced:
  1. Role limit - we already had lot of roles, so had to purge some roles to make space
  2. 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
  3. 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


Popular posts from this blog

Quick Tips: Salesforce default Images

Well, I'm sure a lot of you still rely on using out of the box salesforce images for displaying quick icons within formula fields or even using them within your Visualforce pages. Lately, I realized that a lot of earlier resources are no longer accessible, so I tried to quickly extract all images from Salesforce CSS files and provide a quick reference here. Please note, I've referenced all images from SF servers directly, so if anything changes, the image should stop rendering here. As these images are completely controlled by Salesforce, and in case they change anything, it might lead to image not being accessible. Image path Image /img/samples/flag_green.gif /img/samples/flag_green.gif /img/samples/flag_red.gif /img/samples/color_red.gif /img/samples/color_yellow.gif /img/samples/color_green.gif /img/samples/light_green.gif /img/samples/light_yellow.gif /img/samples/light_red.gif /img/samples/stars_100.gif /img/samples/stars_200.gif /img/samples/stars_300.

Lightning: Generate PDF from Lightning components with in-memory data

I'm sure as everyone is diving into lightning components development, they are getting acquainted with the nuances of the Lightning components framework. As well as, its current limitations. Being a new framework, this is bound to happen. Although we have our users still using salesforce classic, we have started using lightning components framework our primary development platform and Visualforce is considered primarily for rendering lightning components within Classic Service console. Recently, while re-architecting a critical module, we encountered a problem wherein we needed to generate PDF from lightning components. Now, being Javascript intensive framework, it has limited room for such features (may be included in future roadmap). As of now, there is no native feature within the lightning framework to do so (at least I didn't find anything). Common Scenario - Create Visualforce page to retrieve data and generate PDF For scenarios where the data exist within Sa

Lightning: Generate PDF within Lightning Experience with Salesforce Data

Some time back I posted a solution to generate PDF from Lightning components using in-memory data. Post url: It was developed for a specific scenario, wherein we need to generate PDF where: User interface is Salesforce classic Initiated via Lightning Component Data doesn't exist within Salesforce and is completely in-memory As complex and tricky this situation was, we did end up finding a stable and equally tricky solution. However, I realize that there are still lack of solutions (or maybe my search skills are downgrading) to generate and automatically download PDF document from Lightning Experience, without using any lightning components, wherein data exists within Salesforce. You can use the earlier solution in that case, but it will be an overkill. There are various solutions available to generate PDF from javascript. But, I still think the plain old method of converting HTML to PDF (via