I have a trigger on my Opportunity object that executes after insert and simply posts a new FeedItem to a specific group within my instance when the Opportunity record Stage is set to "Signed". Here is the method I'm calling to instantiate each FeedItem.

private FeedItem createSignedRecordPost(Opportunity o, String groupId)
    //create new feed item to insert
    FeedItem post = new FeedItem();
    post.ParentId = groupId;
    post.CreatedById = o.OwnerId;
    post.Body = 'BOOM! #InkBaby Put $'+ Math.round(o.SalesFundingAmount__c) +' on the board!';
    post.LinkUrl = URL.getSalesforceBaseUrl().toExternalForm() + '/' + o.Id;
    post.Title = o.Name;

    return post;

After instantiating a list of FeedItems, I insert them at the end of my trigger logic.

Now, I have created the following unit test to test this logic:

static testMethod void recordSignedChatterPostTest()
    //create account
    Account a = new Account();
    a.Name = 'Test Account';
    insert a;

    //create contact
    Contact c = new Contact();
    c.FirstName = 'John';
    c.LastName = 'Doe';
    c.AccountId = a.Id;
    c.Phone = '1234567890';
    insert c;

    //create opportunity
    Opportunity o = new Opportunity();
    o.AccountId = a.Id;
    o.StageName = 'First Appointment';
    o.CloseDate = system.today();
    o.Name = 'Test Opp 1';
    insert o;

    //create primary contact record
    OpportunityContactRole cRole = new OpportunityContactRole();
    cRole.OpportunityId = o.Id;
    cRole.ContactId = c.Id;
    cRole.IsPrimary = true;
    cRole.Role = 'Decision Maker';
    insert cRole;

    //create chatter collaboration group
    CollaborationGroup[] chatterGroup = [SELECT Id FROM CollaborationGroup WHERE Name='INKBABY' LIMIT 1];
    system.assertEquals(1, chatterGroup.size());


    //update opportunity stagename to  'Signed'
    o.StageName = 'Signed';
    update o;

    Opportunity oppId = [SELECT Id, StageName FROM Opportunity WHERE Id = :o.Id LIMIT 1];

    //new chatter feed item should have been added to the record
    FeedItem[] items = [SELECT Id, Title, ParentId FROM FeedItem WHERE CreatedDate=TODAY ORDER BY CreatedDate DESC];
    system.assertEquals(1, items.size(), 'There should only be one chatter post at this time.');

    //check that Stage Name is set to signed
    system.assertEquals(oppId.StageName, 'Signed');

    //assert that the feed post was posted to the ink baby group
    //system.assertEquals(inkBabyGroupId, items[0].ParentId);

    system.debug('--DEBUG-- Name and Title (should be the same) '+o.Name+ ', ' + items[0].Title);

    //assert that the feed post title is the name of the opportunity
    //system.assertEquals(o.Name, item.Title);


When I run this unit test I receive the following error: System.DmlException: Update failed. First exception on row 0 with id 006e0000002Wc6GAAS; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [CloseDate]: [CloseDate]

As you can see in my unit test, I'm clearly setting the CloseDate of the Opportunity record. I'm really at a loss for words on this error. Any ideas on how to troubleshoot?

  • 2
    Examine the unit test's debug log? I suspect you have a workflow or other "before / after update" trigger that might be clearing this field. Is this the only test that fails? Maybe something new was introduced recently that will make different Opp-related tests to fail...
    – eyescream
    Feb 27, 2013 at 19:14
  • Yeah I've looked into that very closely and the only thing that should effect the CloseDate is a WF I have to update it based on the current Stage of the Opportunity. I've double and triple checked to make sure the WF is working properly. I only receive the error when I attempt to create the Chatter post.
    – jonnybro
    Feb 27, 2013 at 19:21
  • @eyescream agree .There is something nullifying the close date field . Feb 27, 2013 at 19:28
  • 3
    Can you deactivate the workflow to rule out that is causing the issue? Feb 27, 2013 at 19:33
  • 1
    It's only in the unit test or maybe also happens from GUI too? Any chance the way you calculate new date in this workflow might be getting a null somewhere? (null + 7 = null). Maybe workflow or your trigger fires twice (some side-effect that updates opp again)? Search in the log how many times name of trigger pops up.
    – eyescream
    Feb 27, 2013 at 19:51

1 Answer 1


(answer to latest issue from the comments thread ;))

Interesting. So it can't find it but uniqueness check actually finds it? Hmm... Either use "@seeAllData=true" (nasty) or use different name for your group, maybe 'INKBABY Unit Test'.

Then in the test select correct group based either on Test.isRunningTest() or something like that:

CollaborationGroup[] chatterGroup = [SELECT Id 
FROM CollaborationGroup 

Any kind of suffix to the group name will be skipped ;)

(answer that kind of emerged from comments)

Make sure that there are no workflows / triggers that clear the value of Close Date. If there's such workflow - make sure the formula it uses to determine new close date gets all arguments it needs. null + 7 = null so probably you have to specify more fields while inserting your Opportunity.

  • Went the route of changing the query to use the LIKE expression. Worked like a charm!
    – jonnybro
    Feb 27, 2013 at 21:53
  • Follow up question: Any idea why it would work from the standard UI and not from a unit test, when essentially I was doing the exact same DML operation on the record...?
    – jonnybro
    Feb 28, 2013 at 1:05
  • Could you post your "before and after" calculation that determines new CloseDate? Are you 100% positive all fields it relies on were filled in during Opp creation in the test (meaning that old formula didn't rely on say Amount field)?
    – eyescream
    Feb 28, 2013 at 7:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .