+ Start a Discussion
msimondsmsimonds 

Update Chatter on Opportunity when Line Item Is changed

Along with a co-worker, we are trying to write a trigger to update the chatter on our Opportunities with the ProductCode from the line item. We are not having much success. We do have a working trigger

 

 

trigger LineItemToOpportunityFeedUpdate on OpportunityLineItem (after update) {
    List<FeedPost> posts = new List<FeedPost>();
    
    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];
            
                                                                     
            String bodyText = 'Socket outcome was changed from '+oldOLI.Part_Outcome__c+' to '+newOLI.Part_Outcome__c+'.';     
            
            //I would put in the '+lineitem.UnitPrice+' command before the .'; in line 19.
            
            if(opportunityFeedPosts.size() == 0 || opportunityFeedPosts[0].FeedPost.Body != bodyText) {
                //System.debug('OpportunityFeed Posts: '+opportunityFeedPosts[0]);
                
                FeedPost opportunityPost = new FeedPost ();
                opportunityPost.Type = 'LinkPost';
                opportunityPost.Title = 'Socket XYZ has been set to '+newOLI.Part_Outcome__c+'';
                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);
               }
            }
        }
        insert posts;
}

 

 

If you notice, we have a custom field called the part_outcome__c , which is updating, but we are trying to get the Product code and when we attempt to get the Product Code or part number on the chatter area;

 

Currently chatter using the part_outcome__c looks like this:

 

 

Can someone please help me correct this trigger so we can get the productcode on the chatter. Just for Clarification, our socket = ProductCode

 

 

rwoollenrwoollen

For LinkPosts, the Web UI displays the Title and LinkURL.  I believe Body is unused.  (Body is used for TextPosts.)

finalistfinalist

A LinkPost can use a Body and a Title - the Body appears at the top of the post; the link/Title appears at the bottom.

 

FeedPost fpost = new FeedPost();
fpost.Type = 'LinkPost';
fpost.ParentId = <SObjectId>;

 

fpost.linkURL = <url>;
fpost.title = 'This text will serve as the link text';


fpost.Body = 'This text will serve as the post body';


insert fpost;

 

I still haven't figured out a reliable way to insert a relative URL as the linkURL, but that's a story for a different post.