You need to sign in to do that
Don't have an account?
Praveen Jha
Add/remove user to specific group based on role when checkbox is selected - Very Very Urgent
I have one custom field "Notify me on new release" on user object whose data type is checkbox. I have added user to public group based on role e.g. "Partner community manager" . There are 50 users belongs to "partner community manager" and i want to remove any one user from the group when i deselect "notify me on new relese " checkbox field in user detail page. .Right now i can add or remove user to public group "alert" through below code.
trigger AddingtoAlertPublicgroup on User (after insert,after Update) {
List<GroupMember> grpMemlist = new List<GroupMember>();
Set<Id> userIdsToProcess = new Set<Id>();
Set<Id> usersToBeRemoved = new Set<Id>();
Id alertGroupId;
for(User Usr : Trigger.New){
if(trigger.isInsert){
if(Usr.Notify_me_of_releases__c){
userIdsToProcess.add(Usr.Id);
}
}
if(trigger.isUpdate){
if(Usr.Notify_me_of_releases__c && !trigger.oldMap.get(Usr.Id).Notify_me_of_releases__c){
userIdsToProcess.add(Usr.Id);
}
if(!Usr.Notify_me_of_releases__c && trigger.oldMap.get(Usr.Id).Notify_me_of_releases__c){
usersToBeRemoved.add(Usr.Id);
}
}
}
if(!userIdsToProcess.isEmpty() || !usersToBeRemoved.isEmpty()){
List<Group> alertGroup = [SELECT Id FROM Group Where DeveloperName='Alert' LIMIT 1];
if(!alertGroup.isEmpty()){
alertGroupId = alertGroup[0].Id;
}
}
for(User Usr : Trigger.New) {
if(Usr.Notify_me_of_releases__c && userIdsToProcess.contains(Usr.Id) && alertGroupId != null) {
GroupMember gm = new GroupMember();
gm.GroupId = alertGroupId;
gm.UserOrGroupId = Usr.Id;
grpMemlist.add(gm);
}
}
if(!usersToBeRemoved.isEmpty() && alertGroupId != null){
List<GroupMember> grpMemToBeDeleted = [SELECT Id FROM GroupMember WHERE GroupId = :alertGroupId AND UserOrGroupId IN :usersToBeRemoved];
if(!grpMemToBeDeleted.isEmpty()){
delete grpMemToBeDeleted;
}
}
if(!grpMemlist.isEmpty()) {
insert grpMemlist;
}
}
trigger AddingtoAlertPublicgroup on User (after insert,after Update) {
List<GroupMember> grpMemlist = new List<GroupMember>();
Set<Id> userIdsToProcess = new Set<Id>();
Set<Id> usersToBeRemoved = new Set<Id>();
Id alertGroupId;
for(User Usr : Trigger.New){
if(trigger.isInsert){
if(Usr.Notify_me_of_releases__c){
userIdsToProcess.add(Usr.Id);
}
}
if(trigger.isUpdate){
if(Usr.Notify_me_of_releases__c && !trigger.oldMap.get(Usr.Id).Notify_me_of_releases__c){
userIdsToProcess.add(Usr.Id);
}
if(!Usr.Notify_me_of_releases__c && trigger.oldMap.get(Usr.Id).Notify_me_of_releases__c){
usersToBeRemoved.add(Usr.Id);
}
}
}
if(!userIdsToProcess.isEmpty() || !usersToBeRemoved.isEmpty()){
List<Group> alertGroup = [SELECT Id FROM Group Where DeveloperName='Alert' LIMIT 1];
if(!alertGroup.isEmpty()){
alertGroupId = alertGroup[0].Id;
}
}
for(User Usr : Trigger.New) {
if(Usr.Notify_me_of_releases__c && userIdsToProcess.contains(Usr.Id) && alertGroupId != null) {
GroupMember gm = new GroupMember();
gm.GroupId = alertGroupId;
gm.UserOrGroupId = Usr.Id;
grpMemlist.add(gm);
}
}
if(!usersToBeRemoved.isEmpty() && alertGroupId != null){
List<GroupMember> grpMemToBeDeleted = [SELECT Id FROM GroupMember WHERE GroupId = :alertGroupId AND UserOrGroupId IN :usersToBeRemoved];
if(!grpMemToBeDeleted.isEmpty()){
delete grpMemToBeDeleted;
}
}
if(!grpMemlist.isEmpty()) {
insert grpMemlist;
}
}
The trigger seem to be ok, what issue you are facing currently?
Thanks,
Pratik
This is the working code to achive this:
The groupid, you can query on group name.
Trigger:
Variable on User: IsGroupMember__c (checkbox)
Thanks,
Pratik
Add user automatically in group based on "Notify me on new release" checkbox selected i have already done. But problem I am facing is when i have added user to public group based on role e.g. "Partner community manager" . There are 50 users belongs to "partner community manager" and i want to remove any one user from the group when i deselect "notify me on new relese " checkbox field in user detail page. But when i deselect "notify me on new relese " checkbox still user exist in group. Hope you can understand my problem.
What we noticed is that when we select the said checkbox "Notify me on new release" , the user is automatically added to Alert public group with User as Membership Type. Now if you add multiple users by, for example, "Portal Roles", then remove users by deselecting the checkbox, users are still kept as members of the group. This is because the reason of membership is different - "Portal Roles" while the checkbox only applies to "Users" Membership type.
Am i right????
I was able to reproduce the issues as per above mentioned details.
As you have added the users through the Role in Public group that is the reason it will override the checkbox uncheck and trigger.
If you add users to public group through Users and not roles then the trigger will execute properly.
If you want to have this requirement then i will suggest you to export the 50 users for the "Partner community manager" role and add them as users in Public group then the trigger logic will apply properly.
Thanks,
Pratik
One solution is either we replace that usrer to specic new group. But that is very pathetic . Everytime i have to do it manually.
Hope you understand my problem. Salesforce don't have unsubscribe facility so that user can unsubscribe email notification directly to click unsubscribe link.
Trigger.oldmap will have old value for the rcord so you can compare the prior value with current value.
Thanks,
Pratik