Thursday, January 24, 2013

Adding an Activity to a Chatter Feed Using a Trigger

I have had many requests for a trigger like this over time and Kevin Swiggum had a post back in 2010 with a simple to implement solution for this request, the link is below.

http://www.radialweb.com/2010/09/adding-activity-to-a-chatter-feed-using-triggers/


Below is a copy of the code Kevin posted:

trigger ChatterActivity on Task (after insert, after update) {

    List<FeedItem> feedItems = new List<FeedItem>();

    //We want to show the User name as assignedTo. The only way to get to that is by querying the user table.
    Set<ID> ownerIds = new Set<ID>();
    for (Task t : Trigger.new) {
        ownerIds.add(t.ownerId);
    }
    Map<ID,User> userMap = new Map<ID,User>([SELECT ID, Name FROM User WHERE ID IN :ownerIds]); //This is our user map

    //Now loop though the new/updated tasks and create the feed posts
    for (Task t : Trigger.new) {
        if (t.WhatId != null) {
            FeedItem fitem = new FeedItem();
            fitem.type = 'LinkPost';
            fitem.ParentId = t.WhatId;
            fitem.LinkUrl = '/' + t.id; //This is the url to take the user to the activity
            fitem.Title = 'View';  //This is the title that displays for the LinkUrl

            //Get the user by checking the userMap we created earlier
            User assignedTo = userMap.get(t.ownerId);

            fitem.Body = ((Trigger.isInsert) ? 'New' : 'Updated') + ' Activity ' + ((t.ActivityDate != null) ? t.ActivityDate.format() :'')
                        + '\nAssigned To: ' + ((assignedTo != null) ? assignedTo.name : 'Unknown')
                        + '\nSubject: ' + t.Subject
                        + '\nStatus: ' + t.Status;

            feedItems.add(fitem);
        }
    }

    //Save the FeedItems all at once.
    if (feedItems.size() > 0) {
        Database.insert(feedItems,false); //notice the false value. This will allow some to fail if Chatter isn't available on that object
    }
}


And here is the test class associated

public with sharing class ChatterUnitTests {

    /*
    *    Test the ChatterActivity trigger which inserts chatter feed posts whenever a task is inserted on the parent object
    */
    public static testMethod void testChatterActivity() {
        //create the test account
        Account a = new Account(name='Test Account');
        insert a;

        //create a task on that account
        Task t = new Task(whatId=a.id);
        t.Subject = 'This is a test activity for chatter';
        t.ActivityDate = System.today();
        t.Status = 'In Progress';
        t.Description = 'Hello, this will be chattered';

        insert t;

        //Make sure Account has the feed enabled. If it does, make sure the chatter feed post is there
        Schema.DescribeSObjectResult r = Account.SObjectType.getDescribe();
        if (r.isFeedEnabled()) {
            List<AccountFeed> posts = [SELECT Id, Type FROM AccountFeed WHERE ParentId = :a.id];
            System.assertEquals(1, posts.size());
        }
    }
}







Wednesday, January 16, 2013

Bulkify Apex Triggers

This is something that has been harped on in every Apex class I have taken and it finally clicked with me! This is probably much more clearly explained in the link below from developer.force.com but I want to document it here for my own knowledge.

http://wiki.developerforce.com/page/Best_Practice:_Bulkify_Your_Code

  • Create a SET before entering trigger.new that will hold the ID of all SObjects being updated or looped through
  • Pull a list of the IDs of the SObjects that need to be updated when looping through trigger.new and add them to the SET defined above
  • If that SET contains items then create a list of SObjects and use 1 SOQL query to populate the entire list
  • Now you can loop through the entire list of SObjects and make updates from there...

Below is an example of this in action:

if(trigger.isBefore){
//Set to hold the list of Contract Id's that will be queried for the associated Account 
Set<String> ContractIds = new Set<String>();
//loop through all Agency Commission Records and add the Id of the Contract to the ContractIds Set
for(Agency_Commission__c ac : trigger.new){
if(ac.Contract__c != null){
ContractIds.add(ac.Contract__c);
}
}
if(!ContractIds.isEmpty()){
//Use a single query to pull a list of Contract SObjects
List<Asset_C__c> contractList = [select id, Account__c from Asset_C__c where id =: ContractIds];

//Map to hold the Contract ID as the Primary Key and the Account ID as the Key Value
Map<string, string> ContractAndAccount = new Map<string, string>();
//Loop through the List of Contracts and add the Contract ID and Account ID to the map
for(Asset_C__c c1 : contractList){
if(c1.Account__c != null){
ContractAndAccount.put(c1.id, c1.Account__c);
}
}
if(!ContractAndAccount.isEmpty()){
//Loop through the original Agency commission values and assign the Account based on the ContractAndAccount Map
for(Agency_Commission__c acUpdate : trigger.new){
//retrieve the Account from the ContractAndAccount Map and assign it to a string value
String acctForContract = ContractAndAccount.get(acUpdate.Contract__c);
acUpdate.Client__c = acctForContract;
}
}
}
}