Generic JavaScript Functions to Associate and Disassociate Dataverse records using the Power Pages Web API

In my previous post Creating and removing N:N relationship between Dataverse records using Javascript and Power Pages Web API I showed how we can leverage the Power Pages Web API to handle single N:N associate and disassociate requests for a custom N:N relationship between Accounts and Contacts.

In case we want to extend that to be more generic to any N:N relationship we can leverage a custom function that can be used in multiple pages easily.

Prerequisites

  • All required fields (including the N:N relationship name) in the code must be enabled for the WebAPI for the Power Pages site where you wish to run the code creation via Web API
  • The WebAPI Wrapper code must be added to the page you are using the JavaScript code
  • An N:N relationship between two tables you want to handle programmatically needs to be configured in Dataverse


Refactored Generic Versions of the functions

Following the principles explained in the previous post we can create more generic JavaScript functions that accept the target table names and relationships as parameters.

For both functions, instead of passing only two parameters, we will to pass a requestData object, which contains several properties:

  • recordTable: Specifies the plural internal name of the table for the main record (for example contacts)
  • recordId: Specifies the main record ID (example, use the contactid here)
  • relatedRecordTable: Specifies the plural internal name of the table to relate (for example accounts)
  • relatedRecordId: Specifies the record to relate ID (example, use the accountid here)
  • relationshipSchemaName: Specify your N:N relationship Schema name (for example prefix_Account_Contact_Contact)
  • onSuccess: Callback function to execute on success
  • onError: Callback function to execute on error


Associate Records:

This function associates two records by making a POST request using Power Pages Web API into a record with the related record data.

function AssociateRecords(requestData) { 
    var relatedRecord = {
        "@odata.id": `https://${location.host}/_api/${requestData.relatedRecordTable}(${requestData.relatedRecordId})`
    };
    webapi.safeAjax({
        type: "POST",
        contentType: "application/json",
        url: `/_api/${requestData.recordTable}(${requestData.recordId})/${requestData.relationshipSchemaName}/$ref`,
        data: JSON.stringify(relatedRecord),
        success: requestData.onSucess,
        error: requestData.onError
    });    
}

Disassociate Records:

This function disassociates two records by making a DELETE request using Power Pages Web API into a record’s relationship property.

function DisassociateRecords(requestData) {    
    webapi.safeAjax({
        type: "DELETE",
        contentType: "application/json",
        url: `/_api/${requestData.recordTable}(${requestData.recordId})/${requestData.relationshipSchemaName}(${requestData.relatedRecordId})/$ref`,
        success: requestData.onSucess,
        error: requestData.onError
    });
}

See sample usage below:

Associate with the generic function

AssociateRecords({
            recordId: contactId,
            recordTable: "contacts",
            relatedRecordId: accountId,
            relatedRecordTable: "accounts",
            relationshipSchemaName: "prefix_Account_Contact_Contact",
            onSucess: function(data, textStatus, xhr) { alert('Saved!') },
            onError: function(xhr, textStatus, errorThrown) { alert('Error happened!') }
})

Disassociate with the generic function:

AssociateRecords({
            recordId: contactId,
            recordTable: "contacts",
            relatedRecordId: accountId,
            relatedRecordTable: "accounts",
            relationshipSchemaName: "prefix_Account_Contact_Contact",
            onSucess: function(data, textStatus, xhr) { alert('Saved!') },
            onError: function(xhr, textStatus, errorThrown) { alert('Error happened!') }
})

Conclusion

By using this generic function we can easily leverage Power Pages Web API to Disassociate and Associate Records using JavaScript.

References

Associate and disassociate tables by using the Web API – Microsoft Learn

One comment

Leave a Reply

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