How to populate a lookup field in custom object using Apex












2















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



























    2















    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

























      2












      2








      2








      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














      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













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 17 '18 at 19:16









      padmanabh pandepadmanabh pande

      111




      111






















          1 Answer
          1






          active

          oldest

          votes


















          5














          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',
            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%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









            5














            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






























              5














              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




























                5












                5








                5







                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 Dec 17 '18 at 19:19

























                answered Dec 17 '18 at 19:18









                battery.cordbattery.cord

                6,89551745




                6,89551745






























                    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%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

                    Le Mesnil-Réaume

                    Ida-Boy-Ed-Garten

                    web3.py web3.isConnected() returns false always