2

I created a field on the Account object called In_Negotiation__c, it's a picklist with options 'True' and 'False' with a default value of 'False'. (Yes, I know it would make more sense to have it be a boolean/checkbox, but there are valid reasons to make it a picklist that I can't get into here)

When I create a new Account in the normal point-and-click interface, the field successfully defaults to 'False'. I've also created a before insert/before update trigger to set the value of this field (looks at the Accounts opportunities and determines whether there exists an opportunity that fits our definition of "in negotiation"). This should mean the field always has a value of either 'True' or 'False'.

However, in my unit test, it seems that after the initial creation of a test Account, the field value comes back NULL.

Here is my Unit Test code:

@istest static void test_setInNegotiation() {

    Account a = new Account();

    a.Name = 'account';

    insert a;

    system.assertEquals('False', a.In_Negotiation__c, 'In_Negotiation__c is not defaulted to False!');

    Opportunity o = new Opportunity();

    o.Name = 'opp';
    o.StageName = 'Active Discussion';
    o.Type = 'New Business';
    o.CloseDate = date.today().addDays(15);
    o.Amount = 990;
    o.AccountId = a.id;

    insert o;

    system.assertEquals('False', a.In_Negotiation__c, 'In_Negotiation__c is not False when it should be!');

    o.StageName = 'Negotiation';

    update o;

    system.assertEquals('True', a.In_Negotiation__c, 'In_Negotiation__c is not True when it should be!');

    o.Type = 'Upgrade';

    update o;

    system.assertEquals('True', a.In_Negotiation__c, 'In_Negotiation__c is not True when it should be!');

    o.Type = 'Annual Renewal';

    update o;

    system.assertEquals('True', a.In_Negotiation__c, 'In_Negotiation__c is not True when it should be!');

    o.CloseDate = date.today().addDays(-400);

    update o;

    system.assertEquals('False', a.In_Negotiation__c, 'In_Negotiation__c is not False when it should be!');

    o.CloseDate = date.today().addDays(15);
    o.Type = 'Monthly Renewal';

    update o;

    system.assertEquals('False', a.In_Negotiation__c, 'In_Negotiation__c is not False when it should be!');

}

When I run it, I'm getting the error System.AssertException: Assertion Failed: In_Negotiation__c is not defaulted to False!: Expected: False, Actual: null

Why would this be happening? Why is the value of the field not 'False'?

2 Answers 2

0

All formula fields and default values are calculated on the database level. You need to Select your record from database to get these values. Something like this: a = [SELECT In_Negotiation__c FROM Account WHERE Id =: a.Id];

0
4

Yes, it is possible to populate default values in Apex, but will require an extra step.

Recycled from a previous post response (from @RichUnger):

You'll need to construct the Account from it's sObject type and your default values will be populated:

Account a = Account.sObjectType.newSObject(
  recordTypeId, // can be null
  true); // loadDefaultValues

Does 'default value' do anything if the object is created through Apex?

You must log in to answer this question.

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