Set Values of all Data Types using Web API in Dynamics CRM

By | February 10, 2016

In CRM 2016, Microsoft Introduced a new concept called “Web API” (OData v4) to perform CRUD operations as well as other special messages supported in Dynamics CRM. Stepping towards the new enhancement OData v2 is deprecated from the CRM 2016.

Though WEB API too requires the object to be sent in JSON form, it differs a little in the way the values for fields that support complex data type are set. The aim of this blog was to take up most of the common data types used and provide a sample code around how the values need to be set of each of these

Sample code to create an Account Entity Record with all Data Types

Sample code to create an Activity & set Activity Party

Conclusion:

Lookup, Regarding and Activity parties being special complex field need to be set by providing a reference to the entity type that you intend to set. The field name also includes a reference to the entity as can be seen in the sample code above. This differs from the way it used to be done earlier where entity type used to be provided as a property of the Lookup object instead of in the field name itself.

Accounting inside your Dynamics CRM..try today. Dynamics CRM and QuickBooks Integration.

2 thoughts on “Set Values of all Data Types using Web API in Dynamics CRM

  1. jedom

    I’m facing an error using your code to populate a lookup, on a custom entity


    JSONAcc["new_qualifiedleadid@odata.bind"] = “/opportunities(“+ QualLeadId + “)”; //Currency

    //url for ajax request to create account
    uri = Xrm.Page.context.getClientUrl() + “/api/data/v8.0/new_opportunities”;

    When I download the new_opportunities.json file, I can find a reference to this field:
    “_new_qualifiedleadid_value”

    Nevertheless, send of JSON request returns with following error :

    {
    “error”:{
    “code”:””,”message”:”An undeclared property ‘new_qualifiedleadid’ which only has property annotations in the payload but no property value was found in the payload. In OData, only declared navigation properties and declared named streams can be represented as properties without values.”,”innererror”:{
    “message”:”An undeclared property ‘new_qualifiedleadid’ which only has property annotations in the payload but no property value was found in the payload. In OData, only declared navigation properties and declared named streams can be represented as properties without values.”,”type”:”Microsoft.Crm.CrmHttpException”,”stacktrace”:” at Microsoft.Crm.Extensibility.OData.CrmODataUtilities.ValidateInputParameters(ModelStateDictionary controllerModelState)\r\n at Microsoft.Crm.Extensibility.OData.EntityController.PostEntitySet(String entitySetName, EdmEntityObject entityObject)\r\n at lambda_method(Closure , Object , Object[] )\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.c__DisplayClass10.b__9(Object instance, Object[] methodParameters)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()\r\n— End of stack trace from previous location where exception was thrown —\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()\r\n at System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()”
    }
    }
    }

    Do you have any idea to solve this issue ?
    Thanks.

    1. inogic Post author

      Hi,

      Please follow the steps listed below :

      Step 1 : Goto Cutomization  Developer Resource.

      Step 2 : Click to “Download Odata Metadata” link and Download the same.

      Step 3 : Once Download, open it and find out name of lookup attribute ( i.e. “new_qualifiedleadid”) and check its casing.

      Step 4 : Verify it with the value which you are setting in the code it should be same.

      Hope it helps!

      Thanks

Leave a Reply