If I know the API Name, how can I get and work with the Schema.ChildRelationship?
Like many SFDC developers, we have SObjects which are related to other custom relationships.
So, for example, MyCustomObject__c
could have a relationship with MyCustomRelationship__c
which in Apex we might normally access with MyCustomObject__c.MyCustomRelationship__r
Of course, this isn't a one-off scenario, so we might also have MyCustomObject2__c.MyCustomRelationship2__r
, MyCustomObject3__c.MyCustomRelationship3__r
, etc.
And of course these relationships don't need to be one-to-one or so neatly organized.
But the thing is, we want a generic way to process some of these, so we'd like to make a method with a signature like:
doSomething(SObject sObj, SObjectField sObjectField)
only these aren't actually SObjectFields but rather Schema.ChildRelationships, so in actually we would need a signature like:
doSomething(SObject sObj, Schema.ChildRelationship childRelationship)
But then this still present two problems:
How can I actually get the value for Schema.ChildRelationship from the API name (e.g.
MyCustomObject__c.MyCustomRelationship__r
)?
Having Schema.ChildRelationship, how I can apply it to my SObject to do something like
List<SObject> relatedSObjectList = sObj.get(childRelationship)
apex api schema childrelationship generics
add a comment |
Like many SFDC developers, we have SObjects which are related to other custom relationships.
So, for example, MyCustomObject__c
could have a relationship with MyCustomRelationship__c
which in Apex we might normally access with MyCustomObject__c.MyCustomRelationship__r
Of course, this isn't a one-off scenario, so we might also have MyCustomObject2__c.MyCustomRelationship2__r
, MyCustomObject3__c.MyCustomRelationship3__r
, etc.
And of course these relationships don't need to be one-to-one or so neatly organized.
But the thing is, we want a generic way to process some of these, so we'd like to make a method with a signature like:
doSomething(SObject sObj, SObjectField sObjectField)
only these aren't actually SObjectFields but rather Schema.ChildRelationships, so in actually we would need a signature like:
doSomething(SObject sObj, Schema.ChildRelationship childRelationship)
But then this still present two problems:
How can I actually get the value for Schema.ChildRelationship from the API name (e.g.
MyCustomObject__c.MyCustomRelationship__r
)?
Having Schema.ChildRelationship, how I can apply it to my SObject to do something like
List<SObject> relatedSObjectList = sObj.get(childRelationship)
apex api schema childrelationship generics
add a comment |
Like many SFDC developers, we have SObjects which are related to other custom relationships.
So, for example, MyCustomObject__c
could have a relationship with MyCustomRelationship__c
which in Apex we might normally access with MyCustomObject__c.MyCustomRelationship__r
Of course, this isn't a one-off scenario, so we might also have MyCustomObject2__c.MyCustomRelationship2__r
, MyCustomObject3__c.MyCustomRelationship3__r
, etc.
And of course these relationships don't need to be one-to-one or so neatly organized.
But the thing is, we want a generic way to process some of these, so we'd like to make a method with a signature like:
doSomething(SObject sObj, SObjectField sObjectField)
only these aren't actually SObjectFields but rather Schema.ChildRelationships, so in actually we would need a signature like:
doSomething(SObject sObj, Schema.ChildRelationship childRelationship)
But then this still present two problems:
How can I actually get the value for Schema.ChildRelationship from the API name (e.g.
MyCustomObject__c.MyCustomRelationship__r
)?
Having Schema.ChildRelationship, how I can apply it to my SObject to do something like
List<SObject> relatedSObjectList = sObj.get(childRelationship)
apex api schema childrelationship generics
Like many SFDC developers, we have SObjects which are related to other custom relationships.
So, for example, MyCustomObject__c
could have a relationship with MyCustomRelationship__c
which in Apex we might normally access with MyCustomObject__c.MyCustomRelationship__r
Of course, this isn't a one-off scenario, so we might also have MyCustomObject2__c.MyCustomRelationship2__r
, MyCustomObject3__c.MyCustomRelationship3__r
, etc.
And of course these relationships don't need to be one-to-one or so neatly organized.
But the thing is, we want a generic way to process some of these, so we'd like to make a method with a signature like:
doSomething(SObject sObj, SObjectField sObjectField)
only these aren't actually SObjectFields but rather Schema.ChildRelationships, so in actually we would need a signature like:
doSomething(SObject sObj, Schema.ChildRelationship childRelationship)
But then this still present two problems:
How can I actually get the value for Schema.ChildRelationship from the API name (e.g.
MyCustomObject__c.MyCustomRelationship__r
)?
Having Schema.ChildRelationship, how I can apply it to my SObject to do something like
List<SObject> relatedSObjectList = sObj.get(childRelationship)
apex api schema childrelationship generics
apex api schema childrelationship generics
edited Jan 25 at 14:54
battery.cord
6,83251745
6,83251745
asked Jan 25 at 13:02
Brian KesslerBrian Kessler
1,6411131
1,6411131
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I'll answer your second question first.
To get the child records if you know the relationship name, you pass a String
argument to the SObject.getSObjects
method:
MyObject__c parent = new MyObject__c();
String relatioonshipName = 'Children__r';
List<Child__c> children = parent.getSObjects(relationshipName);
Now back to your first question. There is no way to get a specific ChildRelationship
without looping, but it also doesn't help you if you already have the relationship name as a string. Basically, your loop structure would look like:
ChildRelationship relationship;
for (ChildRelationship relation : SObjectType.MyObject__c.getChildRelationships())
{
if (relation.getRelationshipName() = relationshipName)
{
relationship = relation;
}
}
If you decide you need to do this looping (for example checking permissions, I highly suggest you implement a DescribeCache
where you cache these relationships and call a signature like:
public static ChildRelationship getChildRelationship(SObjectType sObjectType, String name)
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "459"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f247993%2fif-i-know-the-api-name-how-can-i-get-and-work-with-the-schema-childrelationship%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I'll answer your second question first.
To get the child records if you know the relationship name, you pass a String
argument to the SObject.getSObjects
method:
MyObject__c parent = new MyObject__c();
String relatioonshipName = 'Children__r';
List<Child__c> children = parent.getSObjects(relationshipName);
Now back to your first question. There is no way to get a specific ChildRelationship
without looping, but it also doesn't help you if you already have the relationship name as a string. Basically, your loop structure would look like:
ChildRelationship relationship;
for (ChildRelationship relation : SObjectType.MyObject__c.getChildRelationships())
{
if (relation.getRelationshipName() = relationshipName)
{
relationship = relation;
}
}
If you decide you need to do this looping (for example checking permissions, I highly suggest you implement a DescribeCache
where you cache these relationships and call a signature like:
public static ChildRelationship getChildRelationship(SObjectType sObjectType, String name)
add a comment |
I'll answer your second question first.
To get the child records if you know the relationship name, you pass a String
argument to the SObject.getSObjects
method:
MyObject__c parent = new MyObject__c();
String relatioonshipName = 'Children__r';
List<Child__c> children = parent.getSObjects(relationshipName);
Now back to your first question. There is no way to get a specific ChildRelationship
without looping, but it also doesn't help you if you already have the relationship name as a string. Basically, your loop structure would look like:
ChildRelationship relationship;
for (ChildRelationship relation : SObjectType.MyObject__c.getChildRelationships())
{
if (relation.getRelationshipName() = relationshipName)
{
relationship = relation;
}
}
If you decide you need to do this looping (for example checking permissions, I highly suggest you implement a DescribeCache
where you cache these relationships and call a signature like:
public static ChildRelationship getChildRelationship(SObjectType sObjectType, String name)
add a comment |
I'll answer your second question first.
To get the child records if you know the relationship name, you pass a String
argument to the SObject.getSObjects
method:
MyObject__c parent = new MyObject__c();
String relatioonshipName = 'Children__r';
List<Child__c> children = parent.getSObjects(relationshipName);
Now back to your first question. There is no way to get a specific ChildRelationship
without looping, but it also doesn't help you if you already have the relationship name as a string. Basically, your loop structure would look like:
ChildRelationship relationship;
for (ChildRelationship relation : SObjectType.MyObject__c.getChildRelationships())
{
if (relation.getRelationshipName() = relationshipName)
{
relationship = relation;
}
}
If you decide you need to do this looping (for example checking permissions, I highly suggest you implement a DescribeCache
where you cache these relationships and call a signature like:
public static ChildRelationship getChildRelationship(SObjectType sObjectType, String name)
I'll answer your second question first.
To get the child records if you know the relationship name, you pass a String
argument to the SObject.getSObjects
method:
MyObject__c parent = new MyObject__c();
String relatioonshipName = 'Children__r';
List<Child__c> children = parent.getSObjects(relationshipName);
Now back to your first question. There is no way to get a specific ChildRelationship
without looping, but it also doesn't help you if you already have the relationship name as a string. Basically, your loop structure would look like:
ChildRelationship relationship;
for (ChildRelationship relation : SObjectType.MyObject__c.getChildRelationships())
{
if (relation.getRelationshipName() = relationshipName)
{
relationship = relation;
}
}
If you decide you need to do this looping (for example checking permissions, I highly suggest you implement a DescribeCache
where you cache these relationships and call a signature like:
public static ChildRelationship getChildRelationship(SObjectType sObjectType, String name)
answered Jan 25 at 14:41
Adrian Larson♦Adrian Larson
108k19115246
108k19115246
add a comment |
add a comment |
Thanks for contributing an answer to Salesforce Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f247993%2fif-i-know-the-api-name-how-can-i-get-and-work-with-the-schema-childrelationship%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown