2

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!

2
  • 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
    Commented Jun 3, 2013 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 Commented Jun 4, 2013 at 3:09

2 Answers 2

3

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);
1
2

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

6
  • 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! Commented Feb 14, 2013 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. Commented Feb 14, 2013 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! Commented Feb 14, 2013 at 19:49
  • I would imagine replace 'me' with the record Id that you want to post to. Commented Feb 14, 2013 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! Commented Feb 15, 2013 at 18:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .