function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ai2ai2 

Enabling subscriptions on objects on a custom site

I have a custom site that's hosted on my dev chatter account.  I would like to be able to include an action to follow to a custom object.  News feeds for the object are already enabled.  In the controller, I'm having trouble adding an EntitySubscription to a user's subscriptions for that custom object.  

This seems feasible and very easy but I can't figure it out.  I'd appreciate any help on this topic.  Thanks in advance.
Message Edited by ai2 on 03-25-2010 12:15 PM
infmsinfms
I'm having the same issue.  Any luck?
Message Edited by infms on 03-25-2010 12:36 PM
d3developerd3developer
Post some code?
d3developerd3developer

Here's the full code for a trigger that does just that:

 

 

trigger updateFollowers on ToDoItem__c (after insert, after update) { String oldAssignee; String oldManagedBy; String newAssignee; String newManagedBy; if(Trigger.isUpdate){ for (ToDoItem__c tdi : Trigger.new){ oldAssignee = Trigger.oldMap.get(tdi.ID).assigned_to__c; oldManagedBy = Trigger.oldMap.get(tdi.ID).managed_by__c; newAssignee = Trigger.newMap.get(tdi.ID).assigned_to__c; newManagedBy = Trigger.newMap.get(tdi.ID).managed_by__c; if (newAssignee != oldAssignee) { removeFollower(oldAssignee, tdi); addFollower(newAssignee, tdi); } if (newManagedBy != oldManagedBy) { removeFollower(oldManagedBy, tdi); addFollower(newManagedBy, tdi); } } } if(Trigger.isInsert){ for (ToDoItem__c tdi : Trigger.new){ newAssignee = Trigger.newMap.get(tdi.ID).assigned_to__c; newManagedBy = Trigger.newMap.get(tdi.ID).managed_by__c; addFollower(newAssignee, tdi); addFollower(newManagedBy, tdi); } } public void addFollower(String userid, SObject objectToFollow) { //Creator of object is automatically subscribed List <EntitySubscription> existingSubs = [SELECT id from EntitySubscription where parentid=:objectToFollow.id and subscriberid=:userid]; if (existingSubs.isEmpty()) { EntitySubscription e = new EntitySubscription(); e.parentId = objectToFollow.id; e.subscriberid = userid; insert e; } } public void removeFollower(String userid, SObject objectToStopFollowing) { try { EntitySubscription e = [SELECT id from EntitySubscription where parentid=:objectToStopFollowing.id and subscriberid=:userid limit 1]; delete e; } // Given that all subscriptions within this app are automated, there should always be subscriptions. catch (ListException le) { System.debug(Logginglevel.WARN, 'No subscription was found for ToDoItem:' + objectToStopFollowing.id + ' and user: ' + userid); } } }

 

And here is my test code:

 

 

@isTest private class TESTupdateFollowers { public static testMethod void doTest() { Profile p = [select id from profile where name='Standard User']; User uat123 = new User(alias = 'AT123', email='test123@noemail.com', emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US', localesidkey='en_US', profileid = p.Id, country='United States', timezonesidkey='America/Los_Angeles', username='at123@noemail.com'); insert uat123; User umb123 = new User(alias = 'MB123', email='test123@noemail.com', emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US', localesidkey='en_US', profileid = p.Id, country='United States', timezonesidkey='America/Los_Angeles', username='mb123@noemail.com'); insert umb123; ToDoItem__c tdi = new ToDoItem__c(); tdi.name = 'Foo Task'; tdi.assigned_to__c = umb123.id; tdi.managed_by__c = uat123.id; insert tdi; System.assertEquals(1, [SELECT id from EntitySubscription where subscriberid=:umb123.id and parentid=:tdi.id].size()); System.assertEquals(1, [SELECT id from EntitySubscription where subscriberid=:uat123.id and parentid=:tdi.id].size()); User uat321 = new User(alias = 'AT321', email='test321@noemail.com', emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US', localesidkey='en_US', profileid = p.Id, country='United States', timezonesidkey='America/Los_Angeles', username='at3213@noemail.com'); insert uat321; User umb321 = new User(alias = 'MB321', email='test321@noemail.com', emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US', localesidkey='en_US', profileid = p.Id, country='United States', timezonesidkey='America/Los_Angeles', username='mb321@noemail.com'); insert umb321; tdi.assigned_to__c = uat321.id; tdi.managed_by__c = umb321.id; update tdi; System.assertEquals(0, [SELECT id from EntitySubscription where subscriberid=:umb123.id].size()); System.assertEquals(0, [SELECT id from EntitySubscription where subscriberid=:uat123.id].size()); System.assertEquals(1, [SELECT id from EntitySubscription where subscriberid=:umb321.id and parentid=:tdi.id].size()); System.assertEquals(1, [SELECT id from EntitySubscription where subscriberid=:uat321.id and parentid=:tdi.id].size()); } }