How to update or create N:N relationship using the Client API

I'm writing some client-side customisations for a model driven app and I need to update an entry in an N:N relationship. I have been following the documentation here. I can read from the relationship table, but trying to update an entry or create a new entry is failing.

The N:N relationship is between a custom entity new_lastask and the built-in systemuser entity. The name generated for the N:N relationship is new_new_lastask_systemuser. I have managed to create and update other records, and I understand that you need to use the Schema name with the exact casing, not the name of the field and also the @odata.bind syntax when updating a lookup field, but I can't figure out what the name of the field should be.

The example code below tries to find a N:N record with a given user and switch it for another user, I have given an example with the updateRecord method, but I have tried with createRecord too and I get the same error.

// taskId is the Guid of the custom task entity, and userId is the guid of the user 

var query = "?$filter=new_lastaskid eq " + taskId;

Xrm.WebApi.retrieveMultipleRecords("new_new_lastask_systemuser", query).then(
    function success(result) {
        for (var i = 0; i < result.entities.length; i++) {
            if (result.entities[i].systemuserid===oldUserId) {
                // found the offering user, replace them
                var data = {
                    "[email protected]": "/systemusers" + newUserId
                }

                // try to just change the value of the user attached to the N:N record
                Xrm.WebApi.updateRecord(tableName, result.entities[i].new_new_lastask_systemuserid, data).then(
                    function success(result) {
                        console.log("Successfully updated");
                        // perform operations on record update
                    },
                    function (error) {
                        console.log(error.message);
                        // An error occurred while validating input parameters: Microsoft.OData.ODataException: 
                        // An undeclared property 'systemuserid' which only has property annotations in the payload...
                    }
                );

            }
        }                    
    },
    function (error) {
        console.log(error.message);
        // handle error conditions
    }
);

This is the same error I was getting when trying to update any lookup field on my entity when I was trying to use the name instead of the Schema Name, so I thought it might be related to the capitalisation of the field, so I have tried many variations (SystemUserId, User, SystemUser, systemuser, user, userid) and nothing works.

What should the Schema name be of the lookup field on my N:N table, or am I going about handling modifications to these the wrong way via this API?

Upvotes: 1

Views: 2707

Answers (1)

I just could not get it to work via the Client API, so i just made the call to the web api using fetch, following the document from the 8.2 api here:

fetch('https://yoururl.crm.dynamics.com/api/data/v9.0/new_lastasks' + taskId + "/new_new_lastask_licensee/$ref", {
    method: 'post',
    headers: {
        "Content-Type": "application/json",
        "Accept": "application/json",
        "OData-MaxVersion": "4.0",
        "OData-Version": "4.0"
    },
    body: JSON.stringify(
        {"@odata.id": "https://yoururl.crm.dynamics.com/api/data/v9.0/systemusers" + currentUser }
    )
    }).then(function(response) { 
    console.log(response)
    })

Upvotes: 1

Related Questions