You need to sign in to do that
Don't have an account?
Neeraja
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;
}
}
}
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;
}
}
}
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 :
Let me know if you need more help.
Regards,
Abhishek.
All Answers
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.
Hope that helps,
Clint
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 :
Let me know if you need more help.
Regards,
Abhishek.
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.
Thanks,
Abhishek