1

I'm trying to put together a FeedItem trigger that has slightly different behavior if the chatter post comes from a particular Connected App. My understanding is that the ConnectedApi includes a ClientInfo class with an applicationName property that will give me the Connect App. How do I access this from within a FeedItem trigger.

Thanks in advance.

trigger notifyChatterFollowers on FeedItem (after insert, after update) {

    for(FeedItem fi : Trigger.New)  
    {
        String creatorName = [SELECT Id, Name FROM User WHERE Id = :Userinfo.getUserId()].Name;
        ConnectApi.FeedElement feedItem = ConnectApi.ChatterFeeds.getFeedElement(Network.getNetworkId(), fi.Id);
        // ?????
        ...
    }

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

EDITED: Still running into an issue. Here is the entire trigger now:

trigger testFeedItemTrigger on FeedItem (after insert) {

    for(FeedItem fi : Trigger.New)  
    {
        System.debug('------> feedItemId: ' + fi.Id);
        System.debug('------> NetworkId: ' + Network.getNetworkId());

        ConnectApi.FeedElement feedElement = ConnectApi.ChatterFeeds.getFeedElement(Network.getNetworkId(), fi.Id);
        System.debug('------>  ... about to enter if statement ...');
        if (feedElement instanceof ConnectApi.FeedItem) {
            ConnectApi.FeedItem feedItem = (ConnectApi.FeedItem) feedElement;
            if (feedItem.clientInfo != null) {
                System.debug('------> The application name is ' + feedItem.clientInfo.applicationName);
            } else {
                System.debug('------> The application name is null...');
            }
        }
        System.debug('------> Done with Trigger.');        
    }
}

When I run the following test class...

@isTest(SeeAllData=true) 
public class fiTest {
    @isTest public static void feedItemTesting(){
        FeedItem post = new FeedItem();
        post.ParentId = 'a0261000001lyo5';
        post.Body = 'Enter post text here';

        test.StartTest();
        insert post;
        test.StopTest();            
    }
}

I get the expected results...

12:11:27.495 (591286893)|CODE_UNIT_STARTED|[EXTERNAL]|01q4B0000004UH0|testFeedItemTrigger on FeedItem trigger event AfterInsert for [0D54B000007804w]
12:11:27.495 (592279359)|USER_DEBUG|[5]|DEBUG|------> feedItemId: 0D54B000007804wSAA
12:11:27.495 (592447458)|USER_DEBUG|[6]|DEBUG|------> NetworkId: null
12:11:27.495 (676589305)|USER_DEBUG|[9]|DEBUG|------>  ... about to enter if statement ...
12:11:27.495 (676793061)|USER_DEBUG|[15]|DEBUG|------> The application name is null...
12:11:27.495 (676822223)|USER_DEBUG|[18]|DEBUG|------> Done with Trigger.
12:11:27.495 (678567798)|CODE_UNIT_FINISHED|testFeedItemTrigger on FeedItem trigger event AfterInsert for [0D54B000007804w]

However when I run from either my connected app or from the salesforce UI, I don't get the same result:

12:12:08.0 (509135)|CODE_UNIT_STARTED|[EXTERNAL]|01q4B0000004UH0|testFeedItemTrigger on FeedItem trigger event AfterInsert for [0D54B000007805B]
12:12:08.0 (1792004)|USER_DEBUG|[5]|DEBUG|------> feedItemId: 0D54B000007805BSAQ
12:12:08.0 (1910149)|USER_DEBUG|[6]|DEBUG|------> NetworkId: null

... and that's it. Any idea why this trigger is only working with the apex test code?

1
  • Just to add, it looks like the clientInfo property is a property of deprecated (as of API 32.0) ConnectApi.FeedItem class. But I don't see it available on the ConnectApi.FeedElement class. – Rob Goldiez May 22 '16 at 11:47
0

You're right, the clientInfo property is only available for feed items. The ConnectApi.FeedItem class itself is not deprecated. After you get the feed element, you need to determine whether or not it's a feed item, and if it is, then you can inspect the clientInfo. Here's some sample code:

ConnectApi.FeedElement feedElement = ConnectApi.ChatterFeeds.getFeedElement(Network.getNetworkId(), fi.Id);
if (feedElement instanceof ConnectApi.FeedItem) {
    ConnectApi.FeedItem feedItem = (ConnectApi.FeedItem) feedElement;
    if (feedItem.clientInfo != null) {
        System.debug('The application name is ' + feedItem.clientInfo.applicationName);
    }
}

Also, you should consider using ConnectApi.ChatterFeeds.getFeedElementBatch() to bulkify your trigger.

10
  • Your code makes sense but I'm still running into an issue. I edited my original post with what I'm seeing now. – Rob Goldiez May 24 '16 at 18:51
  • I'm not sure why it's halting for you in the middle of the trigger. Maybe your log got truncated somehow? I copied your trigger into my Developer Edition org and I got the expected results (the same debug lines that showed up when you ran your unit test) when I posted feed items using the UI. I tried posting to both a user's profile page and to an account record. – alouie May 25 '16 at 0:58
  • alouie: thanks for helping me out. I trimmed the output to only the system debug messages. Here's the full output: 37.0 APEX_CODE,DEBUG;APEX_PROFILING,NONE;CALLOUT,INFO;DB,NONE;SYSTEM,NONE;VALIDATION,NONE;VISUALFORCE,NONE;WORKFLOW,NONE 20:01:51.0 (381826)|USER_INFO|[EXTERNAL]|00561000000Kj0n|rgoldiez@[...]|Central Standard Time|GMT-05:00 20:01:51.0 (420225)|EXECUTION_STARTED 20:01:51.0 (427165)|CODE_UNIT_STARTED|[EXTERNAL]|01q4B0000004UH0|testFeedItemTrigger on FeedItem trigger event AfterInsert for [0D54B000007827W] [... more in next comment ...] – Rob Goldiez May 25 '16 at 1:03
  • [...continuing...] 20:01:51.0 (1666655)|USER_DEBUG|[5]|DEBUG|------> feedItemId: 0D54B000007827WSAQ 20:01:51.0 (1785014)|USER_DEBUG|[6]|DEBUG|------> NetworkId: null 20:01:51.80 (80490444)|USER_INFO|[EXTERNAL]|00561000000Kj0n|rgoldiez@[...]|Central Standard Time|GMT-05:00 20:01:51.80 (154469717)|USER_DEBUG|[54]|DEBUG|{0054B000000I1TYQA0, 00561000000sHqeAAE} 20:01:51.80 (235860128)|PUSH_NOTIFICATION_NO_DEVICES||Mobile_App 20:01:51.80 (237502118)|CODE_UNIT_FINISHED|notifyChatterFollowers on FeedItem trigger event AfterInsert for [0D54B000007827W] 20:01:51.80 (238543212)|EXECUTION_FINISHED – Rob Goldiez May 25 '16 at 1:04
  • alouie: I confirmed that the trigger works as expected in my dev org too... but it's not working right in my sandbox. – Rob Goldiez May 25 '16 at 1:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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