function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
BellaBella 

Adding User to Public Group

Quick (hopefully) question. I need to add a specific user to a public group. Ids for both are listed within an object called Fund Document Entitlments, so all I have to do is use those two ids, but I'm not sure how. Here's my code so far:

 

 

public static void AddUserToPublicGroup(List<Fund_Document_Entitlement__c> items){
	Set<Id> portalIds = new Set<Id>();
	Set<Id> publicGroupIds = new Set<Id>();
	for(Fund_Document_Entitlement__c fde : items){
		portalIds.add(fde.Customer_Portal_User_ID__c);
		publicGroupIds.add(fde.Public_Group_ID__c);
	}
		
	List<Group> pgList = [select Id from Group where Id in: publicGroupIds];
	List<User> uList = [select Id from User where Id in: portalIds];
		
	for(Fund_Document_Entitlement__c fde : items){
		for(Group pgroup : pgList){
			for(User u : uList){
				if(fde.Customer_Portal_User_ID__c == u.Id && fde.Public_Group_ID__c == pgroup.Id){
					//what do I put here
				}
			}
		}
	}
}

 Somehow I need to say "add the user with user Id u.Id into group pgroup.Id. How do I do that? Thanks!

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You'll need to:

 

create a new GroupMember object

set the GroupId to your group id

set the UserOrGroupId to the user id

insert into the database (or store in a list for later insertion if there are a few of them, to avoid governor limit probs).

 

 

All Answers

bob_buzzardbob_buzzard

You'll need to:

 

create a new GroupMember object

set the GroupId to your group id

set the UserOrGroupId to the user id

insert into the database (or store in a list for later insertion if there are a few of them, to avoid governor limit probs).

 

 

This was selected as the best answer
BellaBella

Ok I did that and it attempts to add the new GroupMember object but then I get the following error:


"12:28:38.819|USER_DEBUG|[46,17]|DEBUG|Trying to Insert Group Member List DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): GroupMember, original object: Investment__c"


Investment__c is a custom object I use in other places. A new investment triggers the creation of an Fund Document Entitlement object which in turn triggers the insertion of the user into the group, but I don't understand the error above. Can anyone explain?


Note: When I tried to do it manually, I added the user to the group without any trouble, so it's something trigger related...

 

Okay figured it out. Used @future and changed my method signeture to use Set<Id>. Thanks!

hiiiiiihiiiiii

Hi,

 

 I'm facing a problem from adding a groupmember to my groups ,type of queue.here is my code

GroupMember gm;
gm = new GroupMember(UserOrGroupId='005E0000000keMl',GroupId='00GE0000001Rd0DMAS');
insert gm;

But I'm not able insert a group member in group.wil you please tell me problem where is and give me sol.

i need it's urgent.

 

Thanks......

AshishyadavAshishyadav
hey can you put full code so that i can study about it
CoolSurenCoolSuren

Hi Bob,

 

Is it possible to pass RoleId as the UserOrGroupId?