Skip to main content
Neeraja Suribhatla (KQED) 님이 #Apex에 질문했습니다
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;

                    }

      }

        

}
답변 7개
  1. 2015년 9월 17일 오전 3:18
    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.
0/9000