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


I cannot figure out for the life of me why I am getting the CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Glen_Task_Chatter: execution AfterInsert error


test code:

public with sharing class test_Glen_Task_Chatter {

    *    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
        User u1 = [select id from User where alias='gbrad'];
//Run As U1  
        Account a = new Account(name='Test Account', Type='Law Firm', Industry='Agriculture', BillingCity='Indianapolis', BillingCountry='USA');
        insert a;
        Contact c = new Contact(LastName='Test Account',, Title='test', Email='', LeadSource='Adwords');
        insert c;

        //create a task on that account
        Task t = new Task(Subject='Subject', Contact_Outreach__c='Log A Phone Meeting',WhoId=c.Id,,;
        insert t;
        //Event e = new Event (Subject='Subject', Related_Services__c='Consulting', DurationInMinutes=30,;
        //insert e;


Actual Trigger:

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

    if(UserInfo.getName()!='Eloqua Marketing'){
        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 : {
        Map<ID,User> userMap = new Map<ID,User>([SELECT ID, Name FROM User WHERE ID IN :ownerIds]); //This is our user map
        for (Task c: {
                    FeedItem fitem = new FeedItem();
                    fitem.type = 'LinkPost';
            		if (c.Accountid != null)
                    	fitem.ParentId = c.Accountid;
                    fitem.LinkUrl = '/' +; //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(c.ownerId);
                    fitem.Body = ((Trigger.isInsert) ? 'New' : 'Updated') + ' Task - ' + c.Subject +': ' + c.Contact_Outreach__c + '\n  ' + c.Description.substring(0,50); //+ c.Who.Name;
                    if (c.AccountId !=null)
        //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




In the test class, when you are inserting the task record, Please also put the value in the 'Description' field for the Task record. In the Trigger, you have not used any null check. Please put the null checks before the following line


fitem.Body = ((Trigger.isInsert) ? 'New' : 'Updated') + ' Task - ' + c.Subject +': ' + c.Contact_Outreach__c + '\n  ' + c.Description.substring(0,50); //+ c.Who.Name;


When running the test case, Description field is null and it is giving the null Pointer Exception.