You need to sign in to do that
Don't have an account?
Rohit Srivastava
Need Urgent Help on Record Unfollow through Apex
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.
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.
Here is the Complete Code with Bulikified Support, Add you condition in the below class if you want, This is a generic trigger,
Trigger:
Apex Class:
Mark this as a best answer, if this solution helpful to you...!
All Answers
I glad to help you.
To achiive this. We have to fire the trigger in two conditions:
- After Insert: In this, We have to insert the record in the EntitySubscription object as you have mentioned in the thread.
- After Update: In this, We should insert the record in the EntitySubscription object for the new owner and delete a record for the older owner.
Here is the sample code for the "2" point:SOQL for delete a record: I would like to give one suggest to write the trigger in a best way that you should use the factory class. Please find the link here - http://developer.force.com/cookbook/recipe/trigger-pattern-for-tidy-streamlined-bulkified-triggers
Please Mark this as Best Answer if it helps you
Regards,
Grazitti Team
Web: www.grazitti.com
Email: sfdc@grazitti.com
Here is the Complete Code with Bulikified Support, Add you condition in the below class if you want, This is a generic trigger,
Trigger:
Apex Class:
Mark this as a best answer, if this solution helpful to you...!