Here are two apex applications that can help you to automate chatter.
1) The following Apex trigger will make users auto follow the records that they create for Opportunity Object.
trigger OppAutoFollowOwner on Opportunity (after insert) {
List NewFollowers = new List ();
for(Opportunity sampleOpp : trigger.new){
EntitySubscription ES = new EntitySubscription();
ES.SubscriberId = sampleOpp.CreatedById;
ES.ParentId = sampleOpp.Id;
NewFollowers.add (ES);
}
try{
insert NewFollowers;
}
catch (System.DmlException e){
}
}
2) Apex Trigger to make Posts on a chatter group.
Sometimes you want your apex code to make automatic posts on a chatter group, if a record is updated and some criteria is met for that record.
for ex- If an Opportunity is closed Won and you want a chatter feed to be sent automatically to Managers Group regarding it.
The following trigger illustrates this for some custom Objects.
trigger Temp_RequestPriceChangeNotificationSender on Temp_Request__c (after update) {
List tempRequestList=[Select g.Id, g.Name,g.User_BU__c, g.Rates_Valid_From__c, g.Rates_Valid_To__c, g.CreatedBy.Username, g.Company_Number__c, g.Company_Name__c, g.CreatedById,
(Select Id, Name, Product__c, Original_Price__c, Product_Price__c, GRA_Product_Param__c From Temp_Request_Lines__r ) From Temp_Request__c g WHERE Id IN : trigger.new and g.status__c='Submitted'];
// CollaborationGroup CoG =[SELECT Id,Name, Description, CollaborationType, OwnerId from CollaborationGroup where Name = 'Managers Group'];
CollaborationGroup CoG1 =[SELECT Id,Name, Description, CollaborationType, OwnerId from CollaborationGroup where Name = 'EMEA Managers Group'];
CollaborationGroup CoG2 =[SELECT Id,Name, Description, CollaborationType, OwnerId from CollaborationGroup where Name = 'NA Managers Group'];
CollaborationGroup CoG3 =[SELECT Id,Name, Description, CollaborationType, OwnerId from CollaborationGroup where Name = 'APAC Managers Group'];
List feItem=new List();
for(Temp_Request__c temp : tempRequestList){
String tempRequestURL = URL.getSalesforceBaseUrl().toExternalForm() + '/' + temp.id;
FeedItem post = new FeedItem();
String rlText = '';
String text = 'Price Increase - Company :' + temp.Company_Name__c+'(Company Number - '+temp.Company_Number__c+')\n\n' ;
text = text
+'Submitted By : '+temp.CreatedBy.Username
+'\nValid from : '+ temp.Rates_Valid_From__c+'\n';
if(temp.User_BU__c.equals('US')){
post.ParentId = Cog2.Id;
}else if(temp.User_BU__c.equals('APAC')){
post.ParentId = Cog3.Id;
}else if(temp.User_BU__c.equals('EMEA')){
post.ParentId = Cog1.Id;
}
for(Temp_Request_Line__c requestLine : temp.Temp_Request_Lines__r){
if(requestLine.Product_Price__c > requestLine.Original_Price__c){
rlText = rlText
+'\n Price Changed for GRA Request Line '+ URL.getSalesforceBaseUrl().toExternalForm() + '/' +requestLine.id ;
}
}//ends inner for
if(rlText != null && !rlText.equals('')){
post.Body = text + rlText ;
post.Title = 'Price Increase for GRA Request '+temp.Name;
post.LinkUrl = tempRequestURL;
post.Type = 'LinkPost';
feItem.add(post);
}
}//ends outer for
if(feItem.size() > 0){
Database.insert(feItem);
}
}
1) The following Apex trigger will make users auto follow the records that they create for Opportunity Object.
trigger OppAutoFollowOwner on Opportunity (after insert) {
List
for(Opportunity sampleOpp : trigger.new){
EntitySubscription ES = new EntitySubscription();
ES.SubscriberId = sampleOpp.CreatedById;
ES.ParentId = sampleOpp.Id;
NewFollowers.add (ES);
}
try{
insert NewFollowers;
}
catch (System.DmlException e){
}
}
List
(Select Id, Name, Product__c, Original_Price__c, Product_Price__c, GRA_Product_Param__c From Temp_Request_Lines__r ) From Temp_Request__c g WHERE Id IN : trigger.new and g.status__c='Submitted'];
// CollaborationGroup CoG =[SELECT Id,Name, Description, CollaborationType, OwnerId from CollaborationGroup where Name = 'Managers Group'];
CollaborationGroup CoG1 =[SELECT Id,Name, Description, CollaborationType, OwnerId from CollaborationGroup where Name = 'EMEA Managers Group'];
CollaborationGroup CoG2 =[SELECT Id,Name, Description, CollaborationType, OwnerId from CollaborationGroup where Name = 'NA Managers Group'];
CollaborationGroup CoG3 =[SELECT Id,Name, Description, CollaborationType, OwnerId from CollaborationGroup where Name = 'APAC Managers Group'];
List
for(Temp_Request__c temp : tempRequestList){
String tempRequestURL = URL.getSalesforceBaseUrl().toExternalForm() + '/' + temp.id;
FeedItem post = new FeedItem();
String rlText = '';
String text = 'Price Increase - Company :' + temp.Company_Name__c+'(Company Number - '+temp.Company_Number__c+')\n\n' ;
text = text
+'Submitted By : '+temp.CreatedBy.Username
+'\nValid from : '+ temp.Rates_Valid_From__c+'\n';
if(temp.User_BU__c.equals('US')){
post.ParentId = Cog2.Id;
}else if(temp.User_BU__c.equals('APAC')){
post.ParentId = Cog3.Id;
}else if(temp.User_BU__c.equals('EMEA')){
post.ParentId = Cog1.Id;
}
for(Temp_Request_Line__c requestLine : temp.Temp_Request_Lines__r){
if(requestLine.Product_Price__c > requestLine.Original_Price__c){
rlText = rlText
+'\n Price Changed for GRA Request Line '+ URL.getSalesforceBaseUrl().toExternalForm() + '/' +requestLine.id ;
}
}//ends inner for
if(rlText != null && !rlText.equals('')){
post.Body = text + rlText ;
post.Title = 'Price Increase for GRA Request '+temp.Name;
post.LinkUrl = tempRequestURL;
post.Type = 'LinkPost';
feItem.add(post);
}
}//ends outer for
Database.insert(feItem);
}
}
No comments:
Post a Comment