特定のユーザーに関連するすべての公開グループをApexを使用して取得する必要がある場合、解決策で示されている次のサンプルコードを使用できます。
組織内では、ユーザーに関連するグループは3種類存在します。
次のサンプルコードは、以上で説明したすべての要件のそれぞれに対して機能します。
//グループ ID が重複しないようにセットを宣言する
Set<Id> results = new Set<Id>();
//ロールのあるグループのマップを宣言する
Map<Id,Id> grRoleMap = new Map<Id,Id>();
//RelatedID (つまり UserRoledId) をキーとしてマップに入力する
for(Group gr : [select id,relatedid,name from Group])
{
grRoleMap.put(gr.relatedId,gr.id);
}
//ユーザに直接関連付けられたグループ
Set<Id> groupwithUser = new Set<Id>();
//GroupId を使用してユーザをグループに入力し、Regular、Role、RoleAndSubordinates タイプのグループに絞り込む
for(GroupMember u :[select groupId from GroupMember where UserOrGroupId='<User Id>' and (Group.Type = 'Regular' OR Group.Type='Role' OR Group.Type='RoleAndSubordinates')])
{
groupwithUser.add(u.groupId);
}
//ロールのあるグループ
for(User u :[select UserRoleId from User where id='<User Id>'])
{
//現在のユーザロールがマップの一部かどうかを確認する
if(grRoleMap.containsKey(u.UserRoleId))
{
results.add(grRoleMap.get(u.UserRoleId));
}
}
//両方のセットを結合する
results.addAll(groupwithUser);
//グループのリスト全体をトラバースして他のネストされたグループを確認する
Map<Id,Id> grMap = new Map<Id,Id>();
for(GroupMember gr : [select id,UserOrGroupId,Groupid from GroupMember where
(Group.Type = 'Regular' OR Group.Type='Role' OR Group.Type='RoleAndSubordinates')])
{
grMap.put(gr.UserOrGroupId,gr.Groupid);
}
for(Id i :results)
{
if(grMap.containsKey(i))
{
results.add(grMap.get(i));
}
}
system.debug('########' + results);
注: ここで示しているコードは一例です。内容を確認し、組織に合わせて変更してください。
000387341

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.