How to populate a lookup field in custom object using Apex











up vote
2
down vote

favorite












I am building an app which reads information from an external API and then populates data in my App for further processing. I have two kinds of custom objects for this. First kind (e.g., BasicProfile) stores all the retrieved information. And second kind (e.g., Summary) stores the derived information.



Summary also needs to have a lookup for BasicProfile and this is working fine in Salesforce UI. But when I try to populate this lookup field using Apex code, it is giving me error "Illegal assignment from BasicProfile__c to Id". Please let me know how can I populate the lookup field using Apex.



Following is my existing code in Apex. (If I remove the lookup part, everything else is working fine, both the objects are inserted separately.)



public BasicProfile__c getBasicProfile(Map<String, Object> basic_profile){
BasicProfile__c obj = new BasicProfile__c();
obj.Zip__c = (String)basic_profile.get('Zip');
insert obj;
return obj;
}

public PageReference doInsert() {
Summary__c objdlt = new Summary__c();
objdlt.Name__c = nameVal;

# Get json response from external API

Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(json_response);

Map<String, Object> basic_profile = (Map<String, Object>)m.get('BasicProfile');

# THIS LINE
objdlt.BasicProfile__c = getBasicProfile(basic_profile);
insert objdlt;

pagereference ref = new pagereference(redirect_page);
ref.setredirect(true);
return ref;
}









share|improve this question







New contributor




