If I know the API Name, how can I get and work with the Schema.ChildRelationship?












8















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:




  1. How can I actually get the value for Schema.ChildRelationship from the API name (e.g. MyCustomObject__c.MyCustomRelationship__r)?



  2. Having Schema.ChildRelationship, how I can apply it to my SObject to do something like



    List<SObject> relatedSObjectList = sObj.get(childRelationship)












share|improve this question





























    8















    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:




    1. How can I actually get the value for Schema.ChildRelationship from the API name (e.g. MyCustomObject__c.MyCustomRelationship__r)?



    2. Having Schema.ChildRelationship, how I can apply it to my SObject to do something like



      List<SObject> relatedSObjectList = sObj.get(childRelationship)












    share|improve this question



























      8












      8








      8


      2






      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:




      1. How can I actually get the value for Schema.ChildRelationship from the API name (e.g. MyCustomObject__c.MyCustomRelationship__r)?



      2. Having Schema.ChildRelationship, how I can apply it to my SObject to do something like



        List<SObject> relatedSObjectList = sObj.get(childRelationship)












      share|improve this question
















      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:




      1. How can I actually get the value for Schema.ChildRelationship from the API name (e.g. MyCustomObject__c.MyCustomRelationship__r)?



      2. 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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      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






















          1 Answer
          1






          active

          oldest

          votes


















          9














          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)





          share|improve this answer























            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
            });


            }
            });














            draft saved

            draft discarded


















            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









            9














            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)





            share|improve this answer




























              9














              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)





              share|improve this answer


























                9












                9








                9







                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)





                share|improve this answer













                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)






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 25 at 14:41









                Adrian LarsonAdrian Larson

                108k19115246




                108k19115246






























                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Human spaceflight

                    Can not write log (Is /dev/pts mounted?) - openpty in Ubuntu-on-Windows?

                    張江高科駅