How to add users to Public Group using Apex in Salesforce?

Sample Trigger:

trigger AddToPG on User (after insert) {
    List<GroupMember> GMlist = new List<GroupMember>();
    for(User U : Trigger.New) {
        if(U.isActive) {
            GroupMember GM = new GroupMember();
            GM.GroupId = '00GU0000001Zjeq';
            GM.UserOrGroupId = U.Id;
    if(!GMList.isEmpty()) {
        System.debug('Group Member List is ' + GMList);
        insert GMList;

No comments:

Post a Comment

Sharing is Caring!!! Share this with your friends!!!

submit to reddit