Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CRMWebAPI.prototype.batch = function (entity, fetchXml, QueryOptions) {
- const self = this
- // build the body
- var body = '--batch_contactfetch\n'
- body += 'Content-Type: application/http\n'
- body += 'Content-Transfer-Encoding: binary\n'
- body += '\n'
- body += 'GET ' + self.config.APIUrl + `${entity}?fetchXml=${encodeURIComponent(fetchXml)} HTTP/1.1\n`
- body += 'Content-Type: application/json\n'
- body += 'OData-Version: 4.0\n'
- body += 'OData-MaxVersion: 4.0\n'
- body += '\n'
- body += '--batch_contactfetch--'
- return new Promise(function (resolve, reject) {
- const url = self._BuildQueryURL("$batch", QueryOptions, self.config)
- self._log('ODataUrl',url)
- //self._GetHttpRequest(self.config, "POST", url, { // won't work, additive headers
- self._hack(self.config, "POST", url, {
- 'data': body,
- headers: {
- "Content-Type": "multipart/mixed;boundary=batch_contactfetch"
- }
- }, function (err, res) { // callback
- if (err != false) {
- self._log('Errors','batch error',res)
- reject(res)
- } else {
- //if(DEBUG) console.log("raw r", res)
- var data = JSON.parse(self._sliceBatchResponse(res.response), self._DateReviver)
- if(data.error) {
- // its really an error
- self._log('Errors','batch error', res)
- reject(data.error)
- return
- }
- var nextLink = data['@odata.nextLink']
- var recordCount = data['@odata.count']
- var response = {
- List: data.value,
- Count: recordCount
- }
- if ((QueryOptions != null) && (QueryOptions.RecordAction != null))
- {
- response.List.forEach(function(record){
- QueryOptions.RecordAction(record)
- });
- response.List = [];
- }
- if ((QueryOptions != null) && (QueryOptions.PageAction != null))
- {
- QueryOptions.PageAction(response.List)
- response.List = []
- }
- if (!nextLink) {
- resolve(response)
- } else {
- if(DEBUG) console.log("NEXT LINK BUT NEED TO IMPLEMENT WHILST!!!")
- if(DEBUG) console.log("response", data)
- resolve(response)
- // the lib has page processing code here...copy and paste it!
- }
- }
- })
- })
- }
- CRMWebAPI.prototype._hack = function (config, method, url, payload, callback) {
- var self = this;
- var req = new XMLHttpRequest();
- //req.open(method, encodeURI(url), true);
- req.open(method, url, true);
- if (config.AccessToken != null) req.setRequestHeader("Authorization", "Bearer " + config.AccessToken);
- req.setRequestHeader("Accept", "application/json");
- req.setRequestHeader("OData-MaxVersion", "4.0");
- req.setRequestHeader("OData-Version", "4.0");
- if (config.callerId) req.setRequestHeader("MSCRMCallerID", config.callerId);
- if (config.CallerID) req.setRequestHeader("MSCRMCallerID", config.CallerID);
- if (['POST', 'PUT', 'PATCH'].indexOf(method) >= 0) {
- req.setRequestHeader("Content-Length", payload.data.length);
- //req.setRequestHeader("Content-Type", "application/json");
- }
- if (payload.headers !== 'undefined') {
- for (var name in payload.headers) {
- req.setRequestHeader(name, payload.headers[name]);
- }
- }
- req.onreadystatechange = function () {
- if (this.readyState == 4 /* complete */ ) {
- req.onreadystatechange = null;
- if ((this.status >= 200) && (this.status < 300)) {
- callback(false, {
- 'response': this.response,
- 'headers': self.parseResponseHeaders(this.getAllResponseHeaders())
- });
- } else {
- callback(true, this)
- }
- }
- };
- if (['POST', 'PUT', 'PATCH'].indexOf(method) >= 0) {
- req.send(payload.data);
- } else {
- req.send();
- }
- }
- CRMWebAPI.prototype._sliceBatchResponse = function(response) {
- const start = response.indexOf("{")
- const end = response.lastIndexOf("}")+1
- //console.log("stats", start, end, response[start], response[end-1])
- //console.log("parsing", response.substring(start, end))
- return response.substring(start, end)
- }
Add Comment
Please, Sign In to add comment