Check the custom field type and join to the appropriate entity (contact or activity); display query in debug output