Is there an object we can query to know which page layout is associated to a particular Profile/RecordType match?
2 Answers
If you are interested in solution which would work only for Salesforce Classic, I have it.
It is possible to make Tooling API ProfileLayout Object Query
[ select Layout.Name from ProfileLayout where ProfileId = :UserInfo.getProfileId() AND RecordTypeId = :record.RecordTypeId ]
To make it possible to use in Apex Code you need preparation step.
1) Allow Self-Callout. To do this, go to Remote Site Settings by link https://login.salesforce.com/0rp/e?SiteName=self&EndpointUrl=https://yourinstance.salesforce.com/ on production or Developer Edition instance or https://test.salesforce.com/0rp/e?SiteName=self&EndpointUrl=https://yourinstance.salesforce.com/ on any of sandbox instance and input link to your instance and click save.
2) Run the following code from Anonymous Execution Window
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();
}
static String toolingAPISOQL( String query) {
String baseURL = URL.getSalesforceBaseUrl().toExternalForm();
return restGet( baseURL +
'/services/data/v41.0/tooling/query?q='+ (query.replace(' ', '+'))
,
'GET', UserInfo.getSessionId() );
}
static String getLayoutNameForCurrentUserProfile( Id recordId ) {
String body = toolingAPISOQL('select Layout.Name from ProfileLayout where ProfileId = \'' + UserInfo.getProfileId() + '\' AND RecordTypeId = \'' + [ select RecordTypeId from Account where id = :recordId].RecordTypeId + '\'');
System.debug(LoggingLevel.ERROR, '@@@ v: ' + body );
String name = body.substringBetween('"Name":"', '"');
System.debug(LoggingLevel.ERROR, '@@@ v: ' + name );
return name;
}
getLayoutNameForCurrentUserProfile('001g000001hLEAj');
You will can the name of desired layout.
Note that this would work only in Salesforce Classic and not in Lightning by strange Salesforce policy which disadvantages Lightning very much since
By security policy, sessions created by Lightning components aren’t enabled for API access. This prevents even your Apex code from making API calls to Salesforce.
-
Will this work in lightning if i use named credentials? The docs say "Using a named credential for specific API calls allows you to carefully and selectively bypass this security restriction." This works fine using above code in Classic, but in near future if my client asks me to migrate to lightning, i just want to be ready. Commented Jan 19, 2018 at 6:48