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
Daniel MarcusDaniel Marcus 

NULL lookup field in Test Class

Hi All,

I'm getting a null value on a lookup field when running a test class.
 
Account a = new Account(Name = '1', Owner = u, CurrencyIsoCode = 'CAD');
        insert a;
		a = [SELECT id, AccountNumber, Name, CurrencyIsoCode, Related_Billing_Contract__c FROM account WHERE id = :a.id];

		Billing_Contract__c bc = new Billing_Contract__c(Account__c = a.id, Active__c = TRUE, Annual_Increase_Amount__c = 0.03, Billing_Frequency__c = 'Annually');
		insert bc;
		bc = [SELECT id FROM Billing_Contract__c WHERE id = :bc.id];

		a.Related_Billing_Contract__c = bc.id;
		update a;

		a = [SELECT id, AccountNumber, Name, CurrencyIsoCode, Related_Billing_Contract__c FROM account WHERE id = :a.id];

		System.debug('Related_Billing_Contract__c: ' + a.Related_Billing_Contract__c);
        
        Opportunity o = new Opportunity(Name = '2', GSS_Rep__c = g.id, Account = a, StageName = 'No Status', CloseDate = System.today(), Amount = 1, Type = 'New Implementation', License_Model__c = 'Perpetual', Owner = u, Sales_Rep2__c = s.id, Pricebook2Id = p.id, CurrencyIsoCode = 'CAD', Intall_Date_Opp__c = system.today(), Milestone_Trigger__c = 'Installation', Payment_Terms_Drop__c = '30 Days on Order');
        insert o;

        o = [SELECT id, AccountId, Account.AccountNumber, Account.Related_Billing_Contract__c, Account.Name, Name, Service_Value_No_Milesotne__c, CurrencyIsoCode, Software_Value_No_Milestone__c, Account.Id, Account.Licensing_Model__c, type, Service_Days__c FROM opportunity WHERE id = :o.id];

        System.debug('o.account.Related_Billing_Contract__c: ' + o.Account.Related_Billing_Contract__c);

My initial Debug returns an ID for the Account.Related_Billing_Contract__c, but when I go through the Opportunity to find the same value (Opportunity.Account.Related_Billing_Contract__c) I am getting NULL returned. Any help?
Best Answer chosen by Daniel Marcus
edoardo_spanoedoardo_spano
Hi Daniel,

try to check:
- the field-level security on account field for opportunity object
- eventually logics that could modify the Related_Billing_Contract__c value (e.g. workflows or triggers)
- if the value of AccountId for "o" is what do you expect (system.assertEquals(a.Id,o.AccountId))

 

All Answers

edoardo_spanoedoardo_spano
Hi Daniel,

try to check:
- the field-level security on account field for opportunity object
- eventually logics that could modify the Related_Billing_Contract__c value (e.g. workflows or triggers)
- if the value of AccountId for "o" is what do you expect (system.assertEquals(a.Id,o.AccountId))

 
This was selected as the best answer
Darshan Shah2Darshan Shah2
Hi Daniel Marcus

Why you are wrting SOQL to find out Id?

Please paste below code in your test class and let me know whether it solves your problem.

Account a = new Account(Name = '1', Owner = u, CurrencyIsoCode = 'CAD');
insert a;

Billing_Contract__c bc = new Billing_Contract__c(Account__c = a.id, Active__c = TRUE, Annual_Increase_Amount__c = 0.03, Billing_Frequency__c = 'Annually');
insert bc;

a.Related_Billing_Contract__c = bc.id;
update a;

Opportunity o = new Opportunity(Name = '2', GSS_Rep__c = g.id, Account = a, StageName = 'No Status', CloseDate = System.today(), Amount = 1, Type = 'New Implementation', License_Model__c = 'Perpetual', Owner = u, Sales_Rep2__c = s.id, Pricebook2Id = p.id, CurrencyIsoCode = 'CAD', Intall_Date_Opp__c = system.today(), Milestone_Trigger__c = 'Installation', Payment_Terms_Drop__c = '30 Days on Order');
insert o;

System.debug('o.account.Related_Billing_Contract__c: ' + o.Account.Related_Billing_Contract__c);


If it doesnt work then let me know, so I can test it in my org.

Warm Regards,
Darshan Shah