Loading

Backfilling ApprovalWorkItem Object Permissions Post-Release 264 Upgrade

Publiceringsdatum: Oct 1, 2026
Beskrivning

Following the Release 264 upgrade, ApprovalWorkItem was promoted to a standalone top-level entity with independent security configuration. Consequently, users must have explicit object-level access assigned to custom profiles or permission sets. This guide provides instructions and an Apex utility script to automatically mirror object permissions from ApprovalSubmission onto ApprovalWorkItem for all custom profiles and permission sets.

Lösning

1. How to Execute the script?

Save the script as an apex class and run:

  1. In the Developer Console, click File > New > Apex Class.

  2. Enter the name: ApprovalPermissionBackfill and click ok.

  3. Paste the script into the class body and save (See point 4 [The Apex Backfill Script] for complete apex script).

  4. After save, click on Debug > Open Execute Anonymous apex window and paste the below code:
    ApprovalPermissionBackfill.runBackfill();

  5. Click on "Execute"

  6. Check the logs for any failures or misses and verify them.

2. What does the script changes?

The script interacts directly with the ObjectPermissions database table, applying access rules across different container types. Applies the same permissions to the AWI as that of the AS.

  • Standard Profiles: Salesforce platform rules prohibit explicit object permission updates on Standard Profiles (e.g., "Standard User" or "Read Only").
  • Custom Profiles: The script queries and updates these custom profiles and copies the same access to AWI as that of AS. If AWI has higher access then it will remain as is.

  • Permission Sets: Handled natively. The script inspects the source access levels on ApprovalSubmission and grants identical levels on ApprovalWorkItem.

  • Users: Object permissions cannot be assigned directly to individual users. By updating the underlying Permission Sets or Custom Profiles, assigned users instantly inherit access.

  • Permission Set Groups (PSGs): PSGs compute effective permissions dynamically from constituent Permission Sets. Updating the base Permission Sets automatically updates access for all users assigned via PSGs.

Important Note on Elevated Access: The backfill script only adds missing permissions. If a target Profile or Permission Set already possesses higher permissions on ApprovalWorkItem than ApprovalSubmission (for example, Read-Only on Submission but Read/Edit/Delete on WorkItem), the script preserves the elevated access and will never downgrade or revoke permissions.

 

3. Risks & Operational Safeguards

 

Scenario / Risk

Platform Behavior

Script Handling & Safeguard

Managed Package Permission Sets

Permission sets packaged by AppExchange vendors are read-only in subscriber orgs.

Updates fail with a log entry (Update Failed...). Admins should bundle the managed set with a custom Permission Set in a PSG.

Apex Governor Limits

Synchronous Apex allows a maximum of 10,000 DML row operations per transaction.

Hardcoded LIMIT 2000 safeguard prevents governor limit exceptions during execution.

Partial Row Failures

Restricted licenses or edge-case validations can block individual row inserts.

Uses Database.insert(records, false) and Database.update(records, false) (AllOrNone = false) to commit valid rows and log failures independently.

System Admin Overrides

Profiles with Modify All Data throw exceptions if explicit CRUD rows are added.

The script handles this automatically via the AllOrNone = false DML flag. It will safely skip the System Administrator profile, log the native failure, and continue processing.

Note : After running the script, if you feel you need any changes, you can manually edit the perm sets or profiles as per your requirement.

 

4. The Apex Backfill Script

/**
 * Utility Script to backfill ApprovalWorkItem permissions from ApprovalSubmission.
 * Run this in Developer Console -> Execute Anonymous, or package it as an Apex Class.
 */
public class ApprovalPermissionBackfill {
    
