function onStartFunction executionContext let formContext executionCon

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
function onStartFunction(executionContext) {
let formContext = executionContext.getFormContext();
let accountData = formContext.getAttribute("cr7c1_account").getValue();
if (accountData != null && accountData.length > 0) {
let accountId = accountData[0].id;
accountId = accountId.replace(/{/g, "").replace(/}/g, "");
Xrm.WebApi.retrieveRecord("account", accountId, "?$select=name,cr7c1_primaryrelationshiptype").then(
function success(result) {
if (result.cr7c1_primaryrelationshiptype === 965930000) {
formContext.getControl("cr7c1_donglenumber").setVisible(true);
formContext.getAttribute("cr7c1_donglenumber").setRequiredLevel("required");
}
else {
formContext.getControl("cr7c1_donglenumber").setVisible(false);
formContext.getAttribute("cr7c1_donglenumber").setRequiredLevel("none");
}
},
function (error) {
console.log(error.message);
}
);
}
if (accountData === null) {
formContext.getControl("cr7c1_donglenumber").setVisible(false);
formContext.getAttribute("cr7c1_donglenumber").setRequiredLevel("none");
}
}
function checkSerialNumber(executionContext) {
let formContext = executionContext.getFormContext();
let serialNumberData = formContext.getAttribute("cr7c1_serialnumber").getValue();
console.log(serialNumberData);
let regExp = /[^0-9\-]/ig;
if (regExp.test(serialNumberData) || serialNumberData.length != 12) {
formContext.getControl("cr7c1_serialnumber").setNotification('Sorry, serial number must use only numbers, and length must be 12 numbers');
}
else {
Xrm.WebApi.retrieveMultipleRecords("cr7c1_scanner", "?$select=cr7c1_name&$filter=cr7c1_serialnumber eq '" + serialNumberData + "'").then(
function success(result) {
if (result.entities.length > 0) {
formContext.getControl("cr7c1_serialnumber").setNotification("Sorry, but this serial number is not unique")
console.log(result);
console.log(result.entities.length);
}
if (result.entities.length === 0) {
formContext.getControl("cr7c1_serialnumber").clearNotification();
}
},
function (error) {
formContext.getControl("cr7c1_serialnumber").clearNotification();
console.log(error.message);
}
);
}
}
function saveScanner(executionContext) {
let formContext = executionContext.getFormContext();
let serialNumberData = formContext.getAttribute("cr7c1_serialnumber").getValue();
let regExp = /[^0-9\-]/ig;
if (regExp.test(serialNumberData) || serialNumberData.length != 12 || serialNumberData === null) {
formContext.getEvenArgs().preventDefault();
}
else {
formContext.data.entity.save();
}
}
function accountsLookup(primaryControl) {
let formContext = primaryControl;
let customerScannerFilter = "<filter type='and'>" +
"<condition attribute='cr7c1_primaryrelationshiptype' operator='eq' value='965930000' />" +
"</filter >";
//let customerContactFilter = "<filter type='and'>" +
// "<condition attribute='parentcustomerid' operator='eq' value='" + accountId + "'/>" +
// "</filter >";
let lookupOptions = {
defaultEntityType: "request",
entityTypes: ["account"],
filters: [{
filterXml: customerScannerFilter,
entityLogicalName: "account"
}]
};
Xrm.Utility.lookupObjects(lookupOptions).then(
function (result) {
console.log(result);
console.log(result.entities);
if (result.entities != 'undefined') {
formContext.getAttribute("cr7c1_account").setValue(result);
}
},
function (error) {
console.log(error.message);
}
);
}