System.Collections.Generic.KeyNotFoundException Dynamics CRM C# lookup field error












1














I am trying to create a new record in Dynamics CRM using C#.



Below is the code that I am using:



 Entity Record = new Entity("new_edRecord");
Record["new_year"] = "2100";
Record["new_school"] = new EntityReference("new_school",new Guid("8ba53949-3947-e445-876c-001dd8b71c19"));
Record["new_gradelevel"] = "100000018";
organizationProxy.Create(Record);


When I execute this, I get the below error:




Unexpected exception from plug-in (Execute):
Department.EdRecord.RecordCreate:
System.Collections.Generic.KeyNotFoundException: The given key was not
present in the dictionary.




I double checked to see if a school exist with the GUID by visiting the below url:



https://crm.site.com/orgname/api/data/v8.0/new_schools(8ba53949-3947-e445-876c-001dd8b71c19)


and this brought a result. Not sure what I am missing here.










share|improve this question




















  • 1




    Have you set any breakpoints to see which line is failing in your example? The provided URL, uses "new_schools", and you are using "new_school"
    – Ryan Wilson
    Nov 13 at 18:35












  • Yeah, It fails if I add the new_school. it's a mandatory field so it needs to be specified. And the new_schools is used in the upper because CRM uses plural name for rest api
    – Vignesh Subramanian
    Nov 13 at 18:44










  • I would wager that "new_school" is the key which doesn't exist then or if it is a mandatory field, could it be that it is an identity column generated by the insert, and therefore you are not allowed to give it a value?
    – Ryan Wilson
    Nov 13 at 18:45












  • Could the problem be with your OptionSet values? You've set Record["new_year"] = "2100" but I'm assuming that should be "2010"? You should try to generate strong classes using the CrmSvcUtil tool
    – jasonscript
    Nov 14 at 1:01










  • Is the code in your question part of the Department.EdRecord.RecordCreate plugin?
    – James Wood
    Nov 14 at 11:38
















1














I am trying to create a new record in Dynamics CRM using C#.



Below is the code that I am using:



 Entity Record = new Entity("new_edRecord");
Record["new_year"] = "2100";
Record["new_school"] = new EntityReference("new_school",new Guid("8ba53949-3947-e445-876c-001dd8b71c19"));
Record["new_gradelevel"] = "100000018";
organizationProxy.Create(Record);


When I execute this, I get the below error:




Unexpected exception from plug-in (Execute):
Department.EdRecord.RecordCreate:
System.Collections.Generic.KeyNotFoundException: The given key was not
present in the dictionary.




I double checked to see if a school exist with the GUID by visiting the below url:



https://crm.site.com/orgname/api/data/v8.0/new_schools(8ba53949-3947-e445-876c-001dd8b71c19)


and this brought a result. Not sure what I am missing here.










share|improve this question




















  • 1




    Have you set any breakpoints to see which line is failing in your example? The provided URL, uses "new_schools", and you are using "new_school"
    – Ryan Wilson
    Nov 13 at 18:35












  • Yeah, It fails if I add the new_school. it's a mandatory field so it needs to be specified. And the new_schools is used in the upper because CRM uses plural name for rest api
    – Vignesh Subramanian
    Nov 13 at 18:44










  • I would wager that "new_school" is the key which doesn't exist then or if it is a mandatory field, could it be that it is an identity column generated by the insert, and therefore you are not allowed to give it a value?
    – Ryan Wilson
    Nov 13 at 18:45












  • Could the problem be with your OptionSet values? You've set Record["new_year"] = "2100" but I'm assuming that should be "2010"? You should try to generate strong classes using the CrmSvcUtil tool
    – jasonscript
    Nov 14 at 1:01










  • Is the code in your question part of the Department.EdRecord.RecordCreate plugin?
    – James Wood
    Nov 14 at 11:38














1












1








1







I am trying to create a new record in Dynamics CRM using C#.



Below is the code that I am using:



 Entity Record = new Entity("new_edRecord");
Record["new_year"] = "2100";
Record["new_school"] = new EntityReference("new_school",new Guid("8ba53949-3947-e445-876c-001dd8b71c19"));
Record["new_gradelevel"] = "100000018";
organizationProxy.Create(Record);


