illegal assignment from object to id Error?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












I have written following code to insert records on junction object.



public static List<PDCN__c> addSelectedPDCNs(List<String> selectedRecords, String groupId) 
system.debug('Entering addSelectedPDCNs');
system.debug('Selected Records are-->'+selectedRecords);
system.debug('Group Id is-->'+groupId);

List < PDCN__c > lstAddId = [SELECT Id from PDCN__c where Name IN: selectedRecords];
system.debug('Record Ids Are-->'+lstAddId);

List<PDCNGrpJunc__c> lstJunc = new List<PDCNGrpJunc__c>();

for( PDCN__c pdcn : lstAddId )
system.debug('Inside For Loop-->'+pdcn);
PDCNGrpJunc__c juncRec = new PDCNGrpJunc__c();
juncRec.PDCN__c = pdcn;
juncRec.PDCN_Group__c = groupId;
lstJunc.add(juncRec);

//insert lstJunc;
try
system.debug('Trying Insert');
system.debug('');
insert lstJunc;

catch(Exception e)

System.debug('Exception Occured='+e.getMessage());


return lstAddId;



I am getting error in juncRec.PDCN__c = pdcn; line as :-




Illegal assignment from PDCN__c to Id




Why do I get this error? How do I fix it?







share|improve this question









New contributor




