+ Start a Discussion

Add user Role to GroupMember using Apex code

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