PermissionSetTabSetting record returns different Name when i call it from LWC

apexlightning-web-componentspermission-setstabtab-settings

I have a method which returns list of PermissionSetTabSetting records.
I use following SOQL:

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

When I call this method from anywhere except LWC it returns:

enter image description here
I called this method from anonymous, from apex class , test class(in system mode and as System administrator).

I changed with sharing / without sharing identifier for this class. It works the same.

But!!!

When I call this method from LWC i get:

enter image description here

The Id of the record is the same, but Name is changed!

I checked User ID using UserInfo.getUserId(). User is the same.
Everything is the same, except Name.

What can i do?

Best Answer

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 )