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
vivekanandanvivekanandan 

Chatter groups to receive posts when record is updated

We have a requirement , when an opp record is updated , it should create a post in the Chatter groups. Also the entry in the groups shouldbe based on the opp record field value.(groups names are based on that).

 

IS there any way to get the opp record updates in chatter groups.

alouie_sfdcalouie_sfdc

You can create a trigger on Opportunity and use Chatter in Apex (generally available in Summer '13) to search for the Chatter group and then post a feed item to that group. Here are some documentation links:

 

ConnectApi.ChatterFeeds class

http://www.salesforce.com/us/developer/docs/apexcode/Content/connectAPI_ChatterFeedsClass.htm

 

ConnectApi.ChatterGroups class

http://www.salesforce.com/us/developer/docs/apexcode/Content/connectAPI_ChatterGroupsClass.htm

 

Working with Chatter in Apex

http://www.salesforce.com/us/developer/docs/apexcode/Content/connectAPI_overview.htm

spatelcespatelce

Hope this code will help you:

 

trigger ChatterWonOpportunity on Opportunity (after insert, after update) {

String status;
String OppAccName;
//Decimal OppAmount;
FeedItem post = new FeedItem();
    
    for(Opportunity o : Trigger.new) {
        if(Trigger.isInsert) {
            if(o.StageName == 'Prospecting') {
                for (Opportunity oppty : [SELECT Account.Name FROM Opportunity WHERE Id =:o.Id] ) {
                        OppAccName = oppty.Account.Name;
                }
            }
            status = 'We have a new ' + o.Name + ' opportunity with ' + OppAccName;
            
            post.ParentId = //Your Chatter Group ID
            post.Title = o.Name;
            post.Body = status;
        }
        else {
            if(Trigger.isUpdate) {
                if(Trigger.oldMap.get(o.id).Amount != o.Amount) {
                    for (Opportunity oppty : [SELECT Account.Name FROM Opportunity WHERE Id =:o.Id] ) {
                        OppAccName = oppty.Account.Name;
                    }
                    status = 'The value proposition for ' + o.Name + ' opportunity with ' + OppAccName + ' has increased from ' +  Trigger.oldMap.get(o.Id).Amount + ' to ' + o.Amount + '.';
                }
                else {
                    if(o.IsWon == true && Trigger.oldMap.get(o.id).IsWon == false) {
                    for (Opportunity oppty : [SELECT Account.Name FROM Opportunity WHERE Id =:o.Id] ) {
                        OppAccName = oppty.Account.Name;
                    }
                    status = 'We just won the ' + o.Name + ' opportunity with ‘ + OppAccName + ‘!';
                    }
                    else {
                        return;
                    }
                }
            
            post.ParentId = //Your Chatter Group ID
            post.Title = o.Name;
            post.Body = status;
            }
        }
        insert post;
    }
}