3

I'm having an issue with the Sobject method "isSet()". It works well if I query for a record or create a record outright. But does not work correctly for Trigger objects. On trigger objects, isSet only seems to return true if the value for the field is not null.

E.X.

Account acc = [Select Null_Field_Test__c from Account LIMIT 1)
System.assert(acc.isSet('Null_Field_Test__c'));

Account accTwo = new Account();
accTwo.Null_Field_Test__c = null;
System.assert(accTwo.isSet('Null_Field_Test__c'));

But if I try it in a trigger

trigger test on Account (after insert, after update) {
   System.debug('is set: ' + Trigger.new[0].isSet('Null_Field_Test__c'));
}

Then I always get false until I put a non-null value into "Null_Field_Test__c". Is this just a bug? Is there any work around for this?

1 Answer 1

3

This behavior might make sense if we take a specific interpretation of the documentation for isSet(), which states (at time of writing, Winter '22)

Returns information about the queried sObject field. Returns true if the sObject field is populated, either by direct assignment or by inclusion in a SOQL query. Returns false if the sObject field is not set. If an invalid field is specified, an SObjectException is thrown.

Since we aren't querying for records in trigger context variables (TCVs for short), and aren't (ourselves) directly setting values on records in TCVs (only records in trigger.new/newmap are editable, and further only in before contexts), it's not unreasonable that isSet() is returning false.

Given that TCVs pull all object fields, I don't think it would be too helpful if isSet() would return true for fields on those records. Returning false seems to be a safe default.

I don't think there's a workaround for null fields, but besides that, a simple null check myRecord.myField__c != null would suffice.

2
  • 1
    That's unfortunate as I have a function that queries a record for more information only if it isn't already set on the record. For trigger objects, it preforms unnecessary queries because of the interaction here. As far as I know, there's also no way to differentiate these Trigger sobjects from non trigger sobjects just using the Sobject record. Nov 10, 2021 at 21:50
  • 1
    I would be curious if adding a reference to the field causes isSet to flip to true.
    – Adrian Larson
    Nov 11, 2021 at 1:07

You must log in to answer this question.

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