When I execute this, I get the below error:




Unexpected exception from plug-in (Execute):
Department.EdRecord.RecordCreate:
System.Collections.Generic.KeyNotFoundException: The given key was not
present in the dictionary.




I double checked to see if a school exist with the GUID by visiting the below url:



https://crm.site.com/orgname/api/data/v8.0/new_schools(8ba53949-3947-e445-876c-001dd8b71c19)


and this brought a result. Not sure what I am missing here.










share|improve this question















I am trying to create a new record in Dynamics CRM using C#.



Below is the code that I am using:



 Entity Record = new Entity("new_edRecord");
Record["new_year"] = "2100";
Record["new_school"] = new EntityReference("new_school",new Guid("8ba53949-3947-e445-876c-001dd8b71c19"));
Record["new_gradelevel"] = "100000018";
organizationProxy.Create(Record);


When I execute this, I get the below error:




Unexpected exception from plug-in (Execute):
Department.EdRecord.RecordCreate:
System.Collections.Generic.KeyNotFoundException: The given key was not
present in the dictionary.




I double checked to see if a school exist with the GUID by visiting the below url:



https://crm.site.com/orgname/api/data/v8.0/new_schools(8ba53949-3947-e445-876c-001dd8b71c19)


and this brought a result. Not sure what I am missing here.







c# dynamics-crm crm dynamics-crm-2015






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 at 6:11









Arun Vinoth

9,016132447




9,016132447










asked Nov 13 at 18:32









Vignesh Subramanian

3,65954197




3,65954197








  • 1




    Have you set any breakpoints to see which line is failing in your example? The provided URL, uses "new_schools", and you are using "new_school"
    – Ryan Wilson
    Nov 13 at 18:35












  • Yeah, It fails if I add the new_school. it's a mandatory field so it needs to be specified. And the new_schools is used in the upper because CRM uses plural name for rest api
    – Vignesh Subramanian
    Nov 13 at 18:44










  • I would wager that "new_school" is the key which doesn't exist then or if it is a mandatory field, could it be that it is an identity column generated by the insert, and therefore you are not allowed to give it a value?
    – Ryan Wilson
    Nov 13 at 18:45












  • Could the problem be with your OptionSet values? You've set Record["new_year"] = "2100" but I'm assuming that should be "2010"? You should try to generate strong classes using the CrmSvcUtil tool
    – jasonscript
    Nov 14 at 1:01










  • Is the code in your question part of the Department.EdRecord.RecordCreate plugin?
    – James Wood
    Nov 14 at 11:38














  • 1




    Have you set any breakpoints to see which line is failing in your example? The provided URL, uses "new_schools", and you are using "new_school"
    – Ryan Wilson
    Nov 13 at 18:35












  • Yeah, It fails if I add the new_school. it's a mandatory field so it needs to be specified. And the new_schools is used in the upper because CRM uses plural name for rest api
    – Vignesh Subramanian
    Nov 13 at 18:44










  • I would wager that "new_school" is the key which doesn't exist then or if it is a mandatory field, could it be that it is an identity column generated by the insert, and therefore you are not allowed to give it a value?
    – Ryan Wilson
    Nov 13 at 18:45












  • Could the problem be with your OptionSet values? You've set Record["new_year"] = "2100" but I'm assuming that should be "2010"? You should try to generate strong classes using the CrmSvcUtil tool
    – jasonscript
    Nov 14 at 1:01










  • Is the code in your question part of the Department.EdRecord.RecordCreate plugin?
    – James Wood
    Nov 14 at 11:38








1




1




Have you set any breakpoints to see which line is failing in your example? The provided URL, uses "new_schools", and you are using "new_school"
– Ryan Wilson
Nov 13 at 18:35






Have you set any breakpoints to see which line is failing in your example? The provided URL, uses "new_schools", and you are using "new_school"
– Ryan Wilson
Nov 13 at 18:35














Yeah, It fails if I add the new_school. it's a mandatory field so it needs to be specified. And the new_schools is used in the upper because CRM uses plural name for rest api
– Vignesh Subramanian
Nov 13 at 18:44




Yeah, It fails if I add the new_school. it's a mandatory field so it needs to be specified. And the new_schools is used in the upper because CRM uses plural name for rest api
– Vignesh Subramanian
Nov 13 at 18:44












