+ Start a Discussion

i am not able to understand how to insert a Role as a group member using apex

Hi All 

I am trying to add user role as GroupMember  to a group 

Now i have problem that GroupMember have only two fields GroupId // which will take the group id and UserOrGroupId = // which will only take user id or a group id, but it is not taking role id

and as have check that when we insert role as GroupMember throw cofiguration, salesforce create a saprate group and populate RelatedId with the role id and then insert this new group in GroupMember 

exmaple if there is a Group G1 and we want to add a role R1 , what salesforce will do is it create a saparate Group (let call G2) and in G2 salesforce pupulate RelatedId with the role id and type =role and then add this group G2 as group member to G1 (in that case in group mebmber Groupid will be poulated with G1 and UserOrGroupId populated with G2 id)

Now problem is that RelatedId , is not writable throw apex code so i am not able to understand how to insert a Role as a group member using apex
Eric GronholzEric Gronholz
When a role is created, a set of corresponding groups are created for that role automatically.  To find them, query the group object after you create the role and use the id from the newly created role as a filter in the RelatedId field:

SELECT Id, RelatedId, Type FROM Group WHERE RelatedId = <id of new role>;

You can then interrogate the Type to see which groups were created and use the Type field as another filter item in your query to choose one group.