Snippets Collections
var limit = null; //Integer or null
wipeEmployeeIDHRProfile(); // Remove employee Number for 'resetting' profiles for full pull
updateEmployeeNumbersHRProfile(limit); // Set employee number - for coalesce change on transform maps

function updateEmployeeNumbersHRProfile(limit) {
	var count = 0;
	var gr = new GlideRecord('sys_user');
	if(limit) gr.setLimit(limit);
	gr.query();
	while (gr.next()) {
		var employeeID = gr.user_name.toString(); // 'ND23858';
		if(isValid(employeeID)) {
			var firstTwo = employeeID.substring(0,2);
			var lastChars = employeeID.substring(2);

			if(isAlphaOrParen(firstTwo) && isNumeric(lastChars)) {
				updateEmployeeNumberByUser(gr.sys_id, lastChars);
				count++;
			}
			//else gs.info('EMPID | Bad Employee ID: ' + employeeID);
		} //else gs.info('EMPID | Bad Employee ID (not valid): ' + employeeID);

	}
	gs.info('EMPID | Total Count: ' + count);
}

function updateEmployeeNumberByUser(user, employeeID) {
	var gr = new GlideRecord("sn_hr_core_profile");
	gr.addQuery("user", user);
	gr.query();
	if (gr.next()) {
		gr.employee_number = employeeID;
		gr.autoSysFields(false);
		gr.setWorkflow(false);
		gr.update();
	}
}

function wipeEmployeeIDHRProfile() {
	var count = 0;
	var gr = new GlideRecord("sn_hr_core_profile");
	gr.addEncodedQuery("employee_number!=NULL");
	gr.query();
	while (gr.next()) {
		gr.employee_number = null;
		gr.autoSysFields(false);
		gr.setWorkflow(false);
		gr.update();
		count++;
	}
	gs.info('EMPID | Wiped HR Profile Employee IDs: ' + count);
}

function isAlphaOrParen(str) {
	return /^[a-zA-Z()]+$/.test(str);
}

function isNumeric(str) {
	if (typeof str != "string") return false; // we only process strings!  
	return !isNaN(str) && !isNaN(parseFloat(str)); // ...and ensure strings of whitespace fail
}

function isValid(str){
	if(str.indexOf('-') != -1 || str.indexOf('.') != -1)
		return false;
	else return true;
}
star

Tue Aug 31 2021 14:58:18 GMT+0000 (UTC) https://siteonedev.service-now.com/sys_script_fix.do?sys_id

#siteone #servicenow

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension