You need to sign in to do that
Don't have an account?
Piyush Tawari 9
Trigger on User to update group membership of user based on user profile and custom setting
My question is to Create a trigger on User to update group membership of user based on user profile and custom setting. Create Custom settings which will store the mapping of the groups to Profiles.On Insert/Update of User, trigger will recalculate the group membership of the user based on the mapping stored in custom settings. i Have done for insert part but for Update there are too many possibilities that can happen. So please if anyone knows answer help me.
trigger GroupMembership on User (after update,after insert) {
list<custom_setting__c> setting = custom_setting__c.getAll().values();
list <GroupMember> GrpMem = new list<groupMember>();
list<GroupMember> Grp = [select id,UserOrGroupId,GroupId from GroupMember];
for(User usr : trigger.new)
{
if(trigger.isInsert)
{
for(Custom_setting__c custom : setting)
{ Id cusId = (Id)custom.Profile_Id__c;
if(cusId == usr.ProfileId)
{
GroupMember GM = new GroupMember();
GM.GroupId = (Id)custom.Group_id__c;
GM.UserOrGroupId = usr.Id;
GrpMem.add(GM);
}
}
}
}
insert GrpMem;
}
This is my code when adding new user. Help me with Update code.
trigger GroupMembership on User (after update,after insert) {
list<custom_setting__c> setting = custom_setting__c.getAll().values();
list <GroupMember> GrpMem = new list<groupMember>();
list<GroupMember> Grp = [select id,UserOrGroupId,GroupId from GroupMember];
for(User usr : trigger.new)
{
if(trigger.isInsert)
{
for(Custom_setting__c custom : setting)
{ Id cusId = (Id)custom.Profile_Id__c;
if(cusId == usr.ProfileId)
{
GroupMember GM = new GroupMember();
GM.GroupId = (Id)custom.Group_id__c;
GM.UserOrGroupId = usr.Id;
GrpMem.add(GM);
}
}
}
}
insert GrpMem;
}
This is my code when adding new user. Help me with Update code.
In update why don't you simply delete the records the first and then call your insert method?
Move this code to new function and call it for insertion into GroupMember.
In update just gather all the users and query like this
and delete these Existing Users and then call ur insert function.
[NOTE : I'm assuming one user per group]
Hope it helps.. :)
All Answers
In update why don't you simply delete the records the first and then call your insert method?
Move this code to new function and call it for insertion into GroupMember.
In update just gather all the users and query like this
and delete these Existing Users and then call ur insert function.
[NOTE : I'm assuming one user per group]
Hope it helps.. :)
Hi Piyush,
Please find below code snippet and let me know in cas eof any help :)
Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com
If user's old and new both profile belong to same group then he should remain in that group. But in your solution we are deleting him from that group and again adding him. So its not really Updating his membership of Group? Can we do it without deleting it?
You are considering that one profile is associated with one group only right?
Piyush,
Yes , You are right, I am considering one profile associated with one group
Please let me know, the same.
Thanks
What can we do if one profile is associated with more than one group? I tried using loops but ended up with 3 - 4 for loops in each other.
Thanks
Although it simpify the execution of operation that's y I suggested that approach but if you don't want to delete the user from the group if group are same then u just need to perform some comparisons with the groups.
If you want me to write the code then tell me. I'll be happy to help you.
I would love if you can write that code for me because when I am writing that code I am ending up with 3-4 inner loops.
Did you get a chance to look over my code ?
Please let me know in case of any query/help. I am happy to help you :)
Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com