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
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:
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
The post Generic JavaScript Functions to Associate and Disassociate Dataverse records using the Power Pages Web API appeared first on michelcarlo.