function lock(executionContext) {
let formContext = executionContext.getFormContext();
formContext.getControl("cr7c1_category").setDisabled(true);
formContext.getControl("cr7c1_requestissue").setDisabled(true);
formContext.getControl("cr7c1_subcategory").setDisabled(true);
}
function blockFields(executionContext, mainField, dependendField) {
let formContext = executionContext.getFormContext();
let mainFieldValue = formContext.getAttribute(mainField).getValue();
if (mainFieldValue != null) {
formContext.getControl(dependendField).setDisabled(false);
formContext.getAttribute(dependendField).setRequiredLevel("required");
}
else {
formContext.getControl(dependendField).setDisabled(true);
formContext.getAttribute(dependendField).setValue();
formContext.getAttribute(dependendField).setRequiredLevel("recommended");
formContext.getAttribute(dependendField).fireOnChange()
}
if (dependendField === "cr7c1_subcategory") {
formContext.getAttribute(dependendField).setRequiredLevel("none")
formContext.getAttribute(dependendField).setValue();
}
}
function accountField(executionContext) {
console.log("function started");
let formContext = executionContext.getFormContext();
let accountValue = formContext.getAttribute("cr7c1_account").getValue();
let contactValue = formContext.getAttribute("cr7c1_contact").getValue();
if (accountValue === null && contactValue != null) {
formContext.getAttribute("cr7c1_contact").setValue();
}
if(accountValue != null && accountValue.length > 0) {
let accountId = accountValue[0].id;
accountId = accountId.replace(/{/g, "");
accountId = accountId.replace(/}/g, "");
Xrm.WebApi.retrieveMultipleRecords("contact", "?$select=fullname&$filter=_parentcustomerid_value eq " + accountId).then(
function success(result) {
// perform additional operations on retrieved records
let alertStrings = { text: "The Number of contacts for this account: " + result.entities.length };
let alertOptions = { height: 500, width: 550 };
Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);
},
function(error) {
console.log(error.message);
// handle error conditions
}
);
formContext.getControl("cr7c1_contact").addPreSearch(function () {
contactFilter(executionContext);
});
}
}
function contactFilter(executionContext) {
let formContext = executionContext.getFormContext();
let accountValue = formContext.getAttribute("cr7c1_account").getValue();
if (accountValue != null && accountValue.length > 0) {
let accountId = accountValue[0].id;
accountId = accountId.replace(/{/g, "").replace(/}/g, '');
let customerContactFilter = "<filter type='and'>" +
"<condition attribute='parentcustomerid' operator='eq' value='" + accountId + "'/>" +
"</filter >";
formContext.getControl("cr7c1_contact").addCustomFilter(customerContactFilter, "contact");
}
}
function axQuoteNumberWriteToContact(executionContext) {
let formContext = executionContext.getFormContext();
let axQuoteNumberValue = formContext.getAttribute("cr7c1_axquotenumber").getValue();
let contactValue = formContext.getAttribute("cr7c1_contact").getValue();
if(axQuoteNumberValue != null && axQuoteNumberValue.length > 0) {
let contactId = contactValue[0].id;
contactId = contactId.replace(/{/g, "").replace(/}/g, '');
let data = {
"cr7c1_axquotenumber": axQuoteNumberValue
}
Xrm.WebApi.updateRecord("contact", contactId, data).then(
function success(result) {
console.log("Contact updated");
},
function(error) {
console.log(error.message);
}
);
}
}
function axQuoteField(executionContext) {
let formContext = executionContext.getFormContext();
let axQuoteNumberValue = formContext.getAttribute("cr7c1_axquotenumber").getValue();
let contactFieldValue = formContext.getAttribute("cr7c1_contact").getValue();
if (axQuoteNumberValue != null && axQuoteNumberValue.length > 0) {
Xrm.WebApi.retrieveMultipleRecords("contact", "?$select=fullname&$filter=cr7c1_axquotenumber eq '" + axQuoteNumberValue + "'").then(
function success(result) {
if(result.entities.length === 0) {
console.log("We don't have contact with this axQuoteNumber!");
formContext.getAttribute("cr7c1_contact").setValue();
}
if (result.entities.length > 0) {
let obj = {
entityType: "contact",
id: "{" + result.entities[0].contactid + "}",
name: result.entities[0].fullname
};
let array = [obj];
formContext.getAttribute("cr7c1_contact").setValue(array);
}
},
function(error) {
console.log(error.message);
}
);
};
}
function createContactAndAccountOneRequest(executionContext) {
let dataAccount = {
"name": "Test Account",
}
Xrm.WebApi.createRecord("account", dataAccount).then(
function success(result) {
console.log(result);
console.log("Account created with ID " + result.id);
let accountId = result.id;
let dataContact = {
"firstname": "Test",
"lastname": "Smith",
"cr7c1_axquotenumber": "something",
"parentcustomerid_account@odata.bind": "/accounts(" + accountId + ")",
}
console.log(accountId);
Xrm.WebApi.createRecord("contact", dataContact).then(
function success(result) {
console.log(result);
console.log("Contact created with ID: " + result.id);
let entityFormOptions = {};
entityFormOptions["entityName"] = "contact";
entityFormOptions["entityId"] = result.id;
Xrm.Navigation.openForm(entityFormOptions).then(
function (success) {
console.log(success);
},
function (error) {
console.log(error);
});
},
function (error) {
console.log(error.message);
}
);
},
function (error) {
console.log(error.message);
}
);
}
function createContactAndAccountTwoRequests(primaryControl) {
let formContext = primaryControl;
let accountData = {
"name": "Test Account",
"primarycontactid":
{
"firstname": "Test",
"lastname": "Smith",
}
}
Xrm.WebApi.createRecord("account", accountData).then(
function success(result) {
console.log(result);
let accountId = result.id;
Xrm.WebApi.retrieveRecord("account", accountId, "?$select=name&$expand=primarycontactid($select=contactid)").then(
function success(result) {
let contactId = result.primarycontactid.contactid;
let contactPrimaryAccountData = {
"parentcustomerid_account@odata.bind": "/accounts(" + accountId + ")",
};
Xrm.WebApi.updateRecord("contact", contactId, contactPrimaryAccountData).then(
function success(result) {
console.log("Contact " + contactId + " is succesfully updated");
let entityFormOptions = {};
entityFormOptions["entityName"] = "contact";
entityFormOptions["entityId"] = contactId;
Xrm.Navigation.openForm(entityFormOptions).then(
function success(result) {
console.log(result);
},
function (error) {
console.log(error);
}
);
},
function (error) {
console.log(error.message);
}
);
},
function (error) {
console.log(error.message);
}
);
},
function (error) {
console.log(error.message)
}
);
}
function teamsLookup(primaryControl) {
let formContext = primaryControl;
let lookupOptions = {
defaultEntityType: "request",
entityTypes: ["team"]
};
Xrm.Utility.lookupObjects(lookupOptions).then(
function (result) {
console.log(result);
formContext.getAttribute("cr7c1_team").setValue(result);
},
function (error) {
console.log(error.message);
}
);
}