The opportunity line item in Salesforce.com has always been a difficult child to love. It has nuances that aren't quite clearly explained anywhere and often interacting with it is grueling and painful.
We wanted to write a trigger that would trigger an update to our opportunity if certain criteria on the line item were met.
The beginning of the trigger is pretty straightforward
NOTE: This is a code snippet and is not complete.Code:trigger LineItemToOpportunityFeedUpdate on OpportunityLineItem (after update) { List<FeedPost> posts = new List<FeedPost>(); Set<Id> pbeIds = new Set<Id>(); . . . . pbeIds.add(newOLI.PricebookEntryId); Map<Id, PricebookEntry> entries = new Map<Id, PricebookEntry>([select Product2.ProductCode from PricebookEntry where id in :pbeIds]); String bodyText = ''+entries.get(newOLI.Pricebookentryid).Product2.ProductCode+' has been updated from '+oldOLI.Part_Outcome__c+' to '+newOLI.Part_Outcome__c+'.'; . . .
The third line of code is the "secret sauce" that took some help to figure out. Essentially, the "Set" command is an unordered collection of primitives that don't contain duplicate values.
If you have a text editor with line numbers, this becomes a lot easier to follow. You can download one called Crimson Editor if you don't.
In the above pasted code, Line 8 says to take the Ids you have from line 3 for the opportunity line item and find all of the price book entry ids associated with that line item; add them to the set array called pbeIDs.
In line 10, you invoke the "Map" command which is a collection of key value pairs where each unique key maps to a single value. These keys can be any data type while the values can be primitive, an sObject, object or collection type.
It says to map the ID you got from line 8 to against the pricebookentry table and have that equal to a new map that returns the Product2.ProductCode field. Now you have the "productcode" field which then you can use in line 13 to update the chatterr feed with the product code.
It's not hard once you see it but seeing it was the key. I hope that helps!
Dude great work getting this trigger working. I look forward to seeing mor of your work and working with you in the future.
Are you going to post the whole trigger?
Sure man. Here it is:
Code:trigger LineItemToOpportunityFeedUpdate on OpportunityLineItem (after update) { List<FeedPost> posts = new List<FeedPost>(); Set<Id> pbeIds = new Set<Id>(); for(OpportunityLineItem newOLI : Trigger.new) { OpportunityLineItem oldOLI = Trigger.oldMap.get (newOLI.id); //System.debug('New: '+newOLI.Part_Outcome__c+' Old: '+oldOLI.Part_Outcome__c); if(newOLI.Part_Outcome__c != oldOLI.Part_Outcome__c) { List<OpportunityFeed> opportunityFeedPosts = [SELECT Id, Type, FeedPost.Body From OpportunityFeed Where ParentID = :newOLI.OpportunityID ORDER BY CreatedDate DESC]; pbeIds.add(newOLI.PricebookEntryId); Map<Id, PricebookEntry> entries = new Map<Id, PricebookEntry>([select Product2.ProductCode from PricebookEntry where id in :pbeIds]); //String bodyText = 'Got here'; String bodyText = ''+entries.get(newOLI.Pricebookentryid).Product2.ProductCode+' has been updated from '+oldOLI.Part_Outcome__c+' to '+newOLI.Part_Outcome__c+'.'; if(opportunityFeedPosts.size() == 0 || opportunityFeedPosts[0].FeedPost.Body != bodyText) { //System.debug('OpportunityFeed Posts: '+opportunityFeedPosts[0]); FeedPost opportunityPost = new FeedPost (); opportunityPost.Type = 'LinkPost'; opportunityPost.Title = 'Click here for socket details'; opportunityPost.Body = bodyText; String id = String.valueOf(newOLI.id).substring(0,15); opportunityPost.LinkURL = 'https://cs2.salesforce.com/'+id; opportunityPost.ParentID = newOLI.opportunityid; posts.add(opportunityPost); } } } }
Bookmarks