LoginSignup
4
4

More than 5 years have passed since last update.

ApexでSalesforceのアプリケーションメニューを取得する

Posted at

ApexでSalesforceのアプリケーションメニューを取得する

Salesforceの右上メニューのアプリケーション一覧を取得したくて、REST APIで取得したら問題が起きたので、Apexで実現した。

だめな例

よくこのように書いてあるが、これだとだめ。
参考:apex - How to Get App Name - Salesforce Stack Exchange

SELECT Id, Name, Label FROM AppMenuItem WHERE Type = 'TabSet'
  • REST API
    • システム管理者以外だと何も取得できない。
  • Apex
    • with sharingをつけても権限にかかわらず何でも取得できてしまう。
    • 「サンプルコンソール」のような謎なアプリケーションも取得できてしまう。

正解

正解は以下のようにする。

    @RemoteAction
    global static List<AppMenuItem> getAppMenuItems(){
        String loginUserId = UserInfo.getUserId();
        List<SetupEntityAccess> entities = [
            SELECT SetupEntityId
            FROM SetupEntityAccess
            WHERE ParentId
            IN (select PermissionSetId from PermissionSetAssignment where AssigneeId = :loginUserId)
            AND SetupEntityType = 'TabSet'
        ];
        List<Id> entityIds = new List<Id>();
        for(SetupEntityAccess entity : entities) {
            entityIds.add(entity.SetupEntityId);
        }

        List<AppMenuItem> menus = [
            SELECT Id, Name, Label, NamespacePrefix
            FROM AppMenuItem
            WHERE ApplicationId  IN :entityIds
            ORDER BY SortOrder ASC
        ];
        return menus;
    }

これでプロファイルであるアプリケーションを参照不可にすればちゃんと結果から除かれる。
「サンプルコーンソール」のような謎のアプリケーションも含まれなくなる。

参考:soql - Appmenuitem & SetupEntityAccess relation for creating app launcher for connected apps - Salesforce Stack Exchange

4
4
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4