Please add the check of `adaptedEntityId` back so the if looks like:
```
if (adaptedEntityId == INVALID_TID && !validEntities.empty())
{
}
``
This check is important so `validEntities` are not used in case `adaptedEntityId` has valid value