padmanabh pande is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    2
    down vote

    favorite












    I am building an app which reads information from an external API and then populates data in my App for further processing. I have two kinds of custom objects for this. First kind (e.g., BasicProfile) stores all the retrieved information. And second kind (e.g., Summary) stores the derived information.



    Summary also needs to have a lookup for BasicProfile and this is working fine in Salesforce UI. But when I try to populate this lookup field using Apex code, it is giving me error "Illegal assignment from BasicProfile__c to Id". Please let me know how can I populate the lookup field using Apex.



    Following is my existing code in Apex. (If I remove the lookup part, everything else is working fine, both the objects are inserted separately.)



    public BasicProfile__c getBasicProfile(Map<String, Object> basic_profile){
    BasicProfile__c obj = new BasicProfile__c();
    obj.Zip__c = (String)basic_profile.get('Zip');
    insert obj;
    return obj;
    }

    public PageReference doInsert() {
    Summary__c objdlt = new Summary__c();
    objdlt.Name__c = nameVal;

    # Get json response from external API

    Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(json_response);

    Map<String, Object> basic_profile = (Map<String, Object>)m.get('BasicProfile');

    # THIS LINE
    objdlt.BasicProfile__c = getBasicProfile(basic_profile);
    insert objdlt;

    pagereference ref = new pagereference(redirect_page);
    ref.setredirect(true);
    return ref;
    }









    share|improve this question







    New contributor




    padmanabh pande is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I am building an app which reads information from an external API and then populates data in my App for further processing. I have two kinds of custom objects for this. First kind (e.g., BasicProfile) stores all the retrieved information. And second kind (e.g., Summary) stores the derived information.



      Summary also needs to have a lookup for BasicProfile and this is working fine in Salesforce UI. But when I try to populate this lookup field using Apex code, it is giving me error "Illegal assignment from BasicProfile__c to Id". Please let me know how can I populate the lookup field using Apex.



      Following is my existing code in Apex. (If I remove the lookup part, everything else is working fine, both the objects are inserted separately.)



      public BasicProfile__c getBasicProfile(Map<String, Object> basic_profile){
      BasicProfile__c obj = new BasicProfile__c();
      obj.Zip__c = (String)basic_profile.get('Zip');
      insert obj;
      return obj;
      }

      public PageReference doInsert() {
      Summary__c objdlt = new Summary__c();
      objdlt.Name__c = nameVal;

      # Get json response from external API

      Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(json_response);

      Map<String, Object> basic_profile = (Map<String, Object>)m.get('BasicProfile');

      # THIS LINE
      objdlt.BasicProfile__c = getBasicProfile(basic_profile);
      insert objdlt;

      pagereference ref = new pagereference(redirect_page);
      ref.setredirect(true);
      return ref;
      }









      share|improve this question







      New contributor




      padmanabh pande is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I am building an app which reads information from an external API and then populates data in my App for further processing. I have two kinds of custom objects for this. First kind (e.g., BasicProfile) stores all the retrieved information. And second kind (e.g., Summary) stores the derived information.



      Summary also needs to have a lookup for BasicProfile and this is working fine in Salesforce UI. But when I try to populate this lookup field using Apex code, it is giving me error "Illegal assignment from BasicProfile__c to Id". Please let me know how can I populate the lookup field using Apex.



      Following is my existing code in Apex. (If I remove the lookup part, everything else is working fine, both the objects are inserted separately.)



      public BasicProfile__c getBasicProfile(Map<String, Object> basic_profile){
      BasicProfile__c obj = new BasicProfile__c();
      obj.Zip__c = (String)basic_profile.get('Zip');
      insert obj;
      return obj;
      }

      public PageReference doInsert() {
      Summary__c objdlt = new Summary__c();
      objdlt.Name__c = nameVal;

      # Get json response from external API

      Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(json_response);

      Map<String, Object> basic_profile = (Map<String, Object>)m.get('BasicProfile');

      # THIS LINE
      objdlt.BasicProfile__c = getBasicProfile(basic_profile);
      insert objdlt;

      pagereference ref = new pagereference(redirect_page);
      ref.setredirect(true);
      return ref;
      }






      custom-object lookup






      share|improve this question







      New contributor




      padmanabh pande is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      padmanabh pande is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      padmanabh pande is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 7 hours ago









      padmanabh pande

      111




      111




      New contributor




      padmanabh pande is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      padmanabh pande is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      padmanabh pande is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          4
          down vote













          objdlt.BasicProfile__c = getBasicProfile(basic_profile).Id;


          getBasicProfile(basic_profile) is returning the actual BasicProfile__c object, when you really just need its Id. So, access it via Id.



          It makes more sense when you look at it this way:



          BasicProfile__c profile = getBasicProfile(basic_profile);

          objdlt.BasicProfile__c = profile.Id; // not profile, a reference to an object, not an id


          Maybe also rename getBasicProfile to createBasicProfile?






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


            }
            });






            padmanabh pande is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f243897%2fhow-to-populate-a-lookup-field-in-custom-object-using-apex%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








            up vote
            4
            down vote













            objdlt.BasicProfile__c = getBasicProfile(basic_profile).Id;


            getBasicProfile(basic_profile) is returning the actual BasicProfile__c object, when you really just need its Id. So, access it via Id.



            It makes more sense when you look at it this way:



            BasicProfile__c profile = getBasicProfile(basic_profile);

            objdlt.BasicProfile__c = profile.Id; // not profile, a reference to an object, not an id


            Maybe also rename getBasicProfile to createBasicProfile?






            share|improve this answer



























              up vote
              4
              down vote













              objdlt.BasicProfile__c = getBasicProfile(basic_profile).Id;


              getBasicProfile(basic_profile) is returning the actual BasicProfile__c object, when you really just need its Id. So, access it via Id.



              It makes more sense when you look at it this way:



              BasicProfile__c profile = getBasicProfile(basic_profile);

              objdlt.BasicProfile__c = profile.Id; // not profile, a reference to an object, not an id


              Maybe also rename getBasicProfile to createBasicProfile?






              share|improve this answer

























                up vote
                4
                down vote










                up vote
                4
                down vote









                objdlt.BasicProfile__c = getBasicProfile(basic_profile).Id;


                getBasicProfile(basic_profile) is returning the actual BasicProfile__c object, when you really just need its Id. So, access it via Id.



                It makes more sense when you look at it this way:



                BasicProfile__c profile = getBasicProfile(basic_profile);

                objdlt.BasicProfile__c = profile.Id; // not profile, a reference to an object, not an id


                Maybe also rename getBasicProfile to createBasicProfile?






                share|improve this answer














                objdlt.BasicProfile__c = getBasicProfile(basic_profile).Id;


                getBasicProfile(basic_profile) is returning the actual BasicProfile__c object, when you really just need its Id. So, access it via Id.



                It makes more sense when you look at it this way:



                BasicProfile__c profile = getBasicProfile(basic_profile);

                objdlt.BasicProfile__c = profile.Id; // not profile, a reference to an object, not an id


                Maybe also rename getBasicProfile to createBasicProfile?







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 7 hours ago

























                answered 7 hours ago









                battery.cord

                6,67051744




                6,67051744






















                    padmanabh pande is a new contributor. Be nice, and check out our Code of Conduct.










                    draft saved

                    draft discarded


















                    padmanabh pande is a new contributor. Be nice, and check out our Code of Conduct.













                    padmanabh pande is a new contributor. Be nice, and check out our Code of Conduct.












                    padmanabh pande is a new contributor. Be nice, and check out our Code of Conduct.
















                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2f243897%2fhow-to-populate-a-lookup-field-in-custom-object-using-apex%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

                    Morgemoulin

                    Scott Moir

                    Souastre