function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sindhu@demo.comsindhu@demo.com 

How to write a trigger on a Chatter post on the Account object

Hi Everyone!! I need a trigger on a Chatter post on the Account object that does the following: • If the hashtag “#request” is used in the post, the trigger creates a record in the Request__c custom object. • The Account ID is populated in Account__c • The Type__c field is “Other” It would be helpful if someone help me out in writing trigger on chatter post on "Account" object. Regards, Sindhu
SamuelDeRyckeSamuelDeRycke

Which steps have you taken yourself so far ? If you started writing code, it will help if you share that with us.

 

If you're not sure where to start, having a look at the chatter data model is a good start point: http://developerforce.s3.amazonaws.com/website/pdfs/Chatter-cheatsheet_final.pdf

 

Without putting a lot of thought into this, I expect you'll want to make a trigger on feeditem, and filter out when the parentobject is of the Account type. Give it a try, and let us know where you run into difficulties.

sindhu@demo.comsindhu@demo.com
Hi Srdy, I have written the below trigger and its working perfectly fine but I want this to be associated to the account. (i.e) the "Account" field in "Request__c" should be populated with the account Name on which we post the chatter.(r.Account__c=Name of the account to which it is associated) I think I am missing something in between. Trigger RequestUpdate on FeedItem (after insert) { String accKeyPrefix = Account.sObjectType.getDescribe().getKeyPrefix(); String AccId; for (FeedItem f: trigger.new) { String parentId = f.parentId; if (parentId.startsWith(accKeyPrefix) && f.Body == '#request') { Request__c r = new Request__c(); r.Name = f.body; r.Account__c = '********'; r.Description__c=f.body; insert r; } } }
SamuelDeRyckeSamuelDeRycke

 

 

Trigger RequestUpdate on FeedItem(after insert) {
    String accKeyPrefix = Account.sObjectType.getDescribe().getKeyPrefix();
    String AccId;
    for (FeedItem f: trigger.new) {
        String parentId = f.parentId;
        if (parentId.startsWith(accKeyPrefix) && f.Body == '#request') {
            Request__c r = new Request__c();
            r.Name = f.body;
            r.Account__c = '********';
            r.Description__c = f.body;
            insert r;
        }
    }
}

 That is a very good start !

 

You will most likely want to chek if f.Body contains '#requeste' instead of equals.

 

There are multiple ways to approach this, what I would do is probably:

  1. iterate your feedItem objects like you are doign, and map them into Map<Id,list<feeditem>>, where id is the account id, and list is a list of feeditems per account Id (got to keep thinking in terms of bulk processing when writing triggers)
  2. collect all accounts id & name fields from accounts with 'WHERE Id IN' the keyset of the above map., you can store them in a set<id,name>
  3. iterate your map  values() and for every list and then iterate the list to make a Request__c object for every feeditem. You can get the account name by useing the set from step 2.  set.get(f.parentid) should return the name of the account. Add all request__c objects to a list ! (avoid governor limits on dml statements)
  4. insert the list of request__c objects.

If you could try this, you should probably get it working. 

 

ps: use the code block functionality to post code here, realy helps in terms of readability.