- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-01-2022 04:11 PM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-04-2022 06:48 AM
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:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-01-2022 05:35 PM
Hi, unfortunately your question is not very clear.
If a field is not mapped in your transform field mappings, then it would not be automatically updated and you will retain the existing value, unless you had a before transform script configured to set a value.
If using a before transform script you can define when to populate a field, and or use scripts to map your values. IE
//Populate on Insert
if(action == 'insert') {
target.fieldName = source.fieldName;
}
//Populate on update
if(action == 'update') {
target.fieldName = source.fieldName;
}
//Or a crude check to see if the field of an existing record is empty
if(target.fieldName == '' && action == 'update') {
target.fieldName = source.fieldName;
}
https://docs.servicenow.com/bundle/sandiego-platform-administration/page/script/server-scripting/reference/r_TransformationScriptVariables.html
If this is not the information you are looking for then you can update this thread with clear and specific details so that the forum can better understand your requirement.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-01-2022 05:46 PM
Hello Tony, thanks for your reply.
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 insert
Appreciate all the help. Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-01-2022 05:52 PM
For sure you can do it. Probably implementing a logic along the lines of:
- if the current record is beind creted (as opposed to updated), just return the incoming value
- if the current record is being updated:
- fetch existing values (target.type)
- split those by comma (,)
- filter out empty elements
- eliminate duplicates
- push the sys_id of the new value into the array - if not already there
- return the new array joined by comma (,).
This will also work in a more simple manner, but doing it as above will also clean up the type list. The reduced logic would be:
- fetch existing values
- append the sys_id of the new value - if the string is empty or
- append a comma and the sys_id of the new value - if not in the string already and the string is not empty
- return the new string.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-03-2022 06:01 PM