Skip to content

Instantly share code, notes, and snippets.

@toanshulverma
Last active February 15, 2020 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toanshulverma/52001a0b377c9119188cd0bcd3c02e57 to your computer and use it in GitHub Desktop.
Save toanshulverma/52001a0b377c9119188cd0bcd3c02e57 to your computer and use it in GitHub Desktop.
Salesforce - Mass generate roles within Salesforce
/*
DESCRIPTION: Genreate roles in Salesforce in bulk. Save your sweat
USAGE: For details go to https://www.vermanshul.com/2020/02/salesforce-quick-tips-mass-upload-roles.html
*/
public with sharing class RoleGenerator {
String topRole;
Map<String, String> mapRoleNames = new Map<String, String>();
public RoleGenerator(){
// Add the top level role manually within your instance and add it's name here
topRole = 'CEO';
// EDIT HERE: Replace lines 13-17 with your CODE Column data
mapRoleNames.put('VP', topRole);
mapRoleNames.put('VP - Director 1', 'VP');
mapRoleNames.put('VP - Director 2', 'VP');
mapRoleNames.put('VP - Director 1 - Manager 1', 'VP - Director 1');
mapRoleNames.put('VP - Director 1 - Manager 2', 'VP - Director 1');
}
// Generate roles and associate roles in given hierarchy
public void generateRoles(){
UserRole parentRole = [select id, name from UserRole where name = :topRole];
//GENERATE ROLE RECORDS
Map<String, UserRole> mapRoleObj = new Map<String, UserRole>();
for(String roleName :mapRoleNames.keyset()){
UserRole newRole = new UserRole ();
newRole.Name = roleName;
newRole.Developername = roleName.replace('/','').replace('+','').replace('-', '_').replace('"', '').replace('/', '').replace('\'', '').replace('(', '').replace(')','').replace(' ','').replace('__','_').trim();
newRole.ParentRoleId = parentRole.id;
mapRoleObj.put(roleName, newRole);
}
// INSERT ROLES
insert mapRoleObj.values();
mapRoleObj.put('Merchandising - Top Level',parentRole);
//ASSOCIATE ROLES WITH PARENT
for(UserRole rl : mapRoleObj.values()){
String parentRoleName = mapRoleNames.get(rl.Name);
if(mapRoleObj.containsKey(parentRoleName)) rl.ParentRoleId = mapRoleObj.get(parentRoleName).id;
}
update mapRoleObj.values();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment