Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

My goal is to create a trigger/class that, when a Financial Account (custom object) has it's balance change by more than 20% in either direction, creates a chatter post informing users of the 'Big Change' and @mentioning the Financial Advisor, whose ID is linked to the Financial Account via a lookup field. My question/error message is at the bottom. Thanks in advance!

I used these two articles as sources:

Getting Started with Connect in Apex

Connect in APEX Pilot

Here's the trigger:

trigger financialAccountAfterUpdate on Financial_Account__c (after update) 

    public postChatterOnBigBalanceChange postChatter = new postChatterOnBigBalanceChange();

    List<Financial_Account__c> newAccts = Trigger.new;
    List<Financial_Account__c> oldAccts = Trigger.old;

    if(trigger.isAfter && trigger.isUpdate){
        postChatter.upOrDown(newAccts, oldAccts);
    }
}

Here's the class:

public class postChatterOnBigBalanceChange{

    public void upOrDown(List<Financial_Account__c> newAccts, List<Financial_Account__c> oldAccts){

    //String to hold the body of the Chatter Post//
    String postBody;

    //Populate mapOldAccts w/ Trigger.old info so it can be compared against later//
    Map<Id, Financial_Account__c> mapOldAccts = new Map<Id, Financial_Account__c>();
    for(Financial_Account__c oldAcct : oldAccts){
        mapOldAccts.put(oldAcct.Id, oldAcct);
    }

    //Loop through Trigger.new and check to see if there is a 20% or greater change, and if so, is it up or down//
    for(Financial_Account__c newAcct :newAccts){
        if(newAcct.Account_Balance__c > mapOldAccts.get(newAcct.id).Account_Balance__c * 1.2){
            postBody = newAcct.name + 'has had its account balance INCREASE by more than 20%, from ' + mapOldAccts.get(newAcct.id).Account_Balance__c + ' to ' + newAcct.Account_Balance__c + '.';
        } else if (newAcct.Account_Balance__c < mapOldAccts.get(newAcct.id).Account_Balance__c * 0.8){
            postBody = newAcct.name + 'has had its account balance DECREASE by more than 20%, from ' + mapOldAccts.get(newAcct.id).Account_Balance__c + ' to ' + newAcct.Account_Balance__c + '.';
        }
        if(postBody != null){
            //Get the newAcct's Financial Advisor//
            Id finAdv = newAcct.Financial_Advisor__c;

            ////////////////////////////////////////////
            ////////////Generate FeedItemPost///////////
            ////////////////////////////////////////////

            //Create FeedItemInput to hold message body and @mentions//
            ConnectApi.FeedItemInput feedItemInput = new ConnectApi.FeedItemInput();

            //Create MessageBodyInput (child of FeedItemInput) to hold message segments
            feedItemInput.body = new ConnectApi.MessageBodyInput();

            //Create Message Segments Holder//
            List<ConnectApi.MessageSegmentInput> segments = feedItemInput.body.messageSegments;

            //Create text segment to hold message body//
            ConnectApi.TextSegmentInput textSegment = new ConnectApi.TextSegmentInput();
            textSegment.text = postBody;
            //Add body to message segment//
            segments.add(textSegment);

            //Create mention segment to hold an @mention//
            ConnectApi.MentionSegmentInput mention = new ConnectApi.MentionSegmentInput();
            mention.id = finAdv;
            //Add mention to message segment//
            segments.add(mention);

            //Assign created segments to the messageBodyInput, then assign that to the be the feedItemInput's body//
            feedItemInput.body.messageSegments = segments;

            //FeedType//
            feedTypeEnum feedType = feedTypeEnum.RECORD;

            //Create FeedItemPost//
            ConnectApi.ChatterFeeds.postFeedItem(null, feedType, 'me', feedItemInput, null);
        }
    }

}
}

Error message I'm getting now: Method does not exist or incorrect signature: ConnectApi.ChatterFeeds.postFeedItem(

I'm pretty sure this is related to the FeedType, and I'm not sure how to correctly use an enum to input the FeedType (or how I'm supposed to know which FeedType to use).

Any help would be much appreciated! Thanks!

share|improve this question
    
Are you able to get this working in bulkified manner or did u hit any limits with ConnectAPI? salesforce.com/us/developer/docs/apexcode/Content/… –  PK85 Jun 3 '13 at 20:56
    
Hey @PK85, unfortunately I actually no longer work in the industry, so I never finished what I had been working on when the above question came about, so I can't really answer your question. Thanks for the help though –  jackerman09 Jun 4 '13 at 3:09
add comment

2 Answers 2

up vote 3 down vote accepted

There is a version of postFeedItem() which takes a String as the last arg, and a version that takes ConnectApi.FeedItemInput and ConnectApi.BinaryInput as the last args.

The String one is a shortcut for posting a ConnectApi.FeedItemInput that holds only a single text segment.

If you want to post a @mention you have to use the ConnectApi.FeedItemInput version.

If you want to post on a record feed then use ConnectApi.FeedType.Record for the feedType argument and the id of the record for the subjectId argument, like so:

ConnectApi.ChatterFeeds.postFeedItem(null, ConnectApi.FeedType.Record, <some id>, <ConnectApi.FeedItemInput);
share|improve this answer
    
the complete doc for Connect in Apex is here: salesforce.com/us/developer/docs/chatterapi/index.htm –  Chris S Feb 15 '13 at 0:07
add comment

You can use the FeedBodyParser class as-is as a utility class and use it to parse your message and make the post.

For eg.

String txt = 'Hello @[Ritesh Aswaney](user:00580000002QHQx))';
ConnectApi.FeedItemInput feedItemInput = FeedBodyParser.convertToFeedItemInput(txt);      
ConnectApi.ChatterFeeds.postFeedItem(null, ConnectApi.FeedType.News, 'me', feedItemInput, null);

This uses the Parser class (Regex) to break the post into segments, in particular breaks down the mentions and formats them appropriately. I believe you need ConnectApi.FeedType.News

share|improve this answer
    
Thanks! I think you also inadvertently answered my question about using the enum by showing an example. How would I incorporate the utility class into my code? Thanks again! –  jackerman09 Feb 14 '13 at 2:22
    
Just create a new class and copy-paste the contents of the FeedBodyParser class as-is. You can then reference it for making posts. –  techtrekker Feb 14 '13 at 8:16
    
Thanks. I am also interested in learning how to use all of these new objects (in general), and I almost have it working now, except that it is posting on the feed of the context user, and instead, I want it to post on the feed of the record that was updated to trigger the post. In my Class above, this would be newAcct.id. Is this possible? Thanks again! –  jackerman09 Feb 14 '13 at 19:49
    
I would imagine replace 'me' with the record Id that you want to post to. –  techtrekker Feb 14 '13 at 21:03
1  
it looks like you can replace 'me' with the record Id, but you also need to change the FeedType to ConnectApi.FeedType.Record, which is what through me off for a while. Thanks again! –  jackerman09 Feb 15 '13 at 18:42
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.