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
viv1viv1 

change Public Group Membership

how to change Public Group Membership of user in trigger ??

or through apex code ???

Vinit_KumarVinit_Kumar

Hi Viv,

 

Please try below :-

 

 

List<GroupMember>gpm=[SELECT Id,UserOrGroupId,groupId FROM GroupMember where UserOrGroupId=<Some id>];

List<Id> UserIds = new List<Id>();

for(GroupMember fm : gpm){
UserIds.add(fm.UserOrGroupId);
}

delete gpm;

Group gp = new Group(Name='ABC',Type = 'Regular');
insert gp;

List<GroupMember> gpmnew = new List<GroupMember>();
for(integer i=0;i<UserIds.size();i++){
for(Id idd:UserIds){
GroupMember gm =  new GroupMember(GroupId=gp.id,UserOrGroupId=idd);
gpmnew.add(gm);
}
}

insert gpmnew;

 

viv1viv1

trigger createUser on Student__c (after insert) {
    Profile p = [select id from Profile where name='Student'];
    Student__c stud=[select user__c,Email_ID__c,First_Name__c,Last_Name__c,Username__c,id from Student__c where id=:Trigger.new[0].id];
    User u = new User(alias = 'student', CommunityNickname=stud.Username__c,email=stud.Email_ID__c, emailencodingkey='UTF-8', firstName=stud.First_Name__c, lastname=stud.Last_Name__c, languagelocalekey='en_US', localesidkey='en_US', profileid = p.id, timezonesidkey='Europe/London', username=sabcohc);
    insert u;
    System.resetPassword(u.Id, true);
    stud.User__c=u.id;
    update stud;    
   
}

 

this is my trigger and thid is my group id :

00G90000000aEMr
 
how i will assign that user to this group ??
Vinit_KumarVinit_Kumar

Try below :-

 

trigger createUser on Student__c (after insert) {
    Profile p = [select id from Profile where name='Student'];
    Student__c stud=[select user__c,Email_ID__c,First_Name__c,Last_Name__c,Username__c,id from Student__c where id=:Trigger.new[0].id];
    User u = new User(alias = 'student', CommunityNickname=stud.Username__c,email=stud.Email_ID__c, emailencodingkey='UTF-8', firstName=stud.First_Name__c, lastname=stud.Last_Name__c, languagelocalekey='en_US', localesidkey='en_US', profileid = p.id, timezonesidkey='Europe/London', username=sabcohc);
    insert u;
    System.resetPassword(u.Id, true);
    stud.User__c=u.id;
    update stud;
   

   GroupMember gm =  new GroupMember(GroupId='00G90000000aEMr',UserOrGroupId=u.id);
    insert gm    ; // This will add the user to the group
   
}

 

viv1viv1

i have already tried that..

but its giving me this error :

 

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger createUser caused an unexpected exception, contact your administrator: createUser: execution of AfterInsert caused by: 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): GroupMember, original object: Student__c: []: Trigger.createUser: line 11, column 1

Vinit_KumarVinit_Kumar

Viv,

 

You can create a Handler class for the Trigger and there you can have two methods one future and other synchronous.Then,you won't hit this error as you will be updating in different context.