6

There is some strangeness in the some standard Childrelationships. Most Relationships are as expected, but let us take Account and Quote as an example:

On Quote we have a standard lookup field Quote.AccountId - which is nice. Only if I do this

Schema.getGlobalDescribe().get('Account').getDescribe().getChildRelationships() 

I will only get as a result (reduced)

... , {
  "cascadeDelete" : false,
  "childSObject" : "Quote",
  "deprecatedAndHidden" : false,
  "field" : "AccountId",
  "junctionIdListNames" : [ ],
  "junctionReferenceTo" : [ ],
  "relationshipName" : null,
  "restrictedDelete" : false
}, ...

Now here is the strangeness: why the relationshipName = null?? This I would NEVER expect. Instead I would expect relationshipName = 'Accounts' (plural of 'Account')

When I create a custom lookup from Quote to Account called 'Account__c', I will have to provide such a ChildRelationshipName. Here I would provide 'Accounts' as RelationshipName. This will result as expected in

... , {
  "cascadeDelete" : false,
  "childSObject" : "Quote",
  "deprecatedAndHidden" : false,
  "field" : "Account__c",
  "junctionIdListNames" : [ ],
  "junctionReferenceTo" : [ ],
  "relationshipName" : "Quotes__r",
  "restrictedDelete" : false

},...

So relationshipName = "Quotes__r". Brilliant. Exactly as expected! But why Salesforce can omit that relationshipName sometimes?

Why the heck do I need this?

I need to get all childRelationships dynamically to build a dynamic SOQL to query child objects dynamically using subqueries. I need to query all the quotes on account, too. Of course with the very same dynamic code. Now without a relationshipName I can not create the subquery. And even if I would be able to guess the name, the subquery does not work:

database.query('select Name, (select Name from Quotes) from Account where Id = \'0011n00001yZsqCAAS\' '); 

This will cause an exception: System.QueryException: Didn't understand relationship 'Quotes' in FROM part of query call...

Sure, because there is no such child relationship Quotes, because Salesforce has left that thing blank.

But WHY? Is this a bug or a feature? Is there a pattern why and how Salesforce has decided for standard lookup fields in most cases provided a relationshipName but sometimes in rare cases to leave it blank? This feels super inconsistent to me!

My custom lookup works perfectly database.query('select Name, (select Name from Quotes__r) from Account where Id = '0011n00001yZsqCAAS' ');

Why relationshipName is not a mandatory thing even for salesforce standard fields? Is it on purpose to break the ability to write such subqueries? Has it other reasons?

5
  • There are other examples besides the one you cited; for me, there is no relationship name for the Order.BilltoContactId lookup field to Contact.
    – cropredy
    Sep 24, 2021 at 0:52
  • @cropredy yes, I know. I just wonder WHY salesforce might have made this hard to understand design decision. For me it breaks a consistent behavior and leads to unexpected results for our clients. If it would follow a pattern or if there would be some background we could explain it to the users and all would be fine. But it seems undocumented and feels just random. So it is lowering the trust into the system. sure the workaround is possible by using an additional lookup kept in sync somehow, But usually there is already lot of stuff going on lots of limits close to max...
    – Uwe Heim
    Sep 27, 2021 at 15:32
  • I'm as frustrated as you are - I have no idea why.
    – cropredy
    Sep 27, 2021 at 15:40
  • Cool question! I did also some research (out of curiosity) and maybe it is just in some very specific cases, e.g. Accounts --> Quotes. It might make sense, if we see it this way: a quote actually always belongs to an opportunity. So childrelationship for opportunity is there. and oppys again have a childrelationship to quotes. it might not help regarding your use case, because unfortunately you can't aggregate relationships more than one level. Do you have another examples besides from the one mentioned with quotes?
    – derroman
    Oct 1, 2021 at 8:59
  • 1
    @derroman there are many many other examples, but I have nothing at hand and it would take me quite some time to drill more examples out. I would do that, if someone at salesforce would be willing to talk with us and if it would help to proceed with the clarification. Otherwise I would like to spare the effort. But the standard fields are full of them. That's for sure.
    – Uwe Heim
    Oct 1, 2021 at 10:35

0

You must log in to answer this question.

Browse other questions tagged .