BatchImport | Script Include | House of HR
Mon Dec 27 2021 20:36:14 GMT+0000 (UTC)
Saved by @mathiasVDD #javascript
var BatchImport = Class.create(); BatchImport.prototype = { initialize: function(request, response) { this.logger = new global.GSLog('hohr.csm.backoffice_batch.log', this.type); this.logger.includeTimestamp(); this.request = request; this.response = response; this.validated = false; this.error = false; this.errorObj = { 'statusCode': '', 'message': '', 'details': '' }; this.payload; }, process: function() { //this.payload = this._processPayload(); try { this.validated = this.validateRequest(); if (this.validated !== true) { this._setResponse(); } this.payload = this._processPayload(); this._setResponse(); } catch (e) { this.logErr('[process] caught an error: ' + e + '\nstack:\n' + e.stack); this._setError(500, 'Internal server error', ''); this._setResponse(); } }, validateRequest: function() { try { this.bodyData = request.body.data; if (gs.nil(this.bodyData)) { this.logWarning('[validateRequest] Body data is missing: ' + this.bodyData); this._setError(400, "Body is missing", "Expecting a body with properties."); return false; } } catch (e) { this.logErr('[validateRequest] caught an error: ' + e.message + '\nstack: ' + e.stack); } }, _importSet: function() { var importSet = new GlideRecord('sys_import_set'); importSet.initialize(); importSet.mode = 'asynchronous'; importSet.state = 'loading'; importSet.insert(); return importSet.sys_id; }, _processPayload: function() { var importSet; var transform; var i; if (this.request.body.data.u_bo_contact_imp) { importSet = this._importSet(); var contacts = new GlideRecord('u_bo_contact_imp'); for (i = 0; i < this.request.body.data.u_bo_contact_imp.length; i++) { if (this._isMemberOfPowerhouse(this.request.body.data.u_bo_contact_imp[i].u_email) == false) { contacts.initialize(); contacts.u_first_name = this.request.body.data.u_bo_contact_imp[i].u_first_name; contacts.u_last_name = this.request.body.data.u_bo_contact_imp[i].u_last_name; contacts.u_language = this.request.body.data.u_bo_contact_imp[i].u_language; contacts.u_email = this.request.body.data.u_bo_contact_imp[i].u_email; contacts.u_phone = this.request.body.data.u_bo_contact_imp[i].u_phone; contacts.u_nowjobs_id = this.request.body.data.u_bo_contact_imp[i].u_nowjobs_id; if (this.request.body.data.u_bo_contact_imp[i].u_accent_id) contacts.u_accent_id = this.request.body.data.u_bo_contact_imp[i].u_accent_id; contacts.sys_import_set = importSet; contacts.insert(); } } transform = this._transform(importSet, 'u_bo_contact_imp'); } if (this.request.body.data.u_bo_account_imp) { importSet = this._importSet(); var accounts = new GlideRecord('u_bo_account_imp'); for (i = 0; i < this.request.body.data.u_bo_account_imp.length; i++) { accounts.initialize(); accounts.u_vat_number = this.request.body.data.u_bo_account_imp[i].u_vat_number; accounts.u_commercial_name = this.request.body.data.u_bo_account_imp[i].u_commercial_name; accounts.u_official_name = this.request.body.data.u_bo_account_imp[i].u_official_name; accounts.u_account_manager_office = this.request.body.data.u_bo_account_imp[i].u_account_manager_office; accounts.u_phone = this.request.body.data.u_bo_account_imp[i].u_phone; accounts.u_nowjobs_id = this.request.body.data.u_bo_account_imp[i].u_nowjobs_id; if (this.request.body.data.u_bo_account_imp[i].u_accent_id) accounts.u_accent_id = this.request.body.data.u_bo_account_imp[i].u_accent_id; accounts.sys_import_set = importSet; accounts.insert(); } transform = this._transform(importSet, 'u_bo_account_imp'); } if (this.request.body.data.u_backoffice_candidate) { importSet = this._importSet(); var candidates = new GlideRecord('u_backoffice_candidate'); for (i = 0; i < this.request.body.data.u_backoffice_candidate.length; i++) { if (this._isMemberOfPowerhouse(this.request.body.data.u_backoffice_candidate[i].u_email) == false) { candidates.initialize(); candidates.u_first_name = this.request.body.data.u_backoffice_candidate[i].u_first_name; candidates.u_last_name = this.request.body.data.u_backoffice_candidate[i].u_last_name; candidates.u_language = this.request.body.data.u_backoffice_candidate[i].u_language; candidates.u_email = this.request.body.data.u_backoffice_candidate[i].u_email; candidates.u_phone = this.request.body.data.u_backoffice_candidate[i].u_phone; candidates.u_nowjobs_id = this.request.body.data.u_backoffice_candidate[i].u_nowjobs_id; if (this.request.body.data.u_backoffice_candidate[i].u_accent_id) candidates.u_accent_id = this.request.body.data.u_backoffice_candidate[i].u_accent_id; candidates.u_statute = this.request.body.data.u_backoffice_candidate[i].u_statute; candidates.u_national_nr = this.request.body.data.u_backoffice_candidate[i].u_national_nr; candidates.sys_import_set = importSet; candidates.insert(); } } transform = this._transform(importSet, 'u_backoffice_candidate'); } }, _isMemberOfPowerhouse: function (mail) { var member = false; var user = new GlideRecord('sys_user'); user.addQuery('email', mail); user.addQuery('sys_class_name', 'sys_user'); user.query(); if (user.next()) { if (user.company.u_powerhouse) { member = true; } } return member; }, _transform: function(importSet, table) { var importSetGr = new GlideRecord("sys_import_set"); importSetGr.get(importSet); importSetGr.table_name = table; var importSetId = importSetGr.getUniqueValue(); var importSetRun = new GlideImportSetRun(importSet); var importLog = new GlideImportLog(importSetRun, "SOAP Transform"); var ist = new GlideImportSetTransformer(); ist.setLogger(importLog); ist.setImportSetRun(importSetRun); ist.setImportSetID(importSetId); ist.setSyncImport(true); ist.transformAllMaps(importSetGr); if (ist.isError()) { gs.log('Error executing the transform'); } }, logDebug: function(msg) { this.logger.logDebug(msg); }, logInfo: function(msg) { this.logger.logInfo(msg); }, logWarning: function(msg) { this.logger.logWarning(msg); }, type: 'BatchImport' };
Comments