+ Start a Discussion
Ken KoellnerKen Koellner 

SOQL Query parent fields via objectId in ObjectTerritory2Association

Does anyone know if there's a way to get the parent fields for the Account parent object via the ObjectId relationship in ObjectTerritory2Association?

The query below works to get the Name.  But I don't know of a way to get the other fields.  It's actually one custom field that I'm hoping to get in my query.

select ObjectId , Object.Name,  Territory2.MarketUser_Last_Modified_Date__c ,LastModifiedDate
from ObjectTerritory2Association
where   Object.Type = 'Account' limit 10
Mahesh DMahesh D
Hi Ken,

You can try below SOQL query:
 
SELECT Id, Object.Name, ObjectId,SobjectType, Territory2Id FROM ObjectTerritory2Association WHERE SobjectType = 'Account'

Please do let me know if it helps you.

Regards,
Mahesh
Ken KoellnerKen Koellner
That doesn't really help.  I know I can do that.  What I want to do would be the equivelent of
 
SELECT Id, Object.Name, Object.MyCustomAccountField__c, ObjectId, SobjectType, Territory2Id FROM ObjectTerritory2Association WHERE SobjectType = 'Account'
The thing is "ObjectId" and the relationship "Object" don't appear to allow getting any Account fields other than Id and Name.  What I need is the equivelent of a cast in a program language where the "object" reference could be cast to Account.

 
Jugbeer BholaJugbeer Bhola
Did you ever find a resolution for this?
 
Praveen Krishna 1Praveen Krishna 1
I would suggest using innerquery for this.
Ex
Select ID, Name From account Where ID in (SELECT  ObjectId FROM ObjectTerritory2Association Where object.type='account')

You can add the fields you want from account in the main query.