Name

sn_hr_integrations.HRIntegrationsHelper

Description

No description available

Script

var HRIntegrationsHelper = Class.create();

HRIntegrationsHelper.prototype = {
  initialize: function() {
  	// do nothing
  },
  
  createIntegrationServiceTrackerEntry : function(integrationServiceId, jobId, externalSourceId) {
  	var tracker = new GlideRecord(hrIntegrations.SERVICE_JOB_TRACKER_TABLE);
  	tracker.initialize();
  	tracker.setValue("hr_integration_service", integrationServiceId);
  	tracker.setValue("hr_integrations_job_tracker", jobId);
  	tracker.setValue("hr_external_source", externalSourceId);
  	tracker.setValue("state", "pending");
  	tracker.insert();
  },
  
  getIntegrationServiceTracker : function(integrationServiceId, jobId, state) {
  	var tracker = new GlideRecord(hrIntegrations.SERVICE_JOB_TRACKER_TABLE);
  	tracker.addQuery("hr_integration_service", integrationServiceId);
  	tracker.addQuery("hr_integrations_job_tracker", jobId);
  	if (!gs.nil(state))
  		tracker.addQuery("state", state);
  	tracker.query();
  	if (tracker.next())
  		return tracker;
  	return null;
  },
  
  updateIntegrationServiceTrackerEntry : function(integrationServiceId, jobId, externalSourceId, data) {
  	var tracker = new GlideRecord(hrIntegrations.SERVICE_JOB_TRACKER_TABLE);
  	tracker.addQuery("hr_integration_service", integrationServiceId);
  	tracker.addQuery("hr_integrations_job_tracker", jobId);
  	tracker.addQuery("hr_external_source", externalSourceId);
  	tracker.query();
  	if(tracker.next()) {
  		this.setParamsForTracker(tracker, data);
  		tracker.update();
  	}
  },
  
  
  getTotalNumberOfRecords : function(integrationServiceId, jobId, externalSourceId) {
  	var tracker = new GlideRecord(hrIntegrations.SERVICE_JOB_TRACKER_TABLE);
  	tracker.addQuery("hr_integration_service", integrationServiceId);
  	tracker.addQuery("hr_integrations_job_tracker", jobId);
  	tracker.addQuery("hr_external_source", externalSourceId);
  	tracker.query();
  	if (tracker.next())
  		return tracker.total;
  	else
  		return 0;
  },
  
  updateTracker : function(trackerGr, data) {
  	this.setParamsForTracker(trackerGr, data);
  	trackerGr.update();
  },
  
  setParamsForTracker: function(trackerGr, data) {
  	var val;
  	for (var fieldName in data) {
  		this.logDebug("FIELD NAMES >>>>>>>> " + fieldName + "  >>>Value>>> " + data[fieldName], hrIntegrations.HR_INT_LOADER_LOG);
  		if (fieldName == 'inserts') {
  			if (trackerGr.getValue(fieldName))
  				val =  parseInt(data[fieldName]) + parseInt(trackerGr.getValue(fieldName));
  			else
  				val = parseInt(data[fieldName]);
  			
  			trackerGr.setValue(fieldName, val);
  		} else if (fieldName == 'skipped') {

  			if (trackerGr.getValue(fieldName))
  				val = parseInt(data[fieldName]) +parseInt(trackerGr.getValue(fieldName));
  			else
  				val = parseInt(data[fieldName]);

  			trackerGr.setValue(fieldName, val);
  		} else
  			trackerGr.setValue(fieldName, data[fieldName]);
  	}
  },
  
  getRetriever : function(externalSourceName) {
  	var retriever;
  	/* The following code is required for customers upgrading to Madrid from a previous version.
  	If you upgrade your instance, do not delete the following line.*/
  	if (externalSourceName == hrIntegrations.WORKDAY_SOURCE_NAME)
  		retriever = new HRIntegrationsWorkdayDataRetriever();
  	else if (externalSourceName == hrIntegrations.SUCCESSFACTOR_SOURCE_NAME) {
  	     /*The following code is needed to support the upgrade of customers using the SF integration. 
  	     If you upgrade your instance, do not delete this block of code */
  		 retriever =  new HRIntegrationsSuccessFactorDataRetriever();
  	} else
  		retriever = new HRIntegrationDataRetriever();
  	return retriever;
  },
  
  setProperty: function(column, value, id) {
  	var jobTracker = new GlideRecord(hrIntegrations.JOB_TRACKER_TABLE);
  	this.logDebug("Setting Column Value " + column + "  " + value + "  " + id);
  	if (jobTracker.get(id)) {
  		jobTracker.setValue(column, value ? value : '');
  		jobTracker.update();
  	}
  },
  
  getProperty: function(column,sourceId) {
  	var jobTracker = new GlideRecord(hrIntegrations.JOB_TRACKER_TABLE);
  	jobTracker.addQuery('source',sourceId);
  	jobTracker.orderByDesc('sys_updated_on');
  	if (column == hrIntegrations.LAST_SYNC_DATE) 
  		jobTracker.addNotNullQuery(column);
  	jobTracker.query();
  	if (jobTracker.next())
  		return jobTracker.getValue(column);
  	else
  		return this.checkPreviousPropertyValue(column);	
  },
  
  checkPreviousPropertyValue : function(column) {
  	var jobTracker = new GlideRecord(hrIntegrations.JOB_TRACKER_TABLE);
  	jobTracker.addNullQuery('source');
  	jobTracker.orderByDesc('sys_updated_on');
  	if (column == hrIntegrations.LAST_SYNC_DATE) 
  		jobTracker.addNotNullQuery(column);
  	jobTracker.query();
  	if (jobTracker.next())
  		return jobTracker.getValue(column);
  	else
  		return "";
  },	
  
  getSessionToken: function(jobid) {
  	var jobTracker = new GlideRecord(hrIntegrations.JOB_TRACKER_TABLE);
  	jobTracker.addQuery('sys_id',jobid);
  	jobTracker.orderByDesc('sys_updated_on');
  	jobTracker.query();
  	if (jobTracker.hasNext()) {
  		 jobTracker.next();
  		 return jobTracker.getValue("current_external_session_token");
  	} else
  		 return '';
  },
  
  startWorkflow : function(workflowVars, intServiceName) {
  	var workflow = new global.Workflow();
  	var gr = workflow.startFlow(hrIntegrations.INTEGRATION_WORKFLOW,null,null,workflowVars);
  },
  
  addAdditionalInputs : function(input_values_json,externalSourceGr) {
  	var addInputsGr = new GlideRecord(hrIntegrations.ADDITIONAL_INPUTS);
  	addInputsGr.addQuery('source',externalSourceGr.sys_id);
  	addInputsGr.query();
  	while (addInputsGr.next()) {
  		if (addInputsGr.password_type)
  			input_values_json[addInputsGr.key]= new global.HRSecurityUtils().encryptOrDecryptAditionalValues(false,addInputsGr.getValue('value')); // decrypt the values
  		else
  			input_values_json[addInputsGr.key]= addInputsGr.getValue('value');
  	}
  	return input_values_json;
  },
  
  setInputParameters : function(externalSourceGr, jobId, workflowVars, input_values_json, pageNumber, serviceMappingGr, stagingTable, stagingTableSysId) {
  	
  	var LOG_PREFIX = externalSourceGr.name + "-" +jobId ;
  	var logSource = hrIntegrations.HR_INT_LOADER_LOG + "_"+ externalSourceGr.name;
  	
  	input_values_json.username_inbound = externalSourceGr.username_inbound.toString();
  	input_values_json.username_outbound = externalSourceGr.username_outbound.toString();
  	input_values_json.use_session_token = externalSourceGr.use_session_token.toString();
  	input_values_json.api_version = this.getActiveSourceVersionValue(externalSourceGr.sys_id);
  	input_values_json.pageNumber = pageNumber;
  	input_values_json.external_source_id = externalSourceGr.sys_id.toString();
  	input_values_json.change_in_functionid = false;
  	input_values_json.changed_function_id = "";
  	input_values_json = this.addAdditionalInputs(input_values_json,externalSourceGr);
  	
  	if (serviceMappingGr) {
  		var serviceMappingId = serviceMappingGr.sys_id.toString();
  		var lastSyncDate = this.getProperty(hrIntegrations.LAST_SYNC_DATE, externalSourceGr.sys_id);
  		var serverTS = this.getProperty(hrIntegrations.CURRENT_EXTERNAL_SERVER_TIME, externalSourceGr.sys_id);
          
  		var source = new GlideRecord(stagingTable);
  		source.get(stagingTableSysId);
  		this.logDebug("STAGING TABLE " + stagingTable + "  " + stagingTableSysId);
  		var schemaMapping = new GlideRecord("sn_hr_integrations_outbound_schema_mapping");
  		schemaMapping.addQuery("service_mapping",'CONTAINS' , serviceMappingId);
  		schemaMapping.addQuery("staging_table", stagingTable);
  		schemaMapping.query();
  		while (schemaMapping.next()) {
  			if (schemaMapping.getValue("use_source_script") ==true) {
  				var vars ={source:source, externalSourceId:externalSourceGr.sys_id.toString()};
  				var evaluator = new GlideScopedEvaluator(); 
  				input_values_json[schemaMapping.target_column] = evaluator.evaluateScript(schemaMapping, 'source_script',vars);
  			} else {
  				if (schemaMapping.source_column.indexOf(".") > -1) {
  					var fieldNames = schemaMapping.source_column.toString().split('.');
  					var elem = source.getElement(fieldNames[0]).getRefRecord();
  					input_values_json[schemaMapping.target_column] = elem.getValue(fieldNames[1]);
  				} else
  					input_values_json[schemaMapping.target_column] = source.getValue(schemaMapping.source_column);
  			}

  			if (schemaMapping.getValue("mandatory") == true  && gs.nil(input_values_json[schemaMapping.target_column])) {
  				workflowVars.abortAction = true;
  				workflowVars.abortActionErrorMessage = schemaMapping.target_column;
  				gs.error("Mandatory field value returned empty for HR Integration Outbound Schema Mapping record with Table: "+ stagingTable + ", Source FieldName : " + schemaMapping.source_column+ ", Target Field Name "+ schemaMapping.target_column);
  				return;
  			}

  			if (gs.nil(input_values_json[schemaMapping.target_column]))
  				input_values_json[schemaMapping.target_column] = '';
  		}
  	}
  	
  	workflowVars.input_values = JSON.stringify(input_values_json);
  	this.logDebug(LOG_PREFIX +" Input Value JSON " + input_values_json, logSource);
  },
  
  setWorkflowParameters : function(outboundRequestType,outboundMessageId, outboundMessageFunctionId, jobId, externalSourceGr, pageNumber, serviceMappingGr, stagingTable, stagingTableSysId) {
  	var LOG_PREFIX = externalSourceGr.name + "-" + jobId ;
  	var logSource = hrIntegrations.HR_INT_LOADER_LOG + "_" + externalSourceGr.name;
  	
  	this.logDebug(LOG_PREFIX +" Setting workflow parameters", logSource);
  	var workflowVars = {};
  	var input_values_json = {};
  	this.setInputParameters(externalSourceGr, jobId, workflowVars, input_values_json, pageNumber, serviceMappingGr, stagingTable, stagingTableSysId);
  	if (!gs.nil(jobId))
  		input_values_json.job_id = jobId.toString();
  	if (serviceMappingGr){
  		input_values_json.hr_integration_service_id = serviceMappingGr.hr_integration_service.toString();
  		input_values_json.hr_integration_outbound_service = serviceMappingGr.hr_integration_outbound_service.toString();
  	}
  	
  	workflowVars.outbound_request_type=outboundRequestType;
  	
  	if (outboundRequestType=="soap") {
  		workflowVars.soap_message_id = outboundMessageId;
  		workflowVars.soap_message_function_id =  outboundMessageFunctionId;
  		workflowVars.service_endpoint = externalSourceGr.endpoint_url;	
  	} else if(outboundRequestType=="rest") {
  		workflowVars.rest_message_id = outboundMessageId;
  		workflowVars.rest_message_function_id =  outboundMessageFunctionId;
  		var restMsg = new GlideRecord("sys_rest_message_fn");
  		restMsg.get(outboundMessageFunctionId);
  		workflowVars.service_endpoint = restMsg.getValue("rest_endpoint");
  	}
  	
  	if (gs.nil(workflowVars.service_endpoint))
  		workflowVars.service_endpoint = externalSourceGr.endpoint_url;	
  	
  	workflowVars.session_token= this.getSessionToken(jobId);
  	this.logDebug('Job Id :' + jobId + ' Session Token:' + workflowVars.session_token);
  	workflowVars.input_values = JSON.stringify(input_values_json);
  	this.logDebug(LOG_PREFIX + " End Setting workflow parameters", logSource);
  	return workflowVars;
  },
  
  jobCompleted : function(externalSourceName, jobId) {
  	var jobTrackerAfter = new GlideRecord(hrIntegrations.JOB_TRACKER_TABLE);
  	if (jobTrackerAfter.get(jobId)) {
  		jobTrackerAfter.setValue("state","complete");
  		jobTrackerAfter.setValue("job_ended_at",new GlideDateTime().getDisplayValue());
  		jobTrackerAfter.update();
  	} else
  		gs.error(externalSourceName + " Invalid Job ID . Job not closed." , hrIntegrations.HR_INT_LOADER_LOG +"_"+ externalSourceName);
  	
  },
  
  getPhoneComponents : function (phoneNumber,  fieldName) {	
  	var intl_code = fieldName + '_intl_code';
  	var area_code = fieldName + '_area_code';
  	var phone_number = fieldName + '_phone_number';
  	
  	var format = (phoneNumber.split(/[ ]+/));
  	var formattedNumber = [];
  	for (var i=0; i<format.length; i++) {
  		var part = format[i];
  		if (part.indexOf('+') == 0)
  			formattedNumber['intl_code'] = part.replace('+','');
  		else if (part.indexOf('(') == 0)
  			formattedNumber['area_code'] = part.replace(/[()]/g, '');
  		else
  			formattedNumber['phone_number'] = part.replace(/[-]/g, '');
  	}
  	return formattedNumber;
  },
  
  getSourceProperty : function(externalSourceId, name) {
  	var extSourceProperties = new GlideRecord(hrIntegrations.HR_INT_SOURCE_PROPERTIES);
  	extSourceProperties.addActiveQuery();
  	extSourceProperties.addQuery("source",externalSourceId);
  	extSourceProperties.addQuery("name", name);
  	extSourceProperties.query();
  	if (extSourceProperties.next())
  		return extSourceProperties.getValue("value");
  	return "";
  },
  
  isPushEnabled: function(externalSourceId) {
  	var hrIntHelper = new HRIntegrationsHelper();
  	var isPushEnabled = this.getSourceProperty(externalSourceId, hrIntegrations.HR_INT_ENABLE_PUSH);
  	return (isPushEnabled == 'true' ? true: false);
  },
  
  isDryRun: function(externalSourceId) {
  	var extSourceProperties = new GlideRecord(hrIntegrations.HR_INT_SOURCE_PROPERTIES);
  	extSourceProperties.addActiveQuery();
  	extSourceProperties.addQuery("source",externalSourceId);
  	extSourceProperties.addQuery("name",hrIntegrations.HR_INT_DRY_RUN);
  	extSourceProperties.query();
  	var dryRun = false;
  	if (extSourceProperties.next()) {
  		if (extSourceProperties.getValue("value") == "true") 
  			dryRun = true;
  	}
  	this.logDebug("Dry Run is " + dryRun, hrIntegrations.HR_INT_LOADER_LOG);
  	return dryRun;
  },

  cleanupStagingTable : function(externalSourceId) {
  	var stagingTable = new GlideRecord("sn_hr_integrations_staging");
  	stagingTable.addEncodedQuery("source=" + externalSourceId + "^sys_updated_on<javascript:gs.daysAgoStart(7)");
  	stagingTable.query();
  	stagingTable.deleteMultiple();
  },
  
  getMatchingOutboundServices : function(glideRecord, externalSourceId,tableName) {
  	if (gs.nil(glideRecord)) {
  		this.logDebug("Null input parameter: glideRecord");
  		return;
  	}
  	
  	var outBoundServices = [];
  	
  	if (!externalSourceId)
  		externalSourceId = glideRecord.source;
  	
  	if (!externalSourceId)
  		return outBoundServices;
  	
  	if (glideRecord.transaction_log && glideRecord.transaction_log.changes()) {
  		this.logDebug("Update from Sync, So Ignore Pushing");
  		return;
  	}
  	
  	if (this.isPushEnabled(externalSourceId) == "false") {
  		this.logDebug("Push not enable, Ignore");
  		return;
  	}
  	
  	var gr = new GlideRecord(hrIntegrations.HR_INT_OUT_SOURCE_TRIGGER);
  	if(gs.nil(tableName))
  		tableName = glideRecord.getTableName();
  	gr.addQuery("trigger_source", tableName);
  	gr.addQuery("hr_integrations_outbound_service.active", true);
  	gr.query();
  	var filterConditionMet;
  	
  	while (gr.next()) {
  		if(outBoundServices.indexOf(gr.getValue("hr_integrations_outbound_service")) > -1)
  			continue;
  		var filter = gr.getValue("trigger_condition");
  		if (!gs.nil(filter))
  			filterConditionMet = GlideFilter.checkRecord(glideRecord, filter, true);
  		else
  			filterConditionMet = false;

  		if (filterConditionMet)
  			outBoundServices.push(gr.getValue("hr_integrations_outbound_service"));
  	}
  	return outBoundServices;
  },
  
  invokeMatchingOutboundServices : function(matchingServices, sourceRecord, externalSourceId,tableName,IsSynchronous) {
  	var outboundServiceRecord;
  	var recordCount = {
  		inserts:0,
  		ignored:0
  	};
  	
  	//if enable push property is turned off 
  	var sourcePropValue = this.getSourceProperty(externalSourceId, hrIntegrations.HR_INT_ENABLE_PUSH);
  	if(sourcePropValue == "false")
  		return recordCount;
  	
  	for (var i =0; i < matchingServices.length; i++) {
  		outboundServiceRecord = new GlideRecord(hrIntegrations.HR_INT_OUT_SOURCE);
  		if (outboundServiceRecord.get(matchingServices[i])) {
  			if(gs.nil(tableName))
  				tableName = sourceRecord.getTableName();
  			if (tableName == outboundServiceRecord.getValue("source_table"))
  				if(this.pushDataToExternalInterface(sourceRecord, outboundServiceRecord, externalSourceId,tableName,IsSynchronous))
  					recordCount.inserts = recordCount.inserts+1;
  				else
  					recordCount.ignored = recordCount.ignored+1;
  			else
  				gs.error("Source Record is not of type " + outboundServiceRecord.getValue("source_table") + " Source Record Type  " + sourceRecord.getTableName());
  		}
  	}
  	return recordCount;
  },
  
  pushDataToExternalInterface : function(glideRecord, outBoundServiceRecord, externalSourceId,tableName,isSynchronous) {
  	var mapping = new GlideRecord(hrIntegrations.HR_INT_SERVICE_MAPPING);
  	mapping.addActiveQuery();
  	mapping.addQuery("hr_integration_outbound_service", outBoundServiceRecord.sys_id);
  	if (externalSourceId)
  		mapping.addQuery("hr_external_source", externalSourceId);
  	mapping.query();
  	if (mapping.getRowCount() < 1) {
  		gs.error("Mapping not found for " + outBoundServiceRecord.name);
  		return false;
  	}
  	var gr;
  	while(mapping.next()) {
  		gr = new GlideRecord(hrIntegrations.HR_INT_EXTERNAL_INTERFACE);
  		//check if the entry already exists
  		gr.addQuery("hr_integration_service_mapping", mapping.getValue("sys_id"));
  		if(gs.nil(tableName))
  			tableName =  glideRecord.getTableName();
  		gr.addQuery("source_table_name", tableName);
  		gr.addQuery("source_table_sys_id", glideRecord.sys_id);
  		gr.addQuery("status","!=","completed");
  		gr.query();
  		if (!gr.next()) {
  			gr.initialize();
  			gr.setValue("source_table_name", tableName);
  			gr.setValue("source_table_sys_id", glideRecord.sys_id);
  			if(!gs.nil(isSynchronous))
  				gr.setValue("process_type", "synchronous");
  			else
  				gr.setValue("process_type", "asynchronous");
  			gr.setValue("status", "pending");
  			gr.setValue("hr_integration_service_mapping",mapping.getValue("sys_id"));
  			gr.insert();
  			this.logDebug("Posted " + outBoundServiceRecord.name + " event to external interface Profile "  +  glideRecord.sys_id);
  			return true;
  		}
  	}
  	return false;
  },
  
  getDataBusValue : function(data,dataValues) {
  	
  	for(var i=0; i<dataValues.length; i++) {
  		try {
  			if (data.get(dataValues[i]).returnResponse)
  				return data.get(dataValues[i]).returnResponse;
  		}
  		catch(err) {
  			continue;
  		}
  	}
  	return data.get(8).returnResponse ;
  },
  
  getActiveSourceVersion : function(externalSourceId) {
  	var gr = new GlideRecord("sn_hr_integrations_source_version");
  	gr.addQuery("source",externalSourceId);
  	gr.addActiveQuery();
  	gr.query();
  	if (gr.next())
  		return gr.getValue("sys_id");
  	else
  		return null;
  },
  
  getActiveSourceVersionValue : function(externalSourceId) {
  	var gr = new GlideRecord("sn_hr_integrations_source_version");
  	gr.addQuery("source",externalSourceId);
  	gr.addActiveQuery();
  	gr.query();
  	if (gr.next())
  		return gr.getValue("version");
  	else
  		return "";
  },
  
  getActiveSourceVersionFromRecord : function(externalSourceGr) {
  	return this.getActiveSourceVersion(externalSourceGr.getValue("sys_id"));
  },
  
  getSchemaMap: function(svcMapping) {
  	var schMapping = new GlideRecord(HR_INT_SCHEMA_MAPPING);
  	schMapping.addQuery('service_mapping','CONTAINS', svcMapping.getValue('sys_id'));
  	var sourceVersion = this.getActiveSourceVersion(svcMapping.getValue("hr_external_source"));
  	if (!gs.nil(sourceVersion))
  		schMapping.addQuery("source_version",'CONTAINS', sourceVersion);
  	schMapping.query();
  	var schemaMap = {};
  	schemaMap.hasSchemaMap = false;
  	while(schMapping.next()) {
  		schemaMap.hasSchemaMap = true;
  		if (!schemaMap.stagingTable)
  			schemaMap.stagingTable = schMapping.getValue("staging_table");
  		if (!schMapping.use_script) {
  			if(schemaMap[schMapping.getValue('staging_table_column')]){
  				schemaMap[schMapping.getValue('staging_table_column')].hasMultipleValues = true;
  				schemaMap[schMapping.getValue('staging_table_column')].valueList.push(schemaMap[schMapping.getValue('staging_table_column')].value);
  				schemaMap[schMapping.getValue('staging_table_column')].valueList.push(schMapping.getValue('external_entity_column_key'));
  			}
  			else
  				schemaMap[schMapping.getValue('staging_table_column')]= {type:schMapping.getValue("type") ,value:schMapping.getValue('external_entity_column_key'), mandatory:schMapping.getValue("mandatory"), hasMultipleValues : false, valueList:[]};
  		} else
  			schemaMap[schMapping.getValue('staging_table_column')]= {type:'script' ,value:schMapping.sys_id.toString(), mandatory:schMapping.getValue("mandatory"),hasMultipleValues : false, valueList:[]};
  	}
  	return schemaMap;
  },
  
  createEntryInExternalInterface : function (sourceTableGr, OutBoundService, integrationSourceSysId) {
  	var externalInterfaceGr = new GlideRecord(hrIntegrations.HR_INT_EXTERNAL_INTERFACE);
  	externalInterfaceGr.addQuery("source_table_name", sourceTableGr.getTableName());
  	externalInterfaceGr.addQuery("source_table_sys_id", sourceTableGr.sys_id);
  	externalInterfaceGr.addQuery("process_type", "synchronous");
  	externalInterfaceGr.addQuery("status","IN","pending,work_in_progress");
  	externalInterfaceGr.query();
  	if (externalInterfaceGr.hasNext()) {
  		this.logDebug("Record with source_table_name : " + sourceTableGr.getTableName() + " and source_table_sys_id : " + sourceTableGr.sys_id + "already exists in external interface in either pending/work in progress status");
  		return "";
  	}
  	externalInterfaceGr.initialize();
  	externalInterfaceGr.setValue("source_table_name", sourceTableGr.getTableName());
  	externalInterfaceGr.setValue("source_table_sys_id", sourceTableGr.sys_id);
  	externalInterfaceGr.setValue("process_type", "synchronous");
  	externalInterfaceGr.setValue("status", "pending");

  	var serviceMappingGr = new GlideRecord(hrIntegrations.HR_INT_SERVICE_MAPPING);
  	serviceMappingGr.addActiveQuery();
  	serviceMappingGr.addEncodedQuery("active=true^hr_integration_outbound_service=" + OutBoundService + "^hr_external_source=" + integrationSourceSysId);
  	serviceMappingGr.query();
  	if (serviceMappingGr.hasNext()) {
  		serviceMappingGr.next();
  		externalInterfaceGr.setValue("hr_integration_service_mapping", serviceMappingGr.getValue("sys_id"));
  		var externalInterfaceSysId = externalInterfaceGr.insert();
  		return externalInterfaceSysId;
  	}
  	else
  		this.logDebug("No Service Mapping found for outbound service");
  	return "";
  },
  
  backGroundCheckReportAsAttachment : function(encodedReportHTML, clientUniqueId, externalIntId) {
  	if (!gs.nil(encodedReportHTML)) {
  		var talentManagementCaseSysId = "";
  		if (gs.nil(clientUniqueId)) {
  			var externalInterfaceGr = new GlideRecord("sn_hr_integrations_external_interface");
  			if (externalInterfaceGr.get(externalIntId))
  				talentManagementCaseSysId = externalInterfaceGr.source_table_sys_id;
  		} else
  			talentManagementCaseSysId = clientUniqueId;

  		var talentManagementGr = new GlideRecord(hrIntegrations.HR_CORE_CASE_TALENT_MANAGMENT);
  		if (talentManagementGr.get(talentManagementCaseSysId)) {
  			if (!gs.nil(talentManagementGr.background_check_report_id))
  				this._deleteExistingBgReportAttachment(hrIntegrations.HR_CORE_CASE_TALENT_MANAGMENT,talentManagementCaseSysId,talentManagementGr.background_check_report_id);
  			var reportName = "";
  			if (!gs.nil(talentManagementGr.subject_person.first_name))
  				reportName = talentManagementGr.subject_person.first_name.toString() + "_" + "backgroundcheck_report.html";
  			else
  				reportName = talentManagementGr.number.toString().toLowerCase()+"_"+"backgroundcheck_report.html";
  			return this._createNewBgCheckReportAttachment(encodedReportHTML, reportName, talentManagementGr);
  		}
  	}
  	return "";
  },
  		
  /*
  * Convenience method to prevent the code becoming unreadable from the useful debug statements
  */
  logDebug : function(str) {
      if (gs.isDebugging())
  	    gs.debug(str);
  },
  
  /**
  * Response message which is sent back to the third party integrations
  * @input status : status of the request 
  * @return response body
  **/
  generateAPIResponseMessage : function (status) {
  	
  	return "<BackgroundReportResponse xmlns='http://www.cpscreen.com/schemas' " +
  			"xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " + 
  			"xsi:schemaLocation='http://www.cpscreen.com/schemas CPBackgroundReportResponse.xsd'>" +
  			"<Status>"+status+"</Status>" +
  			"</BackgroundReportResponse>";	
  },
  
  
 _deleteExistingBgReportAttachment : function (tableName, recordSysId, existingBgAttachmentSysId ) {
  	var existingBgSysAttachment = new GlideSysAttachment();
  	var attachmentGR = existingBgSysAttachment.getAttachments(tableName,recordSysId);
      while (attachmentGR.next()) {
  		if (attachmentGR.sys_id == existingBgAttachmentSysId)
  			new global.HRSecurityUtils().removeSysAttachmentsBySysId(existingBgAttachmentSysId);
  	}
  },
  	
  _createNewBgCheckReportAttachment : function(encodedReportHTML,reportName, tableToInsertGr) {
  	var attachmentSysId ="";
  	var glideSysAttachmentAPI = new GlideSysAttachment();
  	var reportBinData = gs.base64Decode(encodedReportHTML);
  	attachmentSysId = glideSysAttachmentAPI.write(tableToInsertGr, reportName, "text", reportBinData);
  	return attachmentSysId;
  },	
  
  doesRecordExistsInMappingTable: function(tableName,sysId){
  	var todoMappingRecord = new GlideRecord(hrIntegrations.TODO_OUTBOUND_MAPPING_TABLE);
  	todoMappingRecord.addQuery('reference_sys_id',sysId);
  	todoMappingRecord.addQuery('reference_table',tableName);
  	todoMappingRecord.query();
  	if(todoMappingRecord.hasNext())
  		return true;
  	else
  		return false;
  },

  getLinkUrl: function(todoRecord){
  	var hrTaskHelper = new sn_hr_core.hr_Task();
  	return hrTaskHelper.getToDoURL(todoRecord.getTableName(),todoRecord.sys_id);
  },

  //Get's the userId reference of sys_user table for the given IntegrationUserId
  getUserId: function(intUserId) {
  	var userId = "";
  	var hrProfileGr = new GlideRecord(hrIntegrations.HR_PROFILE_TABLE);
  	hrProfileGr.addActiveQuery();
  	hrProfileGr.addQuery("integration_user_id", intUserId);
  	hrProfileGr.query();
  	if(hrProfileGr.next())
  		userId = hrProfileGr.getValue("user");
  	
  	return userId;
  },

  type: 'HRIntegrationsHelper'
};

Sys ID

ee8dc6929fab22003be01050a57fcfc2

Offical Documentation

Official Docs: