afv-library/samples/ui-bundle-template-app-react-sample-b2e/force-app/main/default/classes/TenantTriggerHandler.cls
k-j-kim 445ac2f126
Rename webapp/webapplication to UI bundle across the codebase
Directory renames: webapplications/ → uiBundles/, samples dirs drop -experimental suffix
File renames: *.webapplication-meta.xml → *.uibundle-meta.xml, webapplication.json → ui-bundle.json
XML metadata: <WebApplication> → <UIBundle>
NPM packages: @salesforce/webapp-* → @salesforce/ui-bundle-*
Skill names: *-webapplication-* → *-salesforce-ui-bundle-*
CLI references: sf webapp → sf ui-bundle
Source code: function/variable/type names, import paths, string literals updated
ESLint config: Added .worktrees/** to ignores
CHANGELOG repo URLs preserved
2026-03-27 17:54:18 -07:00

70 lines
2.2 KiB
OpenEdge ABL

/**
* Assigns Tenant_Maintenance_Access permission set to users linked to Tenant__c records.
* When a Tenant is created or updated with User__c set, the linked user gets the permission set.
*/
public with sharing class TenantTriggerHandler {
private static final String PERMISSION_SET_NAME = 'Tenant_Maintenance_Access';
/**
* Assign Tenant_Maintenance_Access to each user linked via Tenant__c.User__c.
* Insert: all records with User__c set.
* Update: records where User__c was set or changed to a non-null value.
*/
public static void assignTenantMaintenanceAccess(List<Tenant__c> newList, Map<Id, Tenant__c> oldMap) {
Set<Id> userIds = new Set<Id>();
for (Tenant__c t : newList) {
if (t.User__c == null) {
continue;
}
if (oldMap == null) {
userIds.add(t.User__c);
} else {
Tenant__c oldRec = oldMap.get(t.Id);
if (oldRec == null || oldRec.User__c != t.User__c) {
userIds.add(t.User__c);
}
}
}
if (userIds.isEmpty()) {
return;
}
List<PermissionSet> permSets = [
SELECT Id
FROM PermissionSet
WHERE Name = :PERMISSION_SET_NAME
AND IsOwnedByProfile = false
LIMIT 1
];
if (permSets.isEmpty()) {
return;
}
Id permSetId = permSets[0].Id;
Set<Id> alreadyAssigned = new Set<Id>();
for (PermissionSetAssignment psa : [
SELECT AssigneeId
FROM PermissionSetAssignment
WHERE PermissionSetId = :permSetId
AND AssigneeId IN :userIds
]) {
alreadyAssigned.add(psa.AssigneeId);
}
List<PermissionSetAssignment> toInsert = new List<PermissionSetAssignment>();
for (Id uid : userIds) {
if (alreadyAssigned.contains(uid)) {
continue;
}
toInsert.add(new PermissionSetAssignment(
PermissionSetId = permSetId,
AssigneeId = uid
));
}
if (!toInsert.isEmpty()) {
insert toInsert;
}
}
}