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
William Woodson 3William Woodson 3 

field not being retreived in soql after insert

Hello,  I have a class that I am inserting a record for unit testing.  The code execute without issue however one of the fields I am setting in the insert is returning as null when retrieving the record in soql.  The code is executing as the system admin user and I have verified the field is visible and editable by that profile.  Any idea on why this might be occurring?  Thanks!

public static T createT(Id contactId) {
        T t = new T__c();
        t.T_Type__c = 'Data';
        insert t;
        T__c t2 = [Select Id, T_Type__c From T__c Where Id = :t.Id];

        system.debug('bw-test: t2.Id = ' + t2.Id);
        // displays same id as was inserted

        system.debug('bw-test: t2.T Type = ' + t2.T_Type__c);
        // displays -> bw-test: t2.T Type = null

        system.debug('bw-test: t.T Type = ' + t.T_Type__c);
        // displays -> bw-test: t.T Type = Data



Hi William, 

IF the access levels are correct, which you've already checked.
Then it looks like there might be an automation (a trigger or workflow / rocess builder / flow ) that is clearing out the field. 

ANUTEJANUTEJ (Salesforce Developers) 
Hi William.

Can you try disabling triggers automated tools that work before the record is getting inserted and in case if the entered data is not being changed to null then try enabling one by one to see which is causing the issue.

I hope this helps and in case if this comes in handy can you please choose this as the best answer so that it can be useful for others in the future.