Updating PermissionSetTabSetting Without Metadata API?

Updating PermissionSetTabSetting Without Metadata API?

On August 14, 2025, Posted by , In Apex,Salesforce Technical Questions, With Comments Off on Updating PermissionSetTabSetting Without Metadata API?
Updating PermissionSetTabSetting Without Metadata API

Question:

I need to update the visibility settings for tabs within a Permission Set in Apex. I successfully retrieved PermissionSetTabSetting records but encountered an issue when trying to upsert changes in a loop.

Here’s my approach:

List<PermissionSetTabSetting> tabs2GetUpserted = new List<PermissionSetTabSetting>();
Map<String, String> existingTabsMap = new Map<String, String>();

MetadataService.PermissionSet target = 
    (MetadataService.PermissionSet) service.readMetadata('PermissionSet', new String[] { sourceName }).getRecords()[0];

if (target.tabSettings != null) {                
    for (MetadataService.PermissionSetTabSetting tv : target.tabSettings) { 
        existingTabsMap.put(tv.tab, (tv.visibility == 'Visible' ? 'DefaultOn' : 'DefaultOff'));
    }
}

for (String entry : this.source.keySet()) {
    String tempoVis = existingTabsMap?.get(entry);
    String vis = this.source.get(entry);
    
    PermissionSetTabSetting pst = new PermissionSetTabSetting();
    pst.parentId = currentTargetId;
    pst.name = entry;
    
    if (tempoVis == null) {
        pst.visibility = vis;
    } else {
        pst.visibility = 'DefaultOff';
    }

    tabs2GetUpserted.add(pst);
}

upsert tabs2GetUpserted;

However, I keep getting this error:
System.DmlException: Upsert failed. First exception on row X; first error: DUPLICATE_VALUE, Permission set already has a tab setting defined for tab. Update the existing tab setting.: []

What am I doing wrong? Is it possible to update PermissionSetTabSetting without using the Metadata API, or is that the only way?

Answer:

The error occurs because PermissionSetTabSetting records already exist for some tabs, and upsert does not automatically handle updates for this object. Unlike standard sObjects, PermissionSetTabSetting does not support upserting in the usual way.

Enroll at CRS Info Solutions, a premier Salesforce online training institute. Book your free demo session now and start your journey to mastering Salesforce!

Solution 1: Use update Instead of upsert

Using update instead of upsert ensures that existing PermissionSetTabSetting records are modified rather than causing duplicate errors. Since upsert does not work reliably for this object, you need to first check if a record exists and then either insert or update accordingly. This approach prevents DUPLICATE_VALUE errors and ensures smooth updates to tab visibility settings.

Instead of upsert, try fetching existing records first and using update for existing ones while inserting only new records:

List<PermissionSetTabSetting> recordsToUpdate = new List<PermissionSetTabSetting>();
List<PermissionSetTabSetting> recordsToInsert = new List<PermissionSetTabSetting>();

for (String entry : this.source.keySet()) {
    String tempoVis = existingTabsMap?.get(entry);
    String vis = this.source.get(entry);
    
    PermissionSetTabSetting pst = new PermissionSetTabSetting();
    pst.parentId = currentTargetId;
    pst.name = entry;
    
    if (tempoVis == null) {
        pst.visibility = vis;
        recordsToInsert.add(pst);
    } else {
        pst.visibility = 'DefaultOff';
        recordsToUpdate.add(pst);
    }
}

if (!recordsToInsert.isEmpty()) {
    insert recordsToInsert;
}
if (!recordsToUpdate.isEmpty()) {
    update recordsToUpdate;
}

Explanation: The code separates new and existing PermissionSetTabSetting records before performing DML operations. It loops through this.source.keySet(), checking if each tab exists in existingTabsMap; if not, it creates a new record for insertion, otherwise, it updates the visibility to DefaultOff. Finally, it inserts new records and updates existing ones to avoid DUPLICATE_VALUE errors.

Solution 2: Use the Metadata API

If DML operations on PermissionSetTabSetting continue to cause issues, the recommended way to modify permission sets is through the Metadata API. You can use Metadata.Operations.enqueueDeployment() with a PermissionSet update.

Accelerate Your Salesforce Career in Seattle

Elevate your Salesforce skills with expert-led  Salesforce online training  in Seattle, tailored for both beginners and professionals. Gain practical experience through real-world projects and master cloud-based CRM solutions with guidance from certified instructors. Unlock exciting career opportunities and become a Salesforce expert with comprehensive, hands-on learning at CRS Info Solutions.

CRS Info Solutions is a leading Salesforce training in Seattle, offering a thorough, real-time project-based curriculum. Our courses cover Salesforce Admin, Developer, Integration, Marketing Cloud, CPQ, and Lightning Web Components (LWC). With expert instructors offering hands-on training, we ensure you’re well-prepared for real-world challenges. Join us now to become a certified Salesforce professional and take the next step in your career.

Enroll today for a free demo at CRS Info Solutions, Seattle!!!

Related Posts:

Permission Sets in Salesforce Step-by-Step
Salesforce – Permission Set Groups Multiple Choice Questions



Comments are closed.