We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Retain existing values using field map script

Evren Yamin
Tera Contributor

Hello,

Is there a way to retain existing values of the field when using a transform map?

My requirement is for example I have this record on group table:

group name = cab approval

type = itil

Then I'm using transform map to update this record. The data I'm loading for example is:

group name = cab approval

type = catalog

What needs to happen after transform is:

group name = cab approval

type = itil, catalog

 

I need to retain the existing value of the type field on the group table after transform. If not empty then update, if empty then inser

Appreciate all the help.

1 ACCEPTED SOLUTION

The code does not implement the part where it is supposed to check whether the current list already contains the item to be added. I would go with a field map script as below:

answer = (function transformEntry (source, target, action, log) {
	var groupTypeUniqueValues = getGroupTypeUniqueValues('' + source.u_sys_user_group_type);

	return addTypeToNewGroup(action, groupTypeUniqueValues) ||
		addTypeToGroupWithNoType(target.type.nil(), groupTypeUniqueValues) ||
		addTypeToGroupWithType(getGroupTypes('' + target.type), groupTypeUniqueValues);

	function addMissingTypes () {
		return function (existingValues, newValue) {
			if (existingValues.indexOf(newValue) < 0)
				existingValues.push(newValue);

			return existingValues;
		};
	}

	function addTypeToGroupWithNoType (typeIsEmpty, value) {
		if (typeIsEmpty)
			return value.join(',');
	}

	function addTypeToGroupWithType (existingValues, newValues) {
		return newValues
			.reduce(addMissingTypes(), existingValues)
			.join(',');
	}

	function addTypeToNewGroup (action, value) {
		if (action == 'insert')
			return value.join(',');
	}

	function getGroupTypes (groupTypeList) {
		return groupTypeList
			.split(semiColonCommaOrNewLineSorrundedByWhiteSpace())
			.filter(retainNotEmpty())
			.filter(retainUnique());
	}

	function getGroupTypeUniqueValues (groupTypeList) {
		return groupTypeList
			.split(semiColonCommaOrNewLineSorrundedByWhiteSpace())
			.filter(retainNotEmpty())
			.map(getUniqueValueFromDisplayValue('sys_user_group_type'))
			.filter(retainNotEmpty())
			.filter(retainUnique());
	}

	function getUniqueValueFromDisplayValue (tableName) {
		return function getUniqueValueFromDisplayValue (displayValue) {
			return '' + GetIDValue(tableName, displayValue);
		};
	}

	function retainNotEmpty () {
		return function retainNotEmpty (item) {
			return item != '';
		};
	}

	function retainUnique () {
		return function retainUnique (item, index, items) {
			return items.indexOf(item) == index;
		};
	}

	function semiColonCommaOrNewLineSorrundedByWhiteSpace () {
		return /\s*[;,\n]\s*/g;
	}

})(source, target, action, log);

answer;

Tested it with a field map setup in a transform map as below:

find_real_file.png

View solution in original post

5 REPLIES 5

The code does not implement the part where it is supposed to check whether the current list already contains the item to be added. I would go with a field map script as below:

answer = (function transformEntry (source, target, action, log) {
	var groupTypeUniqueValues = getGroupTypeUniqueValues('' + source.u_sys_user_group_type);

	return addTypeToNewGroup(action, groupTypeUniqueValues) ||
		addTypeToGroupWithNoType(target.type.nil(), groupTypeUniqueValues) ||
		addTypeToGroupWithType(getGroupTypes('' + target.type), groupTypeUniqueValues);

	function addMissingTypes () {
		return function (existingValues, newValue) {
			if (existingValues.indexOf(newValue) < 0)
				existingValues.push(newValue);

			return existingValues;
		};
	}

	function addTypeToGroupWithNoType (typeIsEmpty, value) {
		if (typeIsEmpty)
			return value.join(',');
	}

	function addTypeToGroupWithType (existingValues, newValues) {
		return newValues
			.reduce(addMissingTypes(), existingValues)
			.join(',');
	}

	function addTypeToNewGroup (action, value) {
		if (action == 'insert')
			return value.join(',');
	}

	function getGroupTypes (groupTypeList) {
		return groupTypeList
			.split(semiColonCommaOrNewLineSorrundedByWhiteSpace())
			.filter(retainNotEmpty())
			.filter(retainUnique());
	}

	function getGroupTypeUniqueValues (groupTypeList) {
		return groupTypeList
			.split(semiColonCommaOrNewLineSorrundedByWhiteSpace())
			.filter(retainNotEmpty())
			.map(getUniqueValueFromDisplayValue('sys_user_group_type'))
			.filter(retainNotEmpty())
			.filter(retainUnique());
	}

	function getUniqueValueFromDisplayValue (tableName) {
		return function getUniqueValueFromDisplayValue (displayValue) {
			return '' + GetIDValue(tableName, displayValue);
		};
	}

	function retainNotEmpty () {
		return function retainNotEmpty (item) {
			return item != '';
		};
	}

	function retainUnique () {
		return function retainUnique (item, index, items) {
			return items.indexOf(item) == index;
		};
	}

	function semiColonCommaOrNewLineSorrundedByWhiteSpace () {
		return /\s*[;,\n]\s*/g;
	}

})(source, target, action, log);

answer;

Tested it with a field map setup in a transform map as below:

find_real_file.png