[SalesForce] Odd error when inserting Chatter post from Opportunity trigger

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());

    Test.startTest();

    //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);

    Test.stopTest();
}

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?

Best Answer

(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 
WHERE Name LIKE ='INKBABY%' 
ORDER BY Name ASC 
LIMIT 1];

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.

Related Topic