-
Notifications
You must be signed in to change notification settings - Fork 0
/
retrieveRecord.js
155 lines (134 loc) · 5.56 KB
/
retrieveRecord.js
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
var D365ToolsRetrieveRecord = new function () {
this.addModalStyles = function () {
let css = '.micro-modal {' +
'display: none;' +
'}' +
'.micro-modal.is-open {' +
'display: block;' +
'z-index: 100000;' +
'background: white;' +
'position: absolute;' +
'top: 100px;' +
'width: 100%;' +
'padding:50px;' +
'}'
let styleEl = document.createElement('style')
styleEl.appendChild(document.createTextNode(css))
document.head.appendChild(styleEl)
}
this.clearContents = function(el) {
while(el.firstChild){
el.removeChild(el.firstChild);
}
}
this.retrieveRecord = function () {
console.log('Retrieving record details...')
// Get the D365 XRM object unwrapped
var Xrm = window.wrappedJSObject.Xrm
// Rewrap the Xrm object so as not to mess with it
XPCNativeWrapper(window.wrappedJSObject.Xrm)
Xrm.Utility.showProgressIndicator()
// Get the entity type and id from the URL (to avoid using deprecated Xrm.Page)
// let currentUrl = window.location.href
// let urlParts = currentUrl.split('&')
// let etnLocation = findInArray(urlParts, 'etn=')
// let idLocation = findInArray(urlParts, 'id=')
// let etn = urlParts[etnLocation].replace('etn=', '')
// let id = urlParts[idLocation].replace('id=', '')
// console.log('etn='+etn+' id='+id)
// Get the entity type and id from the URL
const queryString = window.location.search
const urlParams = new URLSearchParams(queryString)
let etn = urlParams.get('etn')
let id = urlParams.get('id')
let globalContext = Xrm.Utility.getGlobalContext()
// Add a div to the document for our modal if not already created
if (document.getElementById('entityResultModal') == null) {
var modalElement = document.createElement('div')
modalElement.setAttribute('id', 'entityResultModal')
modalElement.setAttribute('class', 'micro-modal')
modalElement.setAttribute('aria-hidden', true)
modalElement.innerHTML =
'<div tabindex="-1" data-micromodal-close>' +
'<div role="dialog" aria-modal="true" aria-labelledby="entityResultModal-title" >' +
'<header>' +
'<h2 id="entityResultModal-title"></h2>' +
'<button aria-label="Close" data-micromodal-close style="padding:10px">Close</button>' +
'<br />' +
'<a href="#" target="_blank" id="openURLInTab">Open in tab</a>' +
'</header>' +
'<div id="entityResultModal-content"></div>' +
'</div>' +
'</div>'
document.body.appendChild(modalElement)
this.addModalStyles()
MicroModal.init()
}
// Create the HTTP Request with no filters (get everything!)
var req = new XMLHttpRequest();
// Pluralize the entity name because someone at MS made a bad decision to use plurals in the endpoints... sigh
let pluralEtn = getPluralEntityName(etn)
req.open("GET", globalContext.getClientUrl() + '/api/data/v9.0/' + pluralEtn + '(' + id + ')', 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=\"*\"")
// Handle the result
req.onreadystatechange = function() {
if (this.readyState === 4) {
req.onreadystatechange = null
if (this.status === 200) {
let result = JSON.parse(this.response)
console.log('result', result)
Xrm.Utility.closeProgressIndicator()
let modalTitle = document.getElementById('entityResultModal-title')
modalTitle.innerHTML = '' // clear past contents
modalTitle.appendChild(document.createTextNode(etn + ' ' + id))
let modalContent = document.getElementById('entityResultModal-content')
modalContent.innerHTML = '' //clear past contents
let innerContent = document.createElement('pre')
let innerContentText = document.createTextNode(JSON.stringify(result, undefined, 2))
innerContent.appendChild(innerContentText)
modalContent.appendChild(innerContent)
let openInTabLink = document.getElementById('openURLInTab')
openInTabLink.setAttribute('href', req.responseURL)
MicroModal.show('entityResultModal')
//alert(JSON.stringify(result))
} else {
alert(this.statusText)
}
}
}
console.log('req', req)
req.send()
function findInArray(a, stringToFind) {
let stringFound = false
a.forEach((el, i) => {
if(el.indexOf(stringToFind) !== -1) {
stringFound = i
}
})
return stringFound
}
}
function getPluralEntityName (etn) {
let pluralName = ''
let lastChar = etn[etn.length - 1]
let lastTwoChars = etn.substring(etn.length - 2)
let secondLastChar = etn.substring(etn.length - 2, 1)
let endingsThatTakeEs = ['s', 'ss', 'sh', 'ch', 'x', 'z']
let vowels = ['a', 'e', 'i', 'o', 'u']
if (endingsThatTakeEs.indexOf(lastChar) !== -1 || endingsThatTakeEs.indexOf(lastTwoChars) !== -1) {
pluralName = etn + 'es'
} else if (lastChar === 'y' && vowels.indexOf(secondLastChar) === -1) {
// If ends in consonant plus 'y', remove 'y' and add 'ies'
pluralName = etn.substring(0, etn.length -1) + 'ies'
} else {
pluralName = etn + 's'
}
return pluralName
}
}
//D365ToolsRetrieveRecord.addModalStyles()
D365ToolsRetrieveRecord.retrieveRecord()