-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpportunity.js
More file actions
339 lines (259 loc) · 12.1 KB
/
Opportunity.js
File metadata and controls
339 lines (259 loc) · 12.1 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
if (typeof (RSA) == "undefined") {
RSA = { __namespace: true };
}
//ENUMS
formType = {
CREATE: 1,
UPDATE: 2,
READONLY: 3,
DISABLED: 4
}
//CONSTANTS
const EFFECTIVEDATE = "estimatedclosedate";
const STAGE_CALL_REASON = "Call Reason";
const STAGE_DUCKCREEK = "Duck Creek";
const DUCKCREEK_FLAG = "rsa_openduckcreek";
RSA.Opportunity = {
//onLoad function for this form
onLoad: function () {
this.filterCallSubReason();
var formtype = Xrm.Page.ui.getFormType();
if (formtype == formType.CREATE) {
this.setEffectiveDate();
try {
// Call a function in another library
RSA.Opportunity.getBrand();
// Xrm.Page.data.setFormDirty(false); // This code check if there are changes on form
}
catch (e) {
Xrm.Page.ui.setFormNotification("Error onLoad" + Date().toString() + " RSA.Opportunity.OnLoad(): " + e.message, "ERROR", "RSA.Opportunity.OnLoad()");
}
}
else if (formtype == formType.UPDATE) {
}
else if (formtype == formType.READONLY) {
}
else if (formtype == formType.DISABLED) {
}
var activeProcess = Xrm.Page.data.process.getActiveProcess();
if (activeProcess != null) {
Xrm.Page.data.process.removeOnStageChange(this.setDuckCreekFlag);
Xrm.Page.data.process.addOnStageChange(this.setDuckCreekFlag);
}
},
//onSave function for this form
onSave: function () {
var formtype = Xrm.Page.ui.getFormType();
if (formtype == formType.CREATE) {
// this.advanceBusinessProcess(); // move to next stage when record is created
try {
// Call a function in another library
RSA.Opportunity.getBrand();
// Xrm.Page.data.setFormDirty(false); // This code check if there are changes on form
}
catch (e) {
Xrm.Page.ui.setFormNotification("Error onLoad" + Date().toString() + " RSA.Opportunity.OnLoad(): " + e.message, "ERROR", "RSA.Opportunity.OnLoad()");
}
}
else if (formtype == formType.UPDATE) {
}
else if (formtype == formType.READONLY) {
}
else if (formtype == formType.DISABLED) {
}
},
//clear call sub-reason on change of call reason
onChangeCallReason: function () {
Xrm.Page.getAttribute("rsa_callsubreasonid").setValue(null);
},
getBrand: function () {
try {
if (Xrm.Page.getAttribute("parentcontactid").getValue() != null) {
var id = Xrm.Page.getAttribute("parentcontactid").getValue()[0].id;
var contactId = id.slice(1, -1);
var serverUrl;
if (Xrm.Page.context.getClientUrl !== undefined) {
serverUrl = Xrm.Page.context.getClientUrl();
}
else {
serverUrl = Xrm.Page.context.getServerUrl();
}
var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
var contactRequest = new XMLHttpRequest();
var leadRequest = new XMLHttpRequest();
contactRequest.open("GET", ODataPath + "/ContactSet(guid'" + contactId + "')", false);
contactRequest.setRequestHeader("Accept", "application/json");
contactRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");
contactRequest.send();
//If contact request was successful
if (contactRequest.status == 200) {
var retrievedContact = JSON.parse(contactRequest.responseText).d;
var cBrand = retrievedContact.rsa_brand;
var value = cBrand.Name
var fieldName = "rsa_brandid"
// value cBrand
RSA.Opportunity.SetLookUp(fieldName, cBrand.LogicalName, cBrand.Id, value);
}
else {
console.log('Contact request failed.');
leadRequest.open("GET", ODataPath + "/LeadSet(guid'" + contactId + "')", false);
leadRequest.setRequestHeader("Accept", "application/json");
leadRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");
leadRequest.send();
//if lead request was succesfull
if (leadRequest.status == 200) {
var retrievedLead = JSON.parse(leadRequest.responseText).d;
var lBrand = retrievedLead.rsa_brandid;
var valuel = lBrand.Name;
var fieldName = "rsa_brandid";
var fieldName2 = "originatingleadid";
var value2 = retrievedLead.FullName;
// value cBrand
RSA.Opportunity.SetLookUp(fieldName, lBrand.LogicalName, lBrand.Id, valuel);
RSA.Opportunity.SetLookUp(fieldName2, "lead", retrievedLead.LeadId, value2);
}
else {
console.log('Lead request failed.');
}
}
// Xrm.Page.getAttribute("rsa_brandid").setSubmitMode("always");
//Xrm.Page.getAttribute("originatingleadid").setSubmitMode("always");
}
}
catch (e) {
Xrm.Page.ui.setFormNotification("Error" + Date().toString() + " RSA.Opportunity.GetBrand(): " + e.message, "ERROR", "RSA.Opportunity.GetBrand");
}
},
SetLookUp: function (fieldName, fieldType, fieldId, value) {
try {
var object = new Array();
object[0] = new Object();
object[0].id = fieldId;
object[0].name = value;
object[0].entityType = fieldType;
Xrm.Page.getAttribute(fieldName).setValue(object);
}
catch (e) {
alert("Error in SetLookUp: fieldName = " + fieldName + " fieldType = " + fieldType + " fieldId = " + fieldId + " value = " + value + " error = " + e);
}
},
//Set flag if stage is moving from CALL REASON TO DUCK CREEK
setDuckCreekFlag: function () {
var activeStage = Xrm.Page.data.process.getActiveStage();
if (activeStage != null && activeStage.getName() == STAGE_DUCKCREEK) {
Xrm.Page.getAttribute(DUCKCREEK_FLAG).setValue(true);
Xrm.Page.data.entity.save();
// Trigger Duck Creek Tab when in USD
if (window.IsUSD == true) {
window.open("http://event/?eventname=RSAOpenDuckCreekQuoteForBPF");
}
}
else {
Xrm.Page.getAttribute(DUCKCREEK_FLAG).setValue(false);
Xrm.Page.data.entity.save();
if (activeStage != null && activeStage.getName() != STAGE_CALL_REASON) {
// Trigger Duck Creek Tab when in USD
if (window.IsUSD == true) {
window.open("http://event/?eventname=RSALoadWrapUpCallGuidenceForBPF");
}
}
}
},
//default the Effective Date to todays date
setEffectiveDate: function () {
Xrm.Page.getAttribute(EFFECTIVEDATE).setValue(new Date());
},
AdvanceBusinessProcessStage: function () {
var activeStage = Xrm.Page.data.process.getActiveStage();
var dirty = Xrm.Page.data.entity.getIsDirty();
if (activeStage != null && activeStage.getName() == STAGE_DUCKCREEK)
this.movefarward();
},
//this method moved the Business Process Flow to next stage
advanceBusinessProcess: function (activeStageName) {
// Xrm.Page.data.process.moveNext();
var activePathCollection = Xrm.Page.data.process.getActivePath();
//Enumerate the stages
activePathCollection.forEach(function (stage, n) {
var name = stage.getName();
//search for specific stage by name
if ((STAGE_DUCKCREEK == name) && (STAGE_DUCKCREEK != activeStageName)) {
//move to the new stage
Xrm.Page.data.process.setActiveStage(stage.getId(), function (result) {
var value = result;
}
);
}
});
},
movefarward: function () {
Xrm.Page.data.process.moveNext(this.callback);
},
retrieveCampaignCode: function () {
Xrm.Page.getAttribute("rsa_discountcode").setValue("");
var campaign = Xrm.Page.getAttribute("campaignid");
if (campaign != null && campaign.getValue() != null) {
var campaignlookup = Xrm.Page.getAttribute("campaignid").getValue()[0];
campaignid = campaignlookup.id.replace(/[{}]/g, "");
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/campaigns(" + campaignid + ")?$select=promotioncodename", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var result = JSON.parse(this.response);
var promotioncodename = result["promotioncodename"];
Xrm.Page.getAttribute("rsa_discountcode").setValue(promotioncodename);
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
}
},
// add filter condition to call sub reason
filterCallSubReason: function ()
{
//Check if the control exist on the form
if (Xrm.Page.getControl('header_process_rsa_callsubreasonid') != null) {
// add the event handler for PreSearch Event
Xrm.Page.getControl('header_process_rsa_callsubreasonid').addPreSearch(this.addFilter);
}
},
//set the filter condition for call sub reason
addFilter: function ()
{
var callReasonId = null;
var callReason = null;
var callReasonLookup;
var fetchQuery;
try {
//Check if control exist on form
if (Xrm.Page.getControl('header_process_rsa_callreasonid') != null
&& Xrm.Page.getControl('header_process_rsa_callreasonid').getAttribute().getValue() != null) {
//Get Account lookup value
callReasonLookup = Xrm.Page.getControl('header_process_rsa_callreasonid').getAttribute().getValue();
//Get the account id
callReason = callReasonLookup[0].id;
callReasonId = callReason.substring(1, 37);
}
//Build fetch
if (callReasonId != null || callReasonId != undefined) {
fetchQuery = "<filter type='and'>" +
"<condition attribute='statecode' operator='eq' value='0' />" +
"<condition attribute='rsa_callreasonid' operator='eq' value='" + callReasonId + "' />" +
"</filter>";
//add custom filter
Xrm.Page.getControl('header_process_rsa_callsubreasonid').addCustomFilter(fetchQuery);
}
} catch (e) {
Xrm.Utility.alertDialog('addFilter Error: ' + (e.description || e.message));
}
}
}