0

Consider a general relation 'Foo.Bar' and some SObject type. An example might be 'Account.Id' and an Opportunity.SObjectType.

Can I determine SObjectType Foo is referring to without getting the field map and checking each DescribeFieldResult to find the field where getRelationshipName() == 'Foo' and then extracting potential types from getReferenceTo()?

8
  • 1
    Sounds like a XY problem but one way is to parse the ID prefix
    – identigral
    Oct 19 '21 at 16:02
  • @identigral Id prefix is not relevant for the question, as there's no Id to work with at all.
    – ipavlic
    Oct 19 '21 at 16:10
  • Account.Id is an Id
    – identigral
    Oct 19 '21 at 16:16
  • @identigral Account.Id is a plain String. My question is, knowing where the relation starts at (e.g. an Opportunity), is it possible to dynamically determine what kind of an SObject "Account" substring might refer to. I don't have an Opportunity which has Account and Id selected, nor am I interested in it. I've edited the question to make it clearer.
    – ipavlic
    Oct 19 '21 at 16:20
  • You can, but I agree with this being an X-Y Problem. Let's say this question was a User Story, I'd have to ask for more information before writing code that would definitively solve the requirements.
    – sfdcfox
    Oct 19 '21 at 16:27
2

If you know the field you want to examine at compile-time, then you can get the describe information for that field directly.

Opportunity.AccountId.getDescribe().getReferenceTo()[0].getDescribe().getName()

If this is more dynamic, and you don't know which field to examine until run-time, then there's no way around needing to get the field map from the DescribeSObjectResult, though since you can get a Map<String, DescribeFieldResult> (the keys are API names) you can simply get the specific field you're looking for.

Opportunity.SObjectType.getDescribe().fields.getMap().get('AccountId').getDescribe().getReferenceTo()[0].getDescribe().getName())

If you have an instance of an SObject with the target relationship field populated, you can use that Id to get at the SObject type

myOpp.AccountId.getSObjectType().getDescribe().getName()

3
  • Thank you for the confirmation! Unfortunately, general relations do not contain field names, but relationship names.
    – ipavlic
    Oct 19 '21 at 16:27
  • @ipavlic I think this is where we come to identigral and sfdcfox's request for more information. Knowing the scenario you're trying to apply this situation to would be helpful. Probably the most helpful clarification would be "how are you getting the "Account.Id" string?" and "can you pass different/additional information?". If you could pass "AccountId" instead of "Account.Id", that'd make your life easier (or at least free you of the burden of that loop you want to avoid).
    – Derek F
    Oct 19 '21 at 16:43
  • It's the inverse of reading fields. Given a relation and an SObject instance, we can read the fields through getSObject and get. To write a field, we can use put and putSObject. However, if intermediate objects do not exist, I'd like to create them. It's all related to apexfp.org for which I'm writing a mapping function factory.
    – ipavlic
    Oct 19 '21 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.