Task[] tasks = [SELECT Id FROM Task WHERE What.RecordType.Id = :AccountsHelper.getPersonAccountRecordType().Id LIMIT 1];
System.debug('tasks size : ' + tasks.size());
Task[] tasks2 = [SELECT Id FROM Task WHERE What.RecordTypeId = :AccountsHelper.getPersonAccountRecordType().Id LIMIT 1];
System.debug('tasks2 size : ' + tasks2.size());
DEBUG|tasks size : 1
DEBUG|tasks2 size : 0
Looks like when using polymorphic fields in unit tests RecordTypeId is not working as expected.
When I used the same query with RecordTypeId outside unit tests in:
- dev console query
- salesforce inspector query
- created selector and invoked in dev console
All works as expected.
I bumped API version to 52 and 53 and still the same behaviour in unit test.
Anybody else experienced the same?
Best Answer
Update:
This is now logged as W-10188438. At this time, there is no ETA for a resolution. The basic problem, as stated by support, is:
This means that, for the foreseeable future, developers will need to be aware that this problem exists, and what the solution to the problem is. Fortunately, the workaround is simple and relatively limited in scope.
I wrote some unit tests:
And found that this only fails for What.RecordTypeId, and only if the Account is a Person Account. In other words, this is definitely a bug, and should be reported. This test is portable to any org that has Person Accounts enabled and minimal validation/automation/etc. You can even run this in a Scratch Org with the feature enabled.
Edited: Also, selecting What.RecordTypeId or What.RecordType.Id results in a null value, so there's some additional problems with Record Types and Person Accounts. I have updated the tests accordingly.