Atlassian uses cookies to improve your browsing experience, perform analytics and research, and conduct advertising. Accept all cookies to indicate that you agree to our use of cookies on your device. Atlassian cookies and tracking notice, (opens new window)
/
Assign Your User to the Administrator Group (Code)
Assign Your User to the Administrator Group (Code)
This documentation provides a walkthrough demonstrating how to execute our permission-set group user assignment Apex Script via the Developer Console in your Salesforce org.
Admin access with Apex permissions is required to execute Apex Scripts from the Developer Console. Only users with administrative access should perform this activity. Please verify your access with your Salesforce Admin before executing this script.
APPLIES TO | CUNEIFORM FOR CRMFIELD AND DATA MANAGEMENT
Assign Your User to the Admin Group
Assign Your User to the Administrator Permission Set Group
ONE MINUTE
Once the Cuneiform for CRM: Administrators permission-set group is created, we'll associate your user with it, providing you administrative access to Cuneiform for CRM.
About this Apex Script
We’ve created an Anonymous Apex script to assign your user to our Cuneiform for CRM: Administrators permission set group. This permission set group manages access administrative / profiling access to Cuneiform for CRM within your Salesforce org. Associating a user to this permission set enables users to see Cuneiform for CRM in the App Launcher and profile Salesforce Objects.
By adding users to the Cuneiform for CRM: Administrators permission set group, you can give them profiling rights and access to Cuneiform for CRM.
The script will:
Check if the Cuneiform for CRM: Administrators permission set group exists
If it does not exist – it halts execution and asks the user to create the permission-set group
Otherwise – it checks if a permission-set group assignment exists for the current user. If the assignment does not exist, it is created.
The script will provide output to the user explaining the script outcome. You can use this script to assign your user to the Cuneiform for CRM: Administrators permission set group programmatically. Alternatively, you can follow these steps to create the assignment declaratively via Salesforce Setup.
Execute the Anonymous Apex Script
Please copy this script to your clipboard. You can execute it against your Salesforce org via SFDX, your Salesforce IDE (ex. VSCode), or the Developer Console.
We provide guidance on executing this script via the Developer Console below. Open this expand-element to see the Anonymous Apex script – and copy it to your clipboard.
Open this expand-element to see the Anonymous Apex script – and copy it to your clipboard.
// Initialize local variables
String permSetGroupApiName;
List<PermissionSet> adminPermSet;
String adminPermSetApiName;
List<PermissionSetGroup> adminPermSetGroup;
List<PermissionSetAssignment> adminUserAssignment;
Boolean exceptionOccurred;
DmlException caughtException;
Boolean hasPermissionSets;
Boolean hasUser;
// Default constants we use in our script for layout and testing / debugging purposes
Boolean testException = false;
String spacer = ' ';
String bdr = '---------------------------------------------------------------------------------------------------------------------------------';
String testExceptionMessage = 'This is a test exception.';
// Initialize local variables (do not change these values)
adminPermSetApiName = 'Cuneiform_for_CRM_Administrative_User';
permSetGroupApiName = 'Cuneiform_for_CRM_Administrators';
hasPermissionSets = false;
hasUser = false;
System.debug(bdr);
System.debug(spacer + 'Cuneiform for CRM: Field and Data Management: Configuration Apex Script');
System.debug(spacer + '4. Provide User Access to the Administrative Permission-Set Group.');
// First, let's verify that our Administrative Permission-Set exists (verifying we're installed)
adminPermSet = [SELECT Id FROM PermissionSet WHERE Name = :adminPermSetApiName];
// Was a valid Permission Set found?
if (adminPermSet.size() == 0) {
// Output that the permission-set was not found -- and needs to be verified
System.debug(bdr);
System.debug(spacer + 'The Administrative Permission-Set "' + adminPermSetApiName + '" was not found.');
System.debug(spacer + 'Please ensure that Cuneiform for CRM is installed and that you have admin-level access.');
System.debug(spacer);
System.debug(spacer + 'You can install Cuneiform for CRM by opening our appexchange listing at');
System.debug(spacer + 'https://peernova.link/cuneiform/install/fdm/appexchange');
System.debug(spacer + 'Click on the "Get It Now" button to install Cuneiform for CRM: Field and Data Management.');
System.debug(bdr);
} else {
// Next, let's see if our Permission-Set Group and ConnectedApp permission-set exists
adminPermSetGroup = [SELECT Id FROM PermissionSetGroup WHERE DeveloperName = :permSetGroupApiName];
// Audit if we couldn't find the administrative permission-set group
if (adminPermSetGroup.size() == 0) {
System.debug(bdr);
System.debug(spacer + 'The Permission-Set Group "' + permSetGroupApiName + '" was not found.');
System.debug(bdr);
} else {
// Verify that the current user is associated with the administrative permission-set group
adminUserAssignment = [SELECT Id FROM PermissionSetAssignment WHERE AssigneeId = :UserInfo.getUserId() AND PermissionSetGroupId = :adminPermSetGroup[0].Id];
if (adminUserAssignment.size() != 0) {
System.debug(bdr);
System.debug(spacer + 'The current user is already assigned to the Permission-Set Group "' + permSetGroupApiName + '".');
System.debug(spacer);
System.debug(spacer + 'Nice work! Please visit');
System.debug(spacer + 'https://peernova.link/cuneiform/fdm/setup/step-006');
System.debug(spacer + 'to complete the next configuration step: Creating the Connected App.');
System.debug(bdr);
} else {
// Create the permission-set assignment
try {
// Test to see if we should throw an exception
if (testException) {
throw new DmlException(testExceptionMessage);
} else {
// Create the permission-set assignment
insert new PermissionSetAssignment(
AssigneeId = UserInfo.getUserId(),
PermissionSetGroupId = adminPermSetGroup[0].Id
);
// Audit that no exceptions took place
exceptionOccurred = false;
}
} catch (DmlException e) {
caughtException = e;
exceptionOccurred = true;
}
// Output that the permission-set was verified and already exists
System.debug(bdr);
if (exceptionOccurred) {
System.debug(spacer + 'An error occurred while attempting to create the permission-set assignment for the Group [' + permSetGroupApiName + '].');
System.debug(spacer + 'Error Message: ' + caughtException.getMessage());
System.debug(bdr);
System.debug(spacer + caughtException.getStackTraceString());
System.debug(bdr);
System.debug(spacer + 'DML exceptions often occur because of rights or access permissions. Please verify that');
System.debug(spacer + 'the user executing this script has the necessary rights to create a permission-set.');
System.debug(spacer + 'Try executing this script again once you have addressed the root cause of this exception.');
} else {
System.debug(spacer + 'The current user was successfully assigned to the Permission-Set Group "' + permSetGroupApiName + '".');
System.debug(spacer);
System.debug(spacer + 'Nice work! Please visit');
System.debug(spacer + 'https://peernova.link/cuneiform/fdm/setup/step-006');
System.debug(spacer + 'to complete the next configuration step: Creating the Connected App.');
}
System.debug(bdr);
}
}
}
Execute Our Permission-Set Assignment Apex Script
Follow these steps to open the Developer Console and execute our script that assigns your user to the Cuneiform for CRM: Administrators permission-sets group for Cuneiform for CRM.
Copy the Anonymous Apex Script to your clipboard.
Log into your Salesforce org – and launch the Developer Console.
Launch the Developer Console via the Gear Icon used to Open Salesforce Setup.
Launch the Execute Anonymous window from within the Developer Console. The Execute Anonymous Window modal is available on the Debug menu.
Open the Execute Anonymous window via the Debug menu in the Developer Console.
Paste the Anonymous Apex script in the Anonymous Apex Window.
Execute the Anonymous Apex Script by clicking on the Execute button.
Paste the Anonymous Apex Script into the Anonymous Apex Window – and click Execute.
Open the log file generated by the script – and filter on the debug results to view the output.
Open the generated log file – and enable the Debug filter to view the script output.
View the debug output and verify that the script assigned your user to the Cuneiform for CRM: Administrators permission-set group.
Verify from the script output that you were assigned to the Administrator permission-set group.