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
beckywillisbeckywillis 

Auto Follow Community Members - Why is my code not working?

A previous Developer in my org included this code in our Community Member Registration class: 

private void addAccountOwnerFollow ()
    {
        /*Auotmatically have the account owner follow the new community user*/
        
        try
        {
            EntitySubscription es = new EntitySubscription();
            es.ParentId = newCommunityUser.Id;
            es.SubscriberId = userAccount.OwnerId;
            insert es;
        }

It does not seem to be working - so is something wrong with it, or is auto-following community members when they register simply not possible for owners? (Yes, I've looked into the follower limit and that's not the case.)
NagendraNagendra (Salesforce Developers) 
Hi Becky,

Please check with below link from forums community with similar issue and suggested workaround. Hope this helps.

Please mark this as solved if it's resolved.

Regards,
Nagendra.
beckywillisbeckywillis
Thanks @Nagendra; however, it didn't work the same for me. This is what I have now: 


private void addAccountOwnerFollow ()
    {
        /*Auotmatically have the account owner follow the new community user*/
        
        try
        {
            Id aid = trigger.new[0].Id;
            EntitySubscription es = new EntitySubscription();
            es.NetworkId = Network.getNetworkId();
            es.ParentId = newCommunityUser.Id;
            es.SubscriberId = userAccount.OwnerId;
                        insert es;
        }
        catch (Exception e) 
        {
                system.debug(logginglevel.ERROR, 'Error in CommunitiesSelfRegController:addAccountOwnerFollow at ' + e.getLineNumber() + ': ' + e.getMessage());        
        }