- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2024 10:06 AM
With the below code only one value is updated in the list collector field.. Could you please help.. its urgent
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2024 12:41 PM
Hey @JPSS,
Try the following code:
(function transformRow(source, target, map, log, isUpdate) {
var name = source.u_watchlist.toString();
var split_names = name.split(","); //Breaking the input in a array
var userArr = [];;
for (i = 0; i < split_names.length; i++)
{
var gr = new GlideRecord('sys_user');
gr.get('email', split_names[i]);
if (gr.isValidRecord()) {
userArr.push(gr.getUniqueValue());
}
}
target.setValue('u_watch_list', userArr.join(','));
})(source, target, map, log, action === "update");
Cheers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2024 12:41 PM
Hey @JPSS,
Try the following code:
(function transformRow(source, target, map, log, isUpdate) {
var name = source.u_watchlist.toString();
var split_names = name.split(","); //Breaking the input in a array
var userArr = [];;
for (i = 0; i < split_names.length; i++)
{
var gr = new GlideRecord('sys_user');
gr.get('email', split_names[i]);
if (gr.isValidRecord()) {
userArr.push(gr.getUniqueValue());
}
}
target.setValue('u_watch_list', userArr.join(','));
})(source, target, map, log, action === "update");
Cheers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2024 01:29 PM
Hi @JPSS
In your ServiceNow transform script, it seems you want to populate a list collector field (u_watch_list) in the target record based on a comma-separated string of user emails (u_watchlist) from the source record. However, there are a couple of issues in your script:
You're using split_names[0] in your loop, which means you're always accessing the first element of the split_names array. Instead, you should use split_names[i] to access each element in the loop.
You're concatenating the sys_id values with , to the existing u_watch_list value. But this may result in an extra comma at the beginning. You should handle this case to avoid it.
Here's the corrected version of your transform script:
(function transformRow(source, target, map, log, isUpdate) {
var name = source.u_watchlist.toString();
var split_names = name.split(","); // Breaking the input into an array
var num_names = split_names.length;
for (var i = 0; i < num_names; i++) {
var gr = new GlideRecord('sys_user');
if (gr.get('email', split_names[i])) {
target.u_watch_list = (target.u_watch_list ? target.u_watch_list + ',' : '') + gr.getValue('sys_id');
}
}
})(source, target, map, log, action === "update");
This script iterates over each email in the u_watchlist field, retrieves the corresponding sys_id from the sys_user table, and appends it to the u_watch_list field in the target record. It handles the case where the u_watch_list field might already have values by checking if it's not empty before concatenating.
🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2024 01:43 PM
Hi @JPSS ,
Update below script according to your requirement:
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
var grp_type = [];
var inputArray = source.u_group_type.split(',');
var gtype = new ArrayUtil().unique(inputArray);
for (var i = 0; i < gtype.length; i++) {
var grpType = new GlideRecord('sys_user_group_type');
grpType.addQuery('name', gtype[i].trim());
grpType.query();
if (grpType.next()) {
grp_type.push(grpType.sys_id.toString());
}
}
var listArray = target.type.toString().split(',');
listArray = new ArrayUtil().concat(listArray, grp_type);
var uniqueArray = new ArrayUtil().unique(listArray);
target.type = uniqueArray.join(',');
})(source, map, log, target);
If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!
Thanks & Regards,
Sumanth Meda