+ Start a Discussion
Andy S.Andy S. 

Auto-follow new opportunities on followed Account

I am following an Account and want to be notified whenever a new Opportunity is created for that account.  Is that available out of the box with Chatter?  It seems somewhat obvious but I can't find an FAQ or discussion board entry on it.

 

If this is not available today, would an apex Trigger be the way to create such functionality?

jkucerajkucera

It is not - check out Chatter Cloud Swarm on the AppExchange for some code on how this can be done.  

 

I'm working with the author to refactor the existing package to make it scale better.  Here's a sample of the unpublished new code: a trigger + asynch method:

 

 

trigger opptySwarm on Opportunity (after insert, after update) {
   List<Id> opptyIds=new List<Id>();
   for (Opportunity o: trigger.new){
     opptyIds.add(o.Id);
   }//for
   
   opptySwarm.evaluateOpptyRules(opptyIds);
}

 

public global class opptySwarm{
    
    @future
    public static void evaluateOpptyRules(List<Id> opptyIds){
        List<Opportunity> opptys=[SELECT Id, Type, AccountId, StageName, Amount FROM Opportunity WHERE Id IN:opptyIds];
        
        String userType = Schema.SObjectType.User.getKeyPrefix();
        String groupType = Schema.SObjectType.Group.getKeyPrefix();
    
        List<Opportunity_Swarm_Rule__c> rules = [select id, type__c, Opportunity_amount__c, Opportunity_stage__c, Opportunity_type__c, user__c, group_id__c, ownerId 
                    from Opportunity_Swarm_Rule__c];
        List<EntitySubscription> subs=new List<EntitySubscription>();
        
        List<EntitySubscription> existingOpptySubs=[select SubscriberId, ParentId from EntitySubscription where ParentId in :opptys];
        Set<String> existingOpptySubsIds=new Set<String>();
        for (EntitySubscription es:existingOpptySubs){
            existingOpptySubsIds.add((String)es.SubscriberId+es.ParentId);
        }//for
        
        
        Set<Id> acctIds=new Set<Id>();
        for (Opportunity o:opptys){
            if(acctIds.contains(o.AccountId)==FALSE){
                acctIds.add(o.AccountId);
            }//if
        }//for
    
        List<Account> accts=[SELECT ownerId, Id FROM Account WHERE Id IN :acctIds];
        Map<Id,Id> acctOwnerIds=new Map<Id, Id>();
        for (Account a: accts){
            acctOwnerIds.put(a.Id, a.ownerId);
        }//for
    //logic - for all opportunities, 
    //check if any     
        for (Opportunity thisOppty : opptys) {
        
            for (Opportunity_Swarm_Rule__c rule: rules) {
    
                if (rule.Type__c.equals('All Opportunities') ||
                    (rule.Type__c.equals('Only Opportunities for Accounts I Own') && thisOppty.AccountId != null &&
                        rule.OwnerId == acctOwnerIds.get(thisOppty.AccountId)) ||
                    (rule.Type__c.equals('Only Opportunities above a certain amount') && 
                        rule.Opportunity_Amount__c <= (thisOppty.Amount)) ||
                    (rule.Type__c.equals('Only Opportunities that reach a certain stage in the sales process') && 
                        rule.Opportunity_Stage__c.equals(thisOppty.StageName)) ||
                    (rule.Type__c.equals('Only Opportunities of a certain Type') && 
                        rule.Opportunity_Type__c.equals(thisOppty.Type))                
                    ) {
                    
                    if ( existingOpptySubsIds.contains((string)rule.User__c+thisOppty.Id)==FALSE){
                        subs.add(new EntitySubscription(parentId=thisOppty.id, SubscriberId=rule.User__c));
                    }//if 2
                }//if 1
            }//for 2  rules
        }//for 1 oppty's
        try{
            insert subs;
        }catch (DMLException e){
            system.debug('Oppty Swarm subscriptions were not all inserted successfully.  Error: '+e);
        }//catch
        
    }//evaluateOpptyRules

}//class

 

 

newbie2010newbie2010

Along these same lines it would be nice to be able to auto subscribe other users when an object is created.  In our company the sales rep is the owner so they are automatically subscribed to follow, but they have a service rep that needs to follow the account as well and they are automatically assigned to the account so there should be a way during this assignment to subscribe them to the account.

kabkab

Hi John,

 

Is there a way I can write a trigger on the custom object to Post an Idea to a group or profile?

I tried with Feed post insert and it displays for only one user  who creates the record on a custom object not the whole group who are following.

FeedPost fp=new FeedPost();

fp.Bosy='Test Post to all Group';

 

insert fp;

 

Also Is there anyway/code  I can dynamically set the Feed tracking of a field on a custom object?

 

 

thanks

 

jkucerajkucera

When you create a post, it always has to originate from somewhere or something.  That means if you post to a group, everyone in the group will see the post so I think that will work for your purposes.  If you post not to a group, then only the people following the posting user will see it.

 

I don't believe you can set field tracking settings in Apex, but you might be able to use the Metadata API to set these from another service

kabkab

Hi John,

Thanks for valuable comment.

Can you please post the apex code , How to post  a msg to group?

 

thanks a lot

d_bizzied_bizzie

Hi John,

 

I was wondering if the code for auto following opportunities when a user is following accounts, has been implemented into any packages/apps availble through the app exchange?

 

I have installed the chatter app "Cloud Swarm 3" but this does not provide the auto-follow functionality for opportunities when following accounts.

 

Thanks.

jkucerajkucera

As written, the code & app won't let you cross objects where you follow opportunities based on Account criteria, but it's not terribly hard to support that with some custom logic.  I don't belive the author is working to support that case, but I can check with him.

Felix KdtFelix Kdt

Taking this szenario where a user automatically follows opportunities when following the parent account (implemented in a trigger). Is there a way to programmatically unfollow all these opportunities on unfollowing the account? I don't see that it can be done by a trigger on EntitySubscription. Can it be done any way?

 

Thanks

Felix Kroppenstedt