[SalesForce] Creating a FeedItem Using Connect in APEX

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!

Best Answer

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);
Related Topic