function assignUserOnOpen(executionContext) {
const formContext = executionContext.getFormContext();
var dcar_contract = formContext.getAttribute('dcar_contract').getValue();
var caseOwnerIdval = formContext.getAttribute('ownerid').getValue();
if (dcar_contract === 1) {
if (caseOwnerIdval[0].name === "# B2B Connector for Dynamics") {
var loggedInUserId = Xrm.Utility.getGlobalContext().userSettings.userId;
var loggedInUsername = Xrm.Utility.getGlobalContext().userSettings.userName;
// Create an EntityReference for the new owner
var newOwnerRef = [{
id: loggedInUserId,
name: loggedInUsername,
entityType: "systemuser"
}];
// Set the 'ownerid' attribute value
formContext.getAttribute('ownerid').setValue(newOwnerRef);
// Fire the 'ownerid' attribute's onchange event
formContext.getAttribute('ownerid').fireOnChange();
formContext.data.entity.save("save");
}
}
}
function assignUserOnClose() {
var caseId = Xrm.Page.data.entity.getId();
var data = {
"ownerid@odata.bind": "/systemusers(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)"
};
Xrm.WebApi.updateRecord("incident", caseId, data).then(
function success(result) {
console.log("Assign # B2B Connector for Dynamics to case when done");
},
function error(error) {
console.error("Error while closing case and assign # B2B Connector for Dynamics:", error.message);
}
);
}