You need to sign in to do that
Don't have an account?
Automatically assign a new user to a Chatter Group
Hi,
I am trying to automatically assign a new user to a Chatter Group called 'Everyone'. The code below works as long as I do not select a role; however, when I select a role I get the following error. Does anyone have a clue what this error means or if there is a code that can do what I am trying to do?
22:29:46.425
(425800000)|EXCEPTION_THROWN|[24]|System.DmlException: Insert failed.
First exception on row 0; first error: MIXED_DML_OPERATION, DML
operation on setup object is not permitted after you have updated a
non-setup object (or vice versa): CollaborationGroupMember, original
object: User: []
22:29:46.426
(426685000)|FATAL_ERROR|System.DmlException: Insert failed. First
exception on row 0; first error: MIXED_DML_OPERATION, DML operation on
setup object is not permitted after you have updated a non-setup object
(or vice versa): CollaborationGroupMember, original object: User: []
trigger User_trigger on User (after insert, before insert, after update, before update) { if ( Trigger.isInsert ) { if ( Trigger.isAfter ) { List<CollaborationGroupMember> cgm = new List<CollaborationGroupMember>(); Id cgID = [ Select Id FROM CollaborationGroup WHERE Name = 'Everyone' LIMIT 1 ].ID; System.debug('zzz: ' + cgID); for ( user u: Trigger.new ) { cgm.add(new CollaborationGroupMember (CollaborationGroupId = cgID, MemberId = u.id)); } insert cgm; } } }
You need to bulkify your code so that it only calls the @future method once and only runs once and the cgm inserts once. Just put your for loop inside of the @future method and pass trigger.newmap.keyset() to that method. Something like this:
As for #2 you just need to insert a user in a testmethod and then check that the @future has run after a test.StopTest. I.E.
All Answers
1. Ok--this is what I came up with and it seems like it works; however, I would appreciate any community feedback about the code. I want to make sure that by doing this it does not break anything within the SFDC ORG.
2. In addition, I may need some help with a test script since I struggle with those and now this would be my first test script working with @future.
So if feedback is thumbs up on #1 above I would appreciate help with step 2. Maybe others have a all user group and you get tired of adding them manually when you setup the user. if this code doesnt inadvertantly cause additional issues I believe I will need to incorporate logic to prevent Customer Users from going into the Everyone Group. I have not played with that yet????
Again, I appreciate all feedback--thanks.
You need to bulkify your code so that it only calls the @future method once and only runs once and the cgm inserts once. Just put your for loop inside of the @future method and pass trigger.newmap.keyset() to that method. Something like this:
As for #2 you just need to insert a user in a testmethod and then check that the @future has run after a test.StopTest. I.E.
Thanks for the heads up on Bulkify! I have an error though at public static void AddUserToGroup(List<User> Users) {
I says: Unsupported parameter type LIST<User>
I even tried other objects such as Account and contact and I get the same error.
I am calling the method like this...
asyncApex.addUserToGroup(Trigger.new);
Any ideas? I am still trying to figure it out but I wonder if @future allows these types of objects to be passed!!!! I quickly tried removing the @future and it compiled so @future doesn't like these types being passed in!!!!
Editing hear again!!!
Just found this -- so I don't think I can do what you are suggesting! What say you?
But then again, talking out loud I can create all the IDS in a collection or array and pass that???
Sorry, forgot about that. You can only pass a primitive, sobject, or list of primitives to @future functions. Just change your @future function to take a list of ids and pass it trigger.newmap.keyset().
That is too funny you just beat me posting pretty much the same thing! Look at the previous post that I edited.
Thanks for your help I will work on that. Another thing that i realized that I have to do. I need to not process Chatter External (Customer Chatter) users. Since this the Everyone Group is only for internal users.
If I run into any more issues I will be sure to reach out to you. Thanks again for the Bulkify solution and the test script. I haven't got to the test script yet! :)
Here is the final code that I unit tested and it works in case anyone else is looking for this solution. Thanks again for your help on the Bulkify. I forget about that sometimes. New concept for me along with coded test scripts too! :)
do you know if it works for Community Groups too?
Thanks in advance!