function lock executionContext let formContext executionContext getFor

  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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
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);
}
);
}