+ Start a Discussion
NeerajaNeeraja 

I need help with apex trigger. I am trying to write a trigger when opportunity stage is equal to closed won I need to auto unfollow the opportunity record. Any help would be greatly appreciated. thanks

trigger Autounfollowopp on Opportunity (after insert,after update) {
set<id> oppid = new set<id>();
    for(opportunity opp:trigger.new){
        if(opp.StageName=='closed won')
            oppid.add(opp.Id);
           }
    list<opportunity> opportunity=[select id, createdby.name from opportunity where id in:oppid];
    
    for(user users : [select id from user ])
        {
          EntitySubscription[] followingES = [select id, parentid, subscriberid, parent.name from EntitySubscription where subscriberid = :users.id];
            Map<string,string> following = new Map<string,string> ();
            for( EntitySubscription Entitysub : followingES )
            following.put( Entitysub.parentid, Entitysub.parent.name );  
             If(opportunity [0].createdby.name == 'xxxx'
                    {
                    EntitySubscription follow = new EntitySubscription (parentId <>:opportunity [0].id,subscriberid = users.id);
                    delete follow;
                    }
      }
        
}
Best Answer chosen by Neeraja
Abhishek BansalAbhishek Bansal
Hi Neeraja,

As per my understanding your requirement is simply to unfollow an Opportunity record if ts stage is "Closed Won" for all the users who are following it.
If i am right than please use the below code to acheive what you want :
 
trigger Autounfollowopp on Opportunity (after insert,after update) {
	Set<Id> entityParentIds = new Set<Id>();
	for(opportunity opp:trigger.new){
        if(opp.StageName=='closed won'){
        	entityParentIds.add(opp.id);
        }
    }
	
	delete [Select Id from EntitySubscription where ParentId IN : entityParentIds];
}

Let me know if you need more help.

Regards,
Abhishek.

All Answers

ClintLeeClintLee
It's not clear exactly from your question who should unfollow the opportunity.  Do you need to remove all followers or just the user who created the opportunity?

The following trigger will remove the User who created the Opportunity from the follower list.  However, if additional users have followed the opportunity it will not remove them.
 
trigger autoUnfollow on Opportunity (after insert, after update) 
{
    Map<Id,Id> oppIdToCreatedById = new Map<Id,Id>();

    for(Opportunity opp : Trigger.New)
    {
        if(opp.StageName == 'Closed Won') {
            oppIdToCreatedById.put(opp.Id, opp.CreatedById);
        }
    }
    
    List<EntitySubscription> subscriptionsToDelete = new List<EntitySubscription>();

    for(EntitySubscription es : [select Id, ParentId, SubscriberId from EntitySubscription where ParentId IN :oppIdToCreatedById.keySet()])
    {
        if(oppIdToCreatedById.get(es.ParentId) == es.SubscriberId) {
            subscriptionsToDelete.add(es);
        }
    }
     
    delete subscriptionsToDelete;   
}

Hope that helps,

Clint
Abhishek BansalAbhishek Bansal
Hi Neeraja,

As per my understanding your requirement is simply to unfollow an Opportunity record if ts stage is "Closed Won" for all the users who are following it.
If i am right than please use the below code to acheive what you want :
 
trigger Autounfollowopp on Opportunity (after insert,after update) {
	Set<Id> entityParentIds = new Set<Id>();
	for(opportunity opp:trigger.new){
        if(opp.StageName=='closed won'){
        	entityParentIds.add(opp.id);
        }
    }
	
	delete [Select Id from EntitySubscription where ParentId IN : entityParentIds];
}

Let me know if you need more help.

Regards,
Abhishek.
This was selected as the best answer
pigginsbpigginsb
Hi, Neeraja. Do you need to write a trigger for this? The AppExchange has an Unfollow app to do this, where you just set up the rules. https://appexchange.salesforce.com/listingDetail?listingId=a0N30000003IP2gEAG
NeerajaNeeraja
@ClintLee @Abhishek Bansal @bjpiggins Thank you so much. It helped me alot. Logged in user has to unfollow the opportunity record is my requirement.
Abhishek BansalAbhishek Bansal
Hi,

Please change this line : delete [Select Id from EntitySubscription where ParentId IN : entityParentIds];
TO : delete [Select Id from EntitySubscription where ParentId IN : entityParentIds AND SubscriberId = :UserInfo.getUserId()];

This will unfollow the Opportunity record for logged in user.

Regards,
Abhishek.
 
NeerajaNeeraja
Thank you so much. 
Abhishek BansalAbhishek Bansal
If you do not have any further queries on this question than please close it by marking it as Solved.

Thanks,
Abhishek