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
frofrik2frofrik2 

Custom field on related account only returns null

Hi all,

 

I have created a test method for one of my triggers.
First it creates an account with a related opportunity. 
But when I'm trying to access one of the custom fields on the account through the opportunity, it only returns null.

 

This is the code I'm having trouble with:

 

 

        Account a = new Account(Name='testkonto',Status__c='Live',Store_ID__c='999999',
        CurrencyIsoCode='SEK',Country__c='Sweden',Industry='Art and Design');
        insert a;
        
        Opportunity o = new Opportunity(Name='Opp1',Account=a,StageName='Contract Sent',
        CloseDate=Date.newInstance(2011, 07, 15), Pricing_is_approved__c=1, Country2__c='Sweden',
        CurrencyIsoCode='SEK');
        insert o;
        
        a.Approval_Status__c = 'Sent for approval';  
        
        Test.startTest();
    	update a;
    	Test.stopTest();

        
        system.debug('account id: '+a.Id);
        system.debug('account approval status: '+a.Approval_Status__c);
        system.debug('account country: '+a.Country__c);
        system.debug('opp account id: '+o.Account.Id);
        system.debug('opp account name: '+o.Account.Name);
        system.debug('opp account approval status: '+o.Account.Approval_Status__c);
        system.debug('opp account country: '+o.Account.Country__c);

 

 

The field Approval_Status__c is returning null when accessing it through o.Account, but 'Sent for approval' when accessed directly.

 

Any ideas?

Best Answer chosen by Admin (Salesforce Developers) 
Ritesh AswaneyRitesh Aswaney

That is a little strange. Could you try setting the AccountId rather than the Account reference, though it should have the same effect presumably. Also, what do you mean by inaccessible?

 

Opportunity o = new Opportunity(Name='Opp1',AccountId=a.Id,StageName='Contract Sent',
        CloseDate=Date.newInstance(2011, 07, 15), Pricing_is_approved__c=1, Country2__c='Sweden',
        CurrencyIsoCode='SEK');
        insert o;

All Answers

Ritesh AswaneyRitesh Aswaney

Hey,

You ought to query for the Account and Opportunity Objects, or anywhere you're depending on Salesforce to join the dots for you after the insert. Only the Id is returned and set on the object after the insert call.

 

So, before you assert, just re-initialize the Opportunity via a SOQL Query

 

o = [Select Id, Account.Name, Account.Approval_Status__c, Account.Country__c from Opportunity where Id = :o.Id];

 

Then enforce your assertions, and you should be fine.

frofrik2frofrik2

It still returns null if I run a query.

The strange thing is that the value of o.Account.Country__c is accessable, but not o.Account.Approval_Status__c.

 

Ritesh AswaneyRitesh Aswaney

That is a little strange. Could you try setting the AccountId rather than the Account reference, though it should have the same effect presumably. Also, what do you mean by inaccessible?

 

Opportunity o = new Opportunity(Name='Opp1',AccountId=a.Id,StageName='Contract Sent',
        CloseDate=Date.newInstance(2011, 07, 15), Pricing_is_approved__c=1, Country2__c='Sweden',
        CurrencyIsoCode='SEK');
        insert o;
This was selected as the best answer
bob_buzzardbob_buzzard

That does seem odd - I'd expect the account to be stored by reference on the opportunity, and as you have carried out all your actions on the same instance it seems counterintuitive that the original fields would be available and not the one that you added via an update.

 

Have you tried it without the startTest/stopTest calls - I can't find anything in the docs to indicate they would add special behaviour, but its difficult to know what else to suggest.  If field level security was an issue, I'd expect your update to fail.

frofrik2frofrik2

It did acutally work to change to setting the AccountId instead, now I'm able to query the fields!

 

Thanks for the help!