I would wager that "new_school" is the key which doesn't exist then or if it is a mandatory field, could it be that it is an identity column generated by the insert, and therefore you are not allowed to give it a value?
– Ryan Wilson
Nov 13 at 18:45






I would wager that "new_school" is the key which doesn't exist then or if it is a mandatory field, could it be that it is an identity column generated by the insert, and therefore you are not allowed to give it a value?
– Ryan Wilson
Nov 13 at 18:45














Could the problem be with your OptionSet values? You've set Record["new_year"] = "2100" but I'm assuming that should be "2010"? You should try to generate strong classes using the CrmSvcUtil tool
– jasonscript
Nov 14 at 1:01




Could the problem be with your OptionSet values? You've set Record["new_year"] = "2100" but I'm assuming that should be "2010"? You should try to generate strong classes using the CrmSvcUtil tool
– jasonscript
Nov 14 at 1:01












Is the code in your question part of the Department.EdRecord.RecordCreate plugin?
– James Wood
Nov 14 at 11:38




Is the code in your question part of the Department.EdRecord.RecordCreate plugin?
– James Wood
Nov 14 at 11:38












3 Answers
3






active

oldest

votes


















3














If new_gradelevel is an Optionset type of field, following should be the code-:



Record["new_gradelevel"] = new OptionSetValue(100000018);


Also, can you confirm the data type of new_year is String.



Hope it helps.






