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
ckellieckellie 

How to AutoFollow a group of People

I am trying to write a trigger that will autofollow a group users to every new record in the object MPS__c

 

I have been able to automatically follow one user to the record, but when I try to autofollow additional users, I recieve the following error:

 

Error: Compile Error: Duplicate field initialization: subscriberid at line 24 column 34

 

 

Here is my trigger:

 

 

trigger MPSFollowers on MPS__c (After Insert) {

    Set<Id> mIds = new Set<Id>();
    Set<Id> fId = new Set<Id>();
    for(MPS__c mps :trigger.new) {
    
        mids.add(mps.id);
        System.debug('**** 1 mps id : '+ mps.id);
    }
    
   List<MPS__c> p = [select id from MPS__c where id in :mIds];

   
   List<User> u1 = [select id from User where name = 'Shawn Prendiville'];
   List<User> u2 = [select id from User where name = 'Mariann Harmon'];
   List<User> u3 = [select id from User where name = 'Paul van Vugt'];
   List<User> u4 = [select id from User where name = 'Gary Brown'];
   List<User> u5 = [select id from User where name = 'Dave Setzer'];
                    
    EntitySubscription follow = new EntitySubscription (

                                 parentId = p[0].id,
                                 subscriberid = u1[0].id,
                                 subscriberid = u2[0].id
                );

    insert follow;


}

 

 

How do I bulkify this trigger and assign multiple users to follow the record?

 

Thank you,

ckellie

Best Answer chosen by Admin (Salesforce Developers) 
saurabhEventsaurabhEvent

I believe you will have to loop through the users which you intend to follow the your entity to create multiple entity subscription records.

All Answers

saurabhEventsaurabhEvent

I believe you will have to loop through the users which you intend to follow the your entity to create multiple entity subscription records.

This was selected as the best answer
ckellieckellie

Thank you very much.

Amit Singh1989Amit Singh1989

Hi ckellie

 

I am facing the same problem,could you please provide me sample code for this.

 

 

Thanks

jaw999jaw999

Was this solved, would love to see how you solved it. Thanks

ckellieckellie

This was solved:

trigger MPSFollowers on MPS__c (After Insert) {

    Set<Id> mIds = new Set<Id>();
    public boolean isTest               { get; set; }
    public integer followingcount       { get; set; }
    for(MPS__c mps :trigger.new) {
    
        mids.add(mps.id);
        System.debug('**** 1 mps id : '+ mps.id);
    }

   List<MPS__c> p = [select id, createdby.name from MPS__c where id in :mIds];
 
   for(user u : [select id from user where name = 'Shawn Prendiville'
                                       or name = 'Mariann Harmon'
                                       or name = 'Ronald Bontan'
                                       or name = 'Alfred van der Wal'
                                       or name = 'Gary Brown'
                                       or name = 'Daniel Leighty']){
   
   EntitySubscription[] followingES = [select id, parentid, subscriberid, parent.name
            from EntitySubscription where subscriberid = :u.id];
   
    Map<string,string> following = new Map<string,string> ();
    
    for( EntitySubscription es : followingES )

                following.put( es.parentid, es.parent.name );  

        // All following
        followingcount = following.size();
   system.debug(followingcount);
    If(p[0].createdby.name != '' && followingcount < 350){
    EntitySubscription follow = new EntitySubscription (

                                 parentId = p[0].id,
                                 subscriberid = u.id
                             
                );

    insert follow;

    }
    }
        
}