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

                      Guess what letter conforming each word

                      Port of Spain

                      Run scheduled task as local user group (not BUILTIN)