Rohit Mishra 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 have written following code to insert records on junction object.



    public static List<PDCN__c> addSelectedPDCNs(List<String> selectedRecords, String groupId) 
    system.debug('Entering addSelectedPDCNs');
    system.debug('Selected Records are-->'+selectedRecords);
    system.debug('Group Id is-->'+groupId);

    List < PDCN__c > lstAddId = [SELECT Id from PDCN__c where Name IN: selectedRecords];
    system.debug('Record Ids Are-->'+lstAddId);

    List<PDCNGrpJunc__c> lstJunc = new List<PDCNGrpJunc__c>();

    for( PDCN__c pdcn : lstAddId )
    system.debug('Inside For Loop-->'+pdcn);
    PDCNGrpJunc__c juncRec = new PDCNGrpJunc__c();
    juncRec.PDCN__c = pdcn;
    juncRec.PDCN_Group__c = groupId;
    lstJunc.add(juncRec);

    //insert lstJunc;
    try
    system.debug('Trying Insert');
    system.debug('');
    insert lstJunc;

    catch(Exception e)

    System.debug('Exception Occured='+e.getMessage());


    return lstAddId;



    I am getting error in juncRec.PDCN__c = pdcn; line as :-




    Illegal assignment from PDCN__c to Id




    Why do I get this error? How do I fix it?







    share|improve this question









    New contributor




    Rohit Mishra 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 have written following code to insert records on junction object.



      public static List<PDCN__c> addSelectedPDCNs(List<String> selectedRecords, String groupId) 
      system.debug('Entering addSelectedPDCNs');
      system.debug('Selected Records are-->'+selectedRecords);
      system.debug('Group Id is-->'+groupId);

      List < PDCN__c > lstAddId = [SELECT Id from PDCN__c where Name IN: selectedRecords];
      system.debug('Record Ids Are-->'+lstAddId);

      List<PDCNGrpJunc__c> lstJunc = new List<PDCNGrpJunc__c>();

      for( PDCN__c pdcn : lstAddId )
      system.debug('Inside For Loop-->'+pdcn);
      PDCNGrpJunc__c juncRec = new PDCNGrpJunc__c();
      juncRec.PDCN__c = pdcn;
      juncRec.PDCN_Group__c = groupId;
      lstJunc.add(juncRec);

      //insert lstJunc;
      try
      system.debug('Trying Insert');
      system.debug('');
      insert lstJunc;

      catch(Exception e)

      System.debug('Exception Occured='+e.getMessage());


      return lstAddId;



      I am getting error in juncRec.PDCN__c = pdcn; line as :-




      Illegal assignment from PDCN__c to Id




      Why do I get this error? How do I fix it?







      share|improve this question









      New contributor




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










      I have written following code to insert records on junction object.



      public static List<PDCN__c> addSelectedPDCNs(List<String> selectedRecords, String groupId) 
      system.debug('Entering addSelectedPDCNs');
      system.debug('Selected Records are-->'+selectedRecords);
      system.debug('Group Id is-->'+groupId);

      List < PDCN__c > lstAddId = [SELECT Id from PDCN__c where Name IN: selectedRecords];
      system.debug('Record Ids Are-->'+lstAddId);

      List<PDCNGrpJunc__c> lstJunc = new List<PDCNGrpJunc__c>();

      for( PDCN__c pdcn : lstAddId )
      system.debug('Inside For Loop-->'+pdcn);
      PDCNGrpJunc__c juncRec = new PDCNGrpJunc__c();
      juncRec.PDCN__c = pdcn;
      juncRec.PDCN_Group__c = groupId;
      lstJunc.add(juncRec);

      //insert lstJunc;
      try
      system.debug('Trying Insert');
      system.debug('');
      insert lstJunc;

      catch(Exception e)

      System.debug('Exception Occured='+e.getMessage());


      return lstAddId;



      I am getting error in juncRec.PDCN__c = pdcn; line as :-




      Illegal assignment from PDCN__c to Id




      Why do I get this error? How do I fix it?









      share|improve this question









      New contributor




      Rohit Mishra 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








      edited Sep 7 at 14:24









      Adrian Larson♦

      100k19106224




      100k19106224






      New contributor




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









      asked Sep 7 at 11:56









      Rohit Mishra

      111




      111




      New contributor




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





      New contributor





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






      Rohit Mishra 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
          5
          down vote













          So the field juncRec.PDCN__c is a Lookup field, (Consider it as a text/ID field)



          where as in your loop pdcn is an object/record.(Not a text field)



          In your code



          juncRec.PDCN__c = pdcn;


          What you are trying to do is assign an Object to a Text/Id field and hence you get an error. You can solve this by using id juncRec.PDCN__c = pdcn.Id;



          for( PDCN__c pdcn : lstAddId ) 
          system.debug('Inside For Loop-->'+pdcn);
          PDCNGrpJunc__c juncRec = new PDCNGrpJunc__c();
          ***juncRec.PDCN__c = pdcn.Id;***
          juncRec.PDCN_Group__c = groupId;
          lstJunc.add(juncRec);






          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: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );






            Rohit Mishra 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%2f231606%2fillegal-assignment-from-object-to-id-error%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            5
            down vote













            So the field juncRec.PDCN__c is a Lookup field, (Consider it as a text/ID field)



            where as in your loop pdcn is an object/record.(Not a text field)



            In your code



            juncRec.PDCN__c = pdcn;


            What you are trying to do is assign an Object to a Text/Id field and hence you get an error. You can solve this by using id juncRec.PDCN__c = pdcn.Id;



            for( PDCN__c pdcn : lstAddId ) 
            system.debug('Inside For Loop-->'+pdcn);
            PDCNGrpJunc__c juncRec = new PDCNGrpJunc__c();
            ***juncRec.PDCN__c = pdcn.Id;***
            juncRec.PDCN_Group__c = groupId;
            lstJunc.add(juncRec);






            share|improve this answer
























              up vote
              5
              down vote













              So the field juncRec.PDCN__c is a Lookup field, (Consider it as a text/ID field)



              where as in your loop pdcn is an object/record.(Not a text field)



              In your code



              juncRec.PDCN__c = pdcn;


              What you are trying to do is assign an Object to a Text/Id field and hence you get an error. You can solve this by using id juncRec.PDCN__c = pdcn.Id;



              for( PDCN__c pdcn : lstAddId ) 
              system.debug('Inside For Loop-->'+pdcn);
              PDCNGrpJunc__c juncRec = new PDCNGrpJunc__c();
              ***juncRec.PDCN__c = pdcn.Id;***
              juncRec.PDCN_Group__c = groupId;
              lstJunc.add(juncRec);






              share|improve this answer






















                up vote
                5
                down vote










                up vote
                5
                down vote









                So the field juncRec.PDCN__c is a Lookup field, (Consider it as a text/ID field)



                where as in your loop pdcn is an object/record.(Not a text field)



                In your code



                juncRec.PDCN__c = pdcn;


                What you are trying to do is assign an Object to a Text/Id field and hence you get an error. You can solve this by using id juncRec.PDCN__c = pdcn.Id;



                for( PDCN__c pdcn : lstAddId ) 
                system.debug('Inside For Loop-->'+pdcn);
                PDCNGrpJunc__c juncRec = new PDCNGrpJunc__c();
                ***juncRec.PDCN__c = pdcn.Id;***
                juncRec.PDCN_Group__c = groupId;
                lstJunc.add(juncRec);






                share|improve this answer












                So the field juncRec.PDCN__c is a Lookup field, (Consider it as a text/ID field)



                where as in your loop pdcn is an object/record.(Not a text field)



                In your code



                juncRec.PDCN__c = pdcn;


                What you are trying to do is assign an Object to a Text/Id field and hence you get an error. You can solve this by using id juncRec.PDCN__c = pdcn.Id;



                for( PDCN__c pdcn : lstAddId ) 
                system.debug('Inside For Loop-->'+pdcn);
                PDCNGrpJunc__c juncRec = new PDCNGrpJunc__c();
                ***juncRec.PDCN__c = pdcn.Id;***
                juncRec.PDCN_Group__c = groupId;
                lstJunc.add(juncRec);







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Sep 7 at 12:04









                Pranay Jaiswal

                8,73431848




                8,73431848




















                    Rohit Mishra is a new contributor. Be nice, and check out our Code of Conduct.









                     

                    draft saved


                    draft discarded


















                    Rohit Mishra is a new contributor. Be nice, and check out our Code of Conduct.












                    Rohit Mishra is a new contributor. Be nice, and check out our Code of Conduct.











                    Rohit Mishra 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%2f231606%2fillegal-assignment-from-object-to-id-error%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    What does second last employer means? [closed]

                    List of Gilmore Girls characters

                    Confectionery