share|improve this answer































    2














    It is a common convention to name CRM lookup fields as the entity name with id appended.



    Is it possible that Record["new_school"] should be Record["new_schoolid"]?



    OR:



    Perhaps the value 100000018 does not exist in the Record["new_gradelevel"] optionset. Maybe comment out that line and see what happens.






    share|improve this answer























    • yeah I tried that, but then it throwed some error like new_schoolid is not found
      – Vignesh Subramanian
      Nov 14 at 3:11










    • OK, I added another possibility to the answer.
      – Aron
      Nov 14 at 12:20



















    1














    Like other answers suggested, verify all the field names/schema names for its correctness.



    You can do trial & error by commenting out the fields one by one and execute this code to find out the buggy line.



    Finally, (this will be my first troubleshooting step) cross check if you have any other plugins on post create of new_edrecord entity or associate of new_school entity. That synchronous plugin may throw this KeyNotFoundExceptionerror but bubble here.






    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      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: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      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%2fstackoverflow.com%2fquestions%2f53287427%2fsystem-collections-generic-keynotfoundexception-dynamics-crm-c-sharp-lookup-fiel%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      If new_gradelevel is an Optionset type of field, following should be the code-:



      Record["new_gradelevel"] = new OptionSetValue(100000018);


      Also, can you confirm the data type of new_year is String.



      Hope it helps.






      share|improve this answer




























        3














        If new_gradelevel is an Optionset type of field, following should be the code-:



        Record["new_gradelevel"] = new OptionSetValue(100000018);


        Also, can you confirm the data type of new_year is String.



        Hope it helps.






        share|improve this answer


























          3












          3








          3






          If new_gradelevel is an Optionset type of field, following should be the code-:



          Record["new_gradelevel"] = new OptionSetValue(100000018);


          Also, can you confirm the data type of new_year is String.



          Hope it helps.






          share|improve this answer














          If new_gradelevel is an Optionset type of field, following should be the code-:



          Record["new_gradelevel"] = new OptionSetValue(100000018);


          Also, can you confirm the data type of new_year is String.



          Hope it helps.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 17 at 5:31









          Arun Vinoth

          9,016132447




          9,016132447










          answered Nov 15 at 12:49









          Aakarsh Dhawan

          1196




          1196

























              2














              It is a common convention to name CRM lookup fields as the entity name with id appended.



              Is it possible that Record["new_school"] should be Record["new_schoolid"]?



              OR:



              Perhaps the value 100000018 does not exist in the Record["new_gradelevel"] optionset. Maybe comment out that line and see what happens.






              share|improve this answer























              • yeah I tried that, but then it throwed some error like new_schoolid is not found
                – Vignesh Subramanian
                Nov 14 at 3:11










              • OK, I added another possibility to the answer.
                – Aron
                Nov 14 at 12:20
















              2














              It is a common convention to name CRM lookup fields as the entity name with id appended.



              Is it possible that Record["new_school"] should be Record["new_schoolid"]?



              OR:



              Perhaps the value 100000018 does not exist in the Record["new_gradelevel"] optionset. Maybe comment out that line and see what happens.






              share|improve this answer























              • yeah I tried that, but then it throwed some error like new_schoolid is not found
                – Vignesh Subramanian
                Nov 14 at 3:11










              • OK, I added another possibility to the answer.
                – Aron
                Nov 14 at 12:20














              2












              2








              2






              It is a common convention to name CRM lookup fields as the entity name with id appended.



              Is it possible that Record["new_school"] should be Record["new_schoolid"]?



              OR:



              Perhaps the value 100000018 does not exist in the Record["new_gradelevel"] optionset. Maybe comment out that line and see what happens.






              share|improve this answer














              It is a common convention to name CRM lookup fields as the entity name with id appended.



              Is it possible that Record["new_school"] should be Record["new_schoolid"]?



              OR:



              Perhaps the value 100000018 does not exist in the Record["new_gradelevel"] optionset. Maybe comment out that line and see what happens.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 14 at 12:19

























              answered Nov 13 at 21:48









              Aron

              1,9152917




              1,9152917












              • yeah I tried that, but then it throwed some error like new_schoolid is not found
                – Vignesh Subramanian
                Nov 14 at 3:11










              • OK, I added another possibility to the answer.
                – Aron
                Nov 14 at 12:20


















              • yeah I tried that, but then it throwed some error like new_schoolid is not found
                – Vignesh Subramanian
                Nov 14 at 3:11










              • OK, I added another possibility to the answer.
                – Aron
                Nov 14 at 12:20
















              yeah I tried that, but then it throwed some error like new_schoolid is not found
              – Vignesh Subramanian
              Nov 14 at 3:11




              yeah I tried that, but then it throwed some error like new_schoolid is not found
              – Vignesh Subramanian
              Nov 14 at 3:11












              OK, I added another possibility to the answer.
              – Aron
              Nov 14 at 12:20




              OK, I added another possibility to the answer.
              – Aron
              Nov 14 at 12:20











              1














              Like other answers suggested, verify all the field names/schema names for its correctness.



              You can do trial & error by commenting out the fields one by one and execute this code to find out the buggy line.



              Finally, (this will be my first troubleshooting step) cross check if you have any other plugins on post create of new_edrecord entity or associate of new_school entity. That synchronous plugin may throw this KeyNotFoundExceptionerror but bubble here.






              share|improve this answer


























                1














                Like other answers suggested, verify all the field names/schema names for its correctness.



                You can do trial & error by commenting out the fields one by one and execute this code to find out the buggy line.



                Finally, (this will be my first troubleshooting step) cross check if you have any other plugins on post create of new_edrecord entity or associate of new_school entity. That synchronous plugin may throw this KeyNotFoundExceptionerror but bubble here.






                share|improve this answer
























                  1












                  1








                  1






                  Like other answers suggested, verify all the field names/schema names for its correctness.



                  You can do trial & error by commenting out the fields one by one and execute this code to find out the buggy line.



                  Finally, (this will be my first troubleshooting step) cross check if you have any other plugins on post create of new_edrecord entity or associate of new_school entity. That synchronous plugin may throw this KeyNotFoundExceptionerror but bubble here.






                  share|improve this answer












                  Like other answers suggested, verify all the field names/schema names for its correctness.



                  You can do trial & error by commenting out the fields one by one and execute this code to find out the buggy line.



                  Finally, (this will be my first troubleshooting step) cross check if you have any other plugins on post create of new_edrecord entity or associate of new_school entity. That synchronous plugin may throw this KeyNotFoundExceptionerror but bubble here.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 17 at 5:41









                  Arun Vinoth

                  9,016132447




                  9,016132447






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • 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%2fstackoverflow.com%2fquestions%2f53287427%2fsystem-collections-generic-keynotfoundexception-dynamics-crm-c-sharp-lookup-fiel%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

                      鏡平學校

                      ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

                      Why https connections are so slow when debugging (stepping over) in Java?