Search code examples
javascriptdynamics-crmodatamicrosoft-dynamicsdynamics-crm-webapi

Cannot set multi-select option field when creating record in Dynamics 365 XRM Web API


I am attempting to create a record using the Dynamics 365 Web API. The field trying to be set is a multi-select option and is supposed to take an array of integers. The value being passed in is an array of integers (2nd screenshot), and yet I am getting the following error:

An unexpected 'StartArray' node was found when reading from the JSON reader. A 'PrimitiveValue' node was expected.?

**enter image description here**

enter image description here

Below is the code being used, identifying the line causing trouble.

var data = {
            "title": "sample title",
            "new_f1": f1_value,
            "new_meetingdate": meeting_date,
            "new_trainingmodules": training_modules, // Error on this line
            "new_annualnoticedate": annual_notices,

        }  

        Xrm.WebApi.createRecord("incident", data).then(
            function success(result) {
                alert("Case record created.");
            },
            function (error) {
                alert("error.message);
                // handle error conditions
            }
        );


Solution

  • You need not to send Multi-select optionset attribute as an array in payload, just send it as comma separated string value.

    var training_modules = "100000001, 100000002";
    .
    .
    .
    
    "new_trainingmodules": training_modules,
    

    Reference

    Setting multi-select picklist values
    With the Web API, you set the values by passing a string containing comma separated number values as shown in the following example:

    Request

    POST [organization uri]/api/data/v9.0/contacts HTTP/1.1
    Accept: application/json
    Content-Type: application/json; charset=utf-8
    OData-MaxVersion: 4.0
    OData-Version: 4.0
    
    {
        "@odata.type": "Microsoft.Dynamics.CRM.contact",
        "firstname": "Wayne",
        "lastname": "Yarborough",
        "sample_outdooractivities": "1, 9"
    }