NebulaQueryAndSearch icon indicating copy to clipboard operation
NebulaQueryAndSearch copied to clipboard

addPolymorphicFields(SObjectField) uses hardcoded Who field when no mapping is specified even if the queried SObject does not contain such fields

Open TrangOul opened this issue 1 year ago • 0 comments

System.debug(
	new Query(Case.SObjectType)
		.addPolymorphicFields(Case.OwnerId)
		.getQuery()
);

result:

SELECT CaseNumber, Id, Who.Alias, Who.Email, Who.FirstName, (a lot of Who.X fields)
FROM Case

Obviously, such a query will fail. Result with getResults(): System.QueryException: Didn't understand relationship 'Who' in field path. (...)

The same happens if there is a Who relationship on the queried SObject, but we choose a different field, which is ignored:

System.debug(
	new Query(Task.SObjectType)
		.addPolymorphicFields(Task.WhatId)
		.getQuery()
);

result:

SELECT Id, Subject, Who.Alias, Who.Email, Who.FirstName, (a lot of Who.X fields)
FROM Task

This query won't throw an exception, which is even worse, since is silently skips the field we expected to be queried.

TrangOul avatar Aug 12 '24 11:08 TrangOul