forked from JoseFuentesDevAp/CRMScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathap_crmscripts.js
More file actions
123 lines (83 loc) · 3.33 KB
/
ap_crmscripts.js
File metadata and controls
123 lines (83 loc) · 3.33 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
async function createLogAccountOnSave(executionContext) {
const formContext = executionContext.getFormContext();
const entityname = formContext.data.entity.getEntityName();
if (entityname !== "account") {
return;
}
const CRMAPI = await setmode();
const logDate = {
"ap_descripcion": "Registro Modificado " + formContext.data.entity.getId(),
}
CRMAPI.createRecord("ap_logclientes", logDate).then(
function success(result) {
console.log("Log created with ID: " + result.id);
},
function (error) {
console.error("Error creating log: " + error.message);
showMessage("Error creating log: " + error.message, "Error");
}
);
}
async function initForm(executionContext)
{
const formContext = executionContext.getFormContext();
const attributes = formContext.data.entity.attributes.get();
formContext.getAttribute("defaultuomscheduleid").setRequiredLevel('none');
formContext.getAttribute("defaultuomid").setRequiredLevel('none');
formContext.getAttribute("quantitydecimal").setRequiredLevel('none');
const CRMAPI = await setmode();
const options = `?$select=name,pricelevelid&$filter=name eq 'Lista de Precios'`;
const response = await CRMAPI.retrieveMultipleRecords("pricelevel", options).then(
function success(result) {
if (result.entities.length > 0) {
const priceLevel = result.entities[0];
const priceleveldata = [
{
id : priceLevel.pricelevelid,
name: priceLevel.name,
entityType: "pricelevel"
}
]
formContext.getAttribute("pricelevelid").setValue(priceleveldata);
}
},
function error(error) {
console.log("Error retrieving price level:", error.message);
showMessage("Error retrieving price level: " + error.message, "Error");
});
}
async function getCountryCode(executionContext) {
const formContext = executionContext.getFormContext();
const entityname = formContext.data.entity.getEntityName();
const countryCode = formContext.getAttribute('ap_codigopais').getValue();
if (countryCode == '') {
return;
}
const data = null;
var requestOptions = {
method: 'GET',
headers: {
"x-api-key": "--registrarse en apiverse para obtener el apikey--",
"Content-Type": "application/json",
"Accept": "application/json"
},
body: null,
redirect: 'follow'
};
const countryUrl = `https://api.apiverve.com/v1/callingcode?country=${countryCode}`;
fetch(countryUrl, requestOptions).then(response => {
if (!response.ok){
console.log(`${response.status} ${response.statusText}`);
return;
}
return response.text();
}).then(result => {
if (!result.status == 'ok') {
console.log(result.error);
return;
}
const resultJ = JSON.parse(result);
const phonecode = resultJ.data.callingcodes[0];
Xrm.Page.getAttribute('telephone1').setValue(phonecode);
}).catch(error => console.log(error));
}