Recently tasked with a mundane coding activity to create security roles based on a bunch of standard roles, i came up with a simple x++ code to help me in this activity or maybe just something to keep me awake!!
What you are trying to achieve in this code is to take all the duties that are part of a set of standard security roles and add those duties to a new security role you created. This role then becomes the starting point for further refinement.
All you need to change in this job is the calls to addDutiesFromSrcToDst method. The number of calls you will make is equal to the number of source roles and the arguments point to the source role name and destination role name. Also the new role should already exist, so just create a new role and supply it as argument.
If you want to know how to figure out the AOT name of the role based on a label name of the role then you can use the below piece of code to figure that out.
SecurityRole securityRole; while select * from securityRole order by Name { info(strFmt('Name: %1, AOT Name: %2', securityRole.Name, securityRole.AotName)); }
Here is the code to merge the source role duties and add them to the new role duties node.
static void CreateNewRole(Args _args) { #AOT int counter, counter1; boolean dutyExistsInNewRole; TreeNode securityRoleDutiesNode; str sourceSecRole1_DutyPath; int sourceSecRole1_DutyCounter; TreeNode newSecurityRoleDutiesNode; str newSecRole_DutyPath; int newSecRole_DutyCounter; TreeNode sourceSecRole1_DutyNode; TreeNode newSecRole_DutyNode; str sourceDutyName; str newSecDutyName; TreeNode newlyAddedDuty; void addDutiesFromSrcToDst(str _srcPath, str _destPath) { // Initialize first security role to copy duties from sourceSecRole1_DutyPath = #SecRolesPath + _srcPath; securityRoleDutiesNode = TreeNode::findNode(sourceSecRole1_DutyPath); sourceSecRole1_DutyCounter = securityRoleDutiesNode.AOTchildNodeCount(); sourceSecRole1_DutyNode = securityRoleDutiesNode.AOTfirstChild(); for(counter = 1; counter<=sourceSecRole1_DutyCounter; counter++) { dutyExistsInNewRole = false; sourceDutyName = sourceSecRole1_DutyNode.AOTname(); // Initialize the new security role\duties node newSecRole_DutyPath = #SecRolesPath + _destPath; newSecurityRoleDutiesNode = TreeNode::findNode(newSecRole_DutyPath); newSecRole_DutyCounter = newSecurityRoleDutiesNode.AOTchildNodeCount(); newSecRole_DutyNode = newSecurityRoleDutiesNode.AOTfirstChild(); for(counter1 = 1; counter1<=newSecRole_DutyCounter; counter1++) { newSecDutyName = newSecRole_DutyNode.AOTname(); if(sourceDutyName == newSecDutyName) { dutyExistsInNewRole = true; break; } newSecRole_DutyNode = newSecRole_DutyNode.AOTnextSibling(); } if(!dutyExistsInNewRole) { // Add this duty to the new role's duties node newlyAddedDuty = newSecurityRoleDutiesNode.AOTadd(sourceDutyName); newlyAddedDuty.AOTsetProperty('Name', sourceDutyName); newlyAddedDuty.AOTsave(); newSecurityRoleDutiesNode.AOTsave(); //sourceSecRole1_DutyNode.AOTinsert(newSecurityRoleDutiesNode, newSecurityRoleDutiesNode, true); dutyExistsInNewRole = false; } //info(strFmt('%1, %2', counter, sourceDutyName)); sourceSecRole1_DutyNode = sourceSecRole1_DutyNode.AOTnextSibling(); } } ; addDutiesFromSrcToDst('\\InventMaterialsManager\\Duties', '\\NewRole\\Duties'); addDutiesFromSrcToDst('\\CRMMarketingCoordinator\\Duties', '\\NewRole\\Duties'); addDutiesFromSrcToDst('\\RouteProcessEngineeringManager\\Duties', '\\NewRole\\Duties'); addDutiesFromSrcToDst('\\ProdProductionSupervisor\\Duties', '\\NewRole\\Duties'); addDutiesFromSrcToDst('\\SMAFieldServiceTechnician\\Duties', '\\NewRole\\Duties'); }
![](http://pixel.wp.com/b.gif?host=vgrandhi.wordpress.com&blog=17329156&post=413&subd=vgrandhi&ref=&feed=1)