Header Ads

Header ADS

SOQL Query for Polymorphic Relationships in Salesforce

If you need to access parent records from child records in a polymorphic relationship, you can easily retrieve these diverse parent records using an SOQL query. In Field Service Lightning, there is a field named ParentRecord on the ServiceAppointment object, which holds different types of records based on selection. Refer to the images below for examples.




If you would like to know the details of a service appointment and its parent record, you need to use TYPEOF in the SOQL query. The parent record can be a Work Order, Work Order Line Item, Account, or Opportunity. Please refer to the query below, which retrieves all the information for both the service appointment and its parent records.


SELECT Id, Reporting_Contact_1__c, Reporting_Contact_2__c, ParentRecordId, TYPEOF ParentRecord WHEN WorkOrder THEN WorkOrderNumber, Subject WHEN Account THEN Name, Phone WHEN Opportunity THEN Name, StageName ELSE Name END FROM ServiceAppointment


No comments

Powered by Blogger.