Tip #787: Creating records in Project Service

Dictionary keyWhen creating records for one of the Dynamics 365 solutions, you may find yourself in the same situation as Guido “Trekkie not Star Wars fan” Preite. The simplest possible code to create msdyn_actual (part of the Project Service)

Entity foobar = new Entity("msdyn_actual");
foobar["msdyn_description"] = "MY TEST";
service.Create(foobar);

generates “The given key was not present in the dictionary” error.

One of the advantages (and curses) of coding CRUD operations is that any attribute can be ignored even if marked as business required. However, most of the complex solutions include their own plugins that reasonably expect mandatory attributes to be present – hence the error (as generated by one of the Project Services plugins).

Solution is to use metadata browser (either from XrmToolbox or SDK) to figure out required attributes. In the case of msdyn_actual entity they are:

  • msdyn_documentdate
  • msdyn_startdatetime
  • msdyn_enddatetime
  • msdyn_transactiontypecode
  • msdyn_transactionclassification

(Interestingly enough, msdyn_description, primary name, is not required)

Entity foobar = new Entity("msdyn_actual");
foobar["msdyn_description"] = "MY TEST";

foobar["msdyn_transactiontypecode"] = 
   new OptionSetValue(192350000); // Cost

foobar["msdyn_transactionclassification"] = 
   new OptionSetValue(192350000); // Time

service.Create(foobar);

We did not use all required attributes but looks like only the type code and classification are explicitly expected and used by the built-in plugins.

Tweet about this on TwitterShare on Facebook3Share on Google+2

Leave a Reply

Your email address will not be published. Required fields are marked *