Skip to main content

SLAM_metadata_relationships

Overview

Retrieves Salesforce object relationship definitions to table METADATA_RELATIONSHIPS.

Each row represents a parent-child relationship between two Salesforce objects, including the lookup or master-detail field that defines the relationship.

info

A relationship record describes a single directional link from a child object to a parent object through a specific field. For example, the standard lookup from Contact.AccountId to Account produces a relationship record with:

  • ChildSobject.QualifiedApiName = Contact
  • ParentSobject.QualifiedApiName = Account
  • Field.QualifiedApiName = AccountId
  • RelationshipName = Contacts (the name used in SOQL relationship traversal)

Parameters

NameTypeDefaultDescription
@exists_actionVARCHAR(20)NULLControls the behavior when the destination table METADATA_RELATIONSHIPS already exists. If NULL, uses the value from SLAM_Settings.default_exists_action (defaulted to 'drop' during initial setup).

Valid options:

  • 'drop' - Drop the existing object and create a new one (default)
  • 'rename' - Rename the existing object by appending a timestamp suffix (format: _YYYYMMDDTHHMMSS_MMM)
  • 'fail' - Throws a SQL exception if the destination object already exists

Output Table

Results are written to METADATA_RELATIONSHIPS. The table is created automatically if it does not exist.

ColumnDescription
IdSalesforce internal record identifier
DurableIdStable composite identifier in the format ParentSobjectId.ChildSobjectId.FieldId
ParentSobjectIdSalesforce internal identifier for the parent object (e.g. Account, but custom objects return a Salesforce Id)
ParentSobject.QualifiedApiNameAPI name of the parent object - this is usually what you want (e.g. Custom_Parent_Object__c)
ChildSobjectIdSalesforce internal identifier for the child object (e.g. Contact, but custom objects return a Salesforce Id)
ChildSobject.QualifiedApiNameAPI name of the child object - this is usually what you want (e.g. Custom_Child_Object__c)
FieldIdSalesforce internal identifier for the lookup/master-detail field
Field.QualifiedApiNameAPI name of the field on the child object that holds the relationship - this is usually what you want (e.g. Custom_Lookup_Field__c)
RelationshipInfoIdComposite identifier for the relationship metadata
RelationshipNameThe SOQL relationship name used for traversal (e.g. Contacts). NULL for system-managed relationships that are not traversable via SOQL
IsCascadeDelete1 if deleting the parent record also deletes child records (master-detail behavior)
IsDeprecatedAndHidden1 if the relationship is object is unavailable for the current version
IsRestrictedDelete1 if the parent record cannot be deleted while child records exist
JunctionIdListNamesThe names of the lists of junction IDs associated with an object. Each ID represents an object that has a relationship with the associated object.
Filtering by relationship name

RelationshipName is the name you use in SOQL dot-notation queries. For example, a relationship with RelationshipName = 'Contacts' on the Account object allows queries like:

-- SOQL
SELECT Id, Name, (SELECT Id, FirstName FROM Contacts) FROM Account

Rows where RelationshipName is NULL are system-managed relationships (such as Owner, CreatedBy) and are not directly traversable using the child-relationship syntax.

Usage Examples

Retrieve all relationships in the org:

EXEC dbo.SLAM_metadata_relationships;

SELECT * FROM METADATA_RELATIONSHIPS;

Find all child objects of Account:

EXEC dbo.SLAM_metadata_relationships;

SELECT
[ChildSobject.QualifiedApiName] AS ChildObject,
[Field.QualifiedApiName] AS LookupField,
RelationshipName
FROM METADATA_RELATIONSHIPS
WHERE [ParentSobject.QualifiedApiName] = 'Account'
ORDER BY [ChildSobject.QualifiedApiName];

Find all parent objects that a Contact references:

SELECT
[ParentSobject.QualifiedApiName] AS ParentObject,
[Field.QualifiedApiName] AS LookupField,
IsCascadeDelete,
IsRestrictedDelete
FROM METADATA_RELATIONSHIPS
WHERE [ChildSobject.QualifiedApiName] = 'Contact'
ORDER BY [ParentSobject.QualifiedApiName];

Refresh with the rename exists action to preserve the previous results:

EXEC dbo.SLAM_metadata_relationships
@exists_action = 'rename';
  • SLAM_describe_objects - retrieves the list of all Salesforce objects and their attributes.
  • SLAM_data_dictionary - retrieves detailed field definitions for all (or filtered) objects, including the ReferenceTo attribute that lists the parent objects for each lookup or master-detail field.
  • SLAM_query_soap - use SOQL dot-notation with relationship names from METADATA_RELATIONSHIPS to query related fields in a single operation.