The script initially generates two lists of users:
A final list is created of users who belong to both groups. This new list is the one that is emailed and contains the users who will have this customer permission set removed. The code's actions are as follows:
Open the Developer Console to execute the script by following these steps:
NOTE: This script removes the custom permission set assignment from the list of user records. Not only will this remove the permissions to CPQ objects, but any access the custom permission set grants will be removed when the permission set is unassigned.
**REMOVES CUSTOM PERMISSION SET ASSIGNMENTS FROM UNLICENSED USERS
//1. find the different permission sets that grant access to CPQ objects
List<ObjectPermissions> objUsers = [SELECT ParentId, Parent.Name
FROM ObjectPermissions
WHERE PermissionsRead = True AND
SobjectType in ('SBQQ__Quote__c', 'SBQQ__PricingGuidance__c', 'SBQQ__ProductRule__c', 'SBQQ__QuoteTemplate__c', 'SBQQ__Subscription__c', 'SBQQ__PriceRule__c') AND
Parent.IsOwnedByProfile = false];
//'sbaa__Approval__c', 'sbaa__ApprovalRule__c' - for advanced approvals
Set<Id> objPermIds = new Set<Id>(); // get unique IDs of permission sets
for(ObjectPermissions objPer: objUsers)
{
objPermIds.add(objPer.ParentId);
}
List<Id> objPermIdsList = new List<id>(objPermIds); //contains IDs of permission sets that grant access to CPQ objects
//EXAMPLE: ('0PS2v000005TGdhGAG','0PS2v000005TGdiGAG','0PS2v000005TGdgGAG','0PS2v000005TGdjGAG','0PS2v000005WS0NGAW')
// 2. find different users who have the above permission sets - [2]
List<PermissionSetAssignment> permSetsUsers = [SELECT Id, AssigneeId,PermissionSetId from PermissionSetAssignment where PermissionSetId =: objPermIdsList];
//System.debug(permSetsUsers);
//3. find users who have CPQ MPL assigned - [3]
List<UserPackageLicense> MPL_users = [SELECT UserId
FROM UserPackageLicense
WHERE PackageLicense.NamespacePrefix = 'SBQQ']; // 'sbaa' - for advanced approvals
//System.debug(MPL_users);
Set<Id> MPL_id_set = new Set<Id>();
for(UserPackageLicense var: MPL_users)
{
MPL_id_set.add(var.UserId); // gets the user id
}
//4. Check the users who have permission sets but no MPL assigned
/* list which is retrieved from the [2](step 2) and will be iterated over to check if the user has MPL assigned (set created in step 3)..*/
List<PermissionSetAssignment> finalUsers = new List<PermissionSetAssignment>();
for(PermissionSetAssignment temp: permSetsUsers)
{
if(!MPL_id_set.contains(temp.AssigneeId))
{
finalUsers.add(temp); // user did not have MPL assigned, needs to be deleted
}
}
if(finalUsers.size()==0)
System.debug('all set');
System.debug(finalUsers.size());
List<PermissionSetAssignment> delFinalUsers = finalUsers;
//5. uncomment the line below to delete PS assignments from the list of users
//delete delFinalUsers;
String Row = '';
Row +='Id' + ','+ 'AssigneeId' + ','+ 'PermissionSetId'+'\n';
for(PermissionSetAssignment permm: finalUsers)
{
Row +=permm.Id + ',' + permm.AssigneeId + ','+ permm.PermissionSetId + '\n' ;
}
Messaging.EmailFileAttachment csvAttachment = new Messaging.EmailFileAttachment();
Blob csvBlob = blob.valueOf(Row);
String csvName = 'userswhodoesnthaveMPL.csv';
csvAttachment.setFileName(csvName);
csvAttachment.setBody(csvBlob);
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
//Replace email address below to receive communications from this script
String[] toAddresses = new String[]{'EMAIL ADDRESS'};
String subject = 'users who do not have MPL';
email.setSubject(subject);
email.setToAddresses(toAddresses);
email.setPlainTextBody('users who do not have MPL');
email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttachment});
Messaging.SendEmailResult[] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[]{email});
000390394

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.