    public static void runBackfill() {
        System.debug('--- Starting ApprovalWorkItem Permission Backfill ---');
        
        // 1. Query Source Permissions (ApprovalSubmission)
        // We explicitly ignore Standard Profiles because Salesforce blocks explicit permission DML on them.
        // LIMIT 2000 safely covers most customers while staying far below the 10,000 DML limit.
        List<ObjectPermissions> sourcePerms = [
            SELECT ParentId, Parent.Name, Parent.Profile.Name, Parent.IsOwnedByProfile,
                   PermissionsRead, PermissionsCreate, PermissionsEdit, 
                   PermissionsDelete, PermissionsViewAllRecords, PermissionsModifyAllRecords
            FROM ObjectPermissions
            WHERE SobjectType = 'ApprovalSubmission'
              AND (PermissionsRead = true OR PermissionsCreate = true OR PermissionsEdit = true 
                   OR PermissionsDelete = true OR PermissionsViewAllRecords = true OR PermissionsModifyAllRecords = true)
            LIMIT 2000
        ];
        
        if (sourcePerms.isEmpty()) {
            System.debug('No applicable ApprovalSubmission permissions found to propagate.');
            return;
        }
        
        // Extract ParentIds to optimize the second query
        Set<Id> parentIds = new Set<Id>();
        for (ObjectPermissions op : sourcePerms) {
            parentIds.add(op.ParentId);
        }
        
        // 2. Query Existing Target Permissions (ApprovalWorkItem) scoped ONLY to the relevant parents
        List<ObjectPermissions> existingTargetPerms = [
            SELECT Id, ParentId, PermissionsRead, PermissionsCreate, PermissionsEdit, 
                   PermissionsDelete, PermissionsViewAllRecords, PermissionsModifyAllRecords
            FROM ObjectPermissions
            WHERE SobjectType = 'ApprovalWorkItem'
              AND ParentId IN :parentIds
        ];
        
        Map<Id, ObjectPermissions> targetPermMap = new Map<Id, ObjectPermissions>();
        for (ObjectPermissions op : existingTargetPerms) {
            targetPermMap.put(op.ParentId, op);
        }
        
        List<ObjectPermissions> permsToInsert = new List<ObjectPermissions>();
        List<ObjectPermissions> permsToUpdate = new List<ObjectPermissions>();
        
        // 3. Compare and build Insert/Update lists
        for (ObjectPermissions src : sourcePerms) {
            if (targetPermMap.containsKey(src.ParentId)) {
                ObjectPermissions tgt = targetPermMap.get(src.ParentId);
                Boolean needsUpdate = false;
                
                // Only flags for update if the target is missing a permission that the source has
                if (src.PermissionsRead && !tgt.PermissionsRead) { tgt.PermissionsRead = true; needsUpdate = true; }
                if (src.PermissionsCreate && !tgt.PermissionsCreate) { tgt.PermissionsCreate = true; needsUpdate = true; }
                if (src.PermissionsEdit && !tgt.PermissionsEdit) { tgt.PermissionsEdit = true; needsUpdate = true; }
                if (src.PermissionsDelete && !tgt.PermissionsDelete) { tgt.PermissionsDelete = true; needsUpdate = true; }
                if (src.PermissionsViewAllRecords && !tgt.PermissionsViewAllRecords) { tgt.PermissionsViewAllRecords = true; needsUpdate = true; }
                if (src.PermissionsModifyAllRecords && !tgt.PermissionsModifyAllRecords) { tgt.PermissionsModifyAllRecords = true; needsUpdate = true; }
                
                if (needsUpdate) {
                    permsToUpdate.add(tgt);
                }
            } else {
                ObjectPermissions newPerm = new ObjectPermissions(
                    ParentId = src.ParentId,
                    SobjectType = 'ApprovalWorkItem',
                    PermissionsRead = src.PermissionsRead,
                    PermissionsCreate = src.PermissionsCreate,
                    PermissionsEdit = src.PermissionsEdit,
                    PermissionsDelete = src.PermissionsDelete,
                    PermissionsViewAllRecords = src.PermissionsViewAllRecords,
                    PermissionsModifyAllRecords = src.PermissionsModifyAllRecords
                );
                permsToInsert.add(newPerm);
            }
        }
        
        // 4. Perform DML (AllOrNone = false prevents one failure from failing the whole batch)
        System.debug('Found ' + permsToInsert.size() + ' new permissions to insert.');
        System.debug('Found ' + permsToUpdate.size() + ' existing permissions to update.');
        
        if (!permsToInsert.isEmpty()) {
            Database.SaveResult[] insertResults = Database.insert(permsToInsert, false);
            logResults(insertResults, 'Insert', permsToInsert);
        }
        
        if (!permsToUpdate.isEmpty()) {
            Database.SaveResult[] updateResults = Database.update(permsToUpdate, false);
            logResults(updateResults, 'Update', permsToUpdate);
        }
        
        System.debug('--- Finished ApprovalWorkItem Permission Backfill ---');
    }
    
    private static void logResults(Database.SaveResult[] results, String operation, List<ObjectPermissions> processedList) {
        Integer successCount = 0;
        for (Integer i = 0; i < results.size(); i++) {
            Database.SaveResult sr = results[i];
            if (sr.isSuccess()) {
                successCount++;
            } else {
                Id failedParentId = processedList[i].ParentId;
                for (Database.Error err : sr.getErrors()) {
                    System.debug(LoggingLevel.ERROR, operation + ' Failed for Profile/PermSet ID ' + failedParentId + ': ' + err.getMessage());
                }
            }
        }
        System.debug(operation + ' operations succeeded: ' + successCount + ' out of ' + results.size());
    }
}

Knowledge-artikelnummer

005460904

 
Laddar
Salesforce Help | Article