Saleforce.com APEX - Chatter Trigger Tutorial
by
, 06-28-2010 at 11:25 AM (6832 Views)
The newest Salesforce.com feature is Chatter. Chatter is a brand new way to collaborate with people at work using your Salesforce.com Organization. Chatter is the Facebook application for the business world. You can follow fellow employees and receive real time updates via email from their chatter feeds. Whether it is an update to an account or opportunity, Chatter works great.
Chatter feeds can be updated using APEX, Salesforce.com's on-demand programming language. By using APEX triggers, a powerful tool that allows for other objects and items to be updated in almost real-time. The following is an APEX Chatter trigger tutorial which performs an update to the Opportunity Chatter feed when someone updates a field on the Opportunity Line Item. Developers can program business logic into APEX triggers, which allows for applications to be developed on the Salesforce platform.
Salesforce has built in governors within their application because it is a multi-tenant platform. One of these governors does not allow for a SOQL (Salesforce Object Query Language) to perform more than 20 queries at a time and will cause an exception to be thrown.
The following APEX Chatter Trigger updates the Opportunity Chatter feed from changes that have occurred on the child object - Opportunity line Item
This APEX trigger is called after update, which means that when a user updates the opportunity line item and clicks the save button, the code is invoked. The business logic checks to see if two fields are changed and if the requirements are met, the Chatter feed is updated.Code:trigger new_chatter_update_trigger on OpportunityLineItem (after update) { //Setup the Feedpost array of items to post to Chatter List<FeedPost> posts = new List<FeedPost>(); //Setup the array to hold the ids to Iterate through Set<Id> pbeIds = new Set<Id>(); //Iterate through the Line Items for (OpportunityLineItem oli : Trigger.new) { // Create individual post pbeIds.add(oli.PricebookEntryId); pbeIds.add(oli.opportunityId); } //Setup APEX MAP Arrays to get the required data from the Pricebook and Opportunity Map<Id, PricebookEntry> entries = new Map<Id, PricebookEntry>([select Product2.ProductCode from PricebookEntry where id in :pbeIds]); Map<Id, Opportunity> Opp = new Map<Id, Opportunity>([select Account.Name, Account.ID from Opportunity where id in :pbeIds]); //Iterate through the line items once again to add the data to the Chatter Object for (OpportunityLineItem oli : Trigger.new) { OpportunityLineItem oldOLI = Trigger.oldMap.get (oli.id); //If the part outcomes are different, let's add to the chatter feed if(oli.Part_Outcome__c != oldOli.Part_Outcome__c) { //Let's add the PriceBookEntrty Id to ge the Product name String bodyText = 'has updated the ' +entries.get(oli.Pricebookentryid).Product2.ProductCode+ ' from ' +oldOLI.Part_Outcome__c+ ' to ' +oli.Part_Outcome__c+ ' on Account: ' +opp.get(oli.OpportunityId).Account.Name+''; FeedPost opportunityPost = new Feedpost(); opportunityPost.parentId = oli.opportunityId; //String bodyText = 'This is the body '; opportunityPost.Type = 'LinkPost'; opportunityPost.Title = '' +entries.get(oli.Pricebookentryid).Product2.ProductCode+ ' socket details'; opportunityPost.Body = bodyText; String id = String.valueOf(oli.Id).substring(0,15); opportunityPost.LinkURL = 'https://na2.salesforce.com/' +id; posts.add(opportunityPost); } //if the max potential has increased by a million, let's add to the chatter feed if(oli.Max_Potential__c >= oldOLI.Max_Potential__c + 1000000) { decimal NewWholeMaxPotential = oli.Max_Potential__c; decimal maxpotentialdifference = oli.Max_Potential__c/1.0 - oldOLI.Max_Potential__c/1.0; FeedPost opportunityPost = new FeedPost (); String bodyText = 'has increased the Max Potential of the ' +entries.get(oli.Pricebookentryid).Product2.ProductCode+ ' by $' +maxpotentialdifference+ ' on Account: ' +opp.get(oli.OpportunityId).Account.Name+ ''; opportunityPost.Type = 'LinkPost'; opportunityPost.Title = ''+entries.get(oli.Pricebookentryid).Product2.ProductCode+' socket details'; opportunityPost.Body = bodyText; String id = String.valueOf(oli.id).substring(0,15); opportunityPost.LinkURL = 'https://na2.salesforce.com/'+id; opportunityPost.ParentID = oli.opportunityid; posts.add(opportunityPost); } } //if the post is empty, don't try and post it to Chatter if(!posts.isEmpty()) { insert posts; } }
As you can see, the code is clearly noted throughout the trigger explaining which each step is. This is vital incase other developers need to update or change the code later.
I hope this helps others in the community. Many thanks to Will for helping me write and understand the steps it took to get this trigger working. I did receive some guidance from Scott Hemmeter, CEO and founder of the Arrowpointe Corp. He has always been available to help other developers. Thanks Scott I appreciate it!
Enjoy!!
~Mike
Comments
Leave Comment








Email Blog Entry
