Is it possible to add a Group to another Group (as Sub/Nested Group) through Apex - Trigger/Batch?. Also want to know the possibility to add a Role into a Group.
Any thoughts ??
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.Sign up to join this community
You can use the GroupMember object for this. A GroupMember can either be a User or a Group. https://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_groupmember.htm
Group parentGroup = [Select Id From Group Where Name = 'ParentGroupName']; Group childGroup = [Select Id From Group Where Name = 'ChildGroupName']; GroupMember gm = new GroupMember(GroupId = parentGroup.Id, UserOrGroupId = childGroup.Id); insert gm;
To add a role into a Group via APEX, use the automatically created Group for the Role (a Group with Type 'Role' and relatedId = roleId). Then add the Group as a GroupMember to the parent Group.
UserRole theRole = [Select Id From UserRole Where Name = 'theRoleName'; Group roleGroup = [Select Id, Name, Type From Group Where Type = 'Role' AND relatedId = :theRole.Id]; GroupMember gm = new GroupMember(GroupId = parentGroup.Id, UserOrGroupId = roleGroup.Id); insert gm;