• Rohit Srivastava
  • NEWBIE
  • 10 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
I have a requirement where I need to make the owner follow a record based on certain conditions. Now when I change the owner, the new owner should automatically follow the record. However the code I have written is making both the old and new owner follow the record. How to make the the old owner unfollow the record? 
Below is the piece of code I have written and I am calling this method from trigger on after insert and after update:

public static String doFollowRecord(List<Opportunity> opportunities, Map<Id, Opportunity> oldMap, Map<Id, Opportunity> newMap) {
        String errMsg;
        if(isInsert){
        for (Opportunity opp : opportunities) {
            if (opp.RecordTypeId == opportunityRecordTypeNameToIdMap.get(RENEWALRECORDTYPE.toLowerCase()) && opp.Managed_Opportunity__c ==True) {
                EntitySubscription follow = new EntitySubscription (
                                         parentId = opp.id,
                                         subscriberid =opp.ownerid);
                insert follow;
            }
                                                }
                                }  
        return errMsg;
    }


Need urgent help on this.
I have a requirement where I need to make the owner follow a record based on certain conditions. Now when I change the owner, the new owner should automatically follow the record. However the code I have written is making both the old and new owner follow the record. How to make the the old owner unfollow the record? 
Below is the piece of code I have written and I am calling this method from trigger on after insert and after update:

public static String doFollowRecord(List<Opportunity> opportunities, Map<Id, Opportunity> oldMap, Map<Id, Opportunity> newMap) {
        String errMsg;
        if(isInsert){
        for (Opportunity opp : opportunities) {
            if (opp.RecordTypeId == opportunityRecordTypeNameToIdMap.get(RENEWALRECORDTYPE.toLowerCase()) && opp.Managed_Opportunity__c ==True) {
                EntitySubscription follow = new EntitySubscription (
                                         parentId = opp.id,
                                         subscriberid =opp.ownerid);
                insert follow;
            }
                                                }
                                }  
        return errMsg;
    }


Need urgent help on this.