+ Reply to Thread
Results 1 to 3 of 3
This is a discussion on Chatter, APEX triggers and the OpportunityLineItem within the Salesforce APEX forums, part of the Salesforce category; The opportunity line item in Salesforce.com has always been a difficult child
  1. #1
    Willis's Avatar
    Willis is offline Administrator
    Join Date
    Feb 2010
    Location
    Sunnyvale, CA
    Posts
    19
    Blog Entries
    9

    Chatter, APEX triggers and the OpportunityLineItem

    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

    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+'.';          
    .
    .
    .
    NOTE: This is a code snippet and is not complete.

    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!

  2. #2
    mike's Avatar
    mike is offline Administrator
    Join Date
    May 2007
    Location
    Wylie, Texas
    Posts
    607
    Blog Entries
    16
    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?

  3. #3
    Willis's Avatar
    Willis is offline Administrator
    Join Date
    Feb 2010
    Location
    Sunnyvale, CA
    Posts
    19
    Blog Entries
    9

    Thumbs up

    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);
                   }
                }
          }
    }

+ Reply to Thread

Similar Threads

  1. Chatter Enabled On My Salesforce developer Instance
    By mike in forum Salesforce Chatter
    Replies: 1
    Last Post: 04-05-2010, 12:03 PM
  2. triggers
    By cmctam in forum Salesforce Coding Discussions
    Replies: 4
    Last Post: 06-27-2008, 07:59 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO 3.5.2