0

I need to get tab visibility for Permission Set. I have tabs for 2 objects:Account and Account__c. I use this SOQl request:

SELECT Id,Name,Visibility,ParentId FROM PermissionSetTabSetting WHERE (ParentId IN ('0PS5g000005r8bWGAQ')) AND (Name IN ('standard-Account','Account__c'))  ORDER BY Id ASC NULLS FIRST  LIMIT 2000

0PS5g000005r8bWGAQ - Permission Set Id standard-Account,Account__c - tab Names from TabDefinition.

When i use this query in anonymous, sf inspector or query editor it returns required data : enter image description here

BUT !!! When i use this query in my Apex code(i hardcoded it) it returns me null.

I found out that in my transaction Name of standard tab is replaced from 'standard-Account' to 'Account'.

And Name of custom tab is replaced from 'Account__c ' to '01r5g000000EJou'. It's CustomTadDefinition id.

So If i replace my SOQl query In Apex class like that:

 SELECT Id,Name,Visibility,ParentId FROM PermissionSetTabSetting WHERE (ParentId IN ('0PS5g000005r8bWGAQ')) AND (Name IN ('Account','01r5g000000EJou'))  ORDER BY Id ASC NULLS FIRST  LIMIT 2000

i'll get all needed info.

But to get CustomTadDefinition id , i need to use Tolling API for each custom tab.

This apex method is triggered from LWC. When i call this method from anonymous it works fine.

It's the same user, i checked it. I use without sharing classes.

I don't understand why i get different result.

2
  • 4
    The most common reason is that you have actually used different users in the two cases and there are sharing rules in use.
    – Phil W
    Aug 31, 2021 at 13:15
  • It's the same user, i checked it. I use without sharing classes.
    – Darya
    Sep 1, 2021 at 7:10

1 Answer 1

0

For some strange reason in LWC, Name field from PermissionSetTabSetting returns either standard object name or custom tab id, while in Classic, it returns the tab name.

Workaround here is to use Tooling API with Classic Session Id.

Create Visualforce Page 'Api'.

<apex:page controller="ApiCont" showHeader="false" sidebar="false">
    {!sid}
</apex:page>

Create Apex Controller ApiCont.

public with sharing class ApiCont {
    public static String SEPAR = '[|]';
    public String sid{ get{ return SEPAR + UserInfo.getSessionId() + SEPAR ;} set;}
}

Create Apex class ApiService.

public with sharing class ApiService{
    @testVisible static String sid;
    public static String getClassicSessionId() {
        if (sid == null) {
            sid = new PageReference(
                '/apex/Api'
            ).getContent().toString().substringBetween(ApiCont.SEPAR, ApiCont.SEPAR);
        }
        return sid;
    }
}

Create Apex class ToolingApi.

public with sharing class ToolingApi {
    static String restGet(String endPoint, String method, String sid) {
        Http h = new Http();
        HttpRequest hr = new HttpRequest();
        hr.setHeader('Authorization', 'Bearer ' + sid);
        hr.setTimeout(60000);
        hr.setEndpoint(endPoint);
        hr.setMethod(method);
        HttpResponse r = h.send(hr);
        return r.getBody();
    }
    public static String query(String query) {
        String baseURL = URL.getSalesforceBaseUrl().toExternalForm();
        return restGet( baseURL + '/services/data/v52.0/tooling/query?q='+ (query.replace(' ', '+')), 'GET', ApiService.getClassicSessionId() );
    }
}

Now if you run it as Tooling Api query, it will return correct results even in LWC:

String body = ToolingApi.query('SELECT Name, ParentId, Visibility FROM PermissionSetTabSetting WHERE ParentId IN (\'' + String.join(parentIds, '\',\'') + '\')');
        
    System.debug(LoggingLevel.Error,  'body: ' + body);

Wherever you used to have query

SELECT Name, ParentId, Visibility
FROM PermissionSetTabSetting
WHERE ParentId IN: parentIds

use instead

Map<String, Object> dataMap = (Map<String, Object>) JSON.deserializeUntyped(body);  
(List<PermissionSetTabSetting>) JSON.deserialize(JSON.serialize(dataMap.get('records')), List<PermissionSetTabSetting>.class )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.