
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-14-2025 06:08 AM
Hi, wondering if someone can help me out.
I am building a flow that when triggered, captures a company record and a user criteria record and pushes the sys_ids into their corresponding flow variables.
I then have an update action where i want it to assign the company to the user criteria record.
As the companies field in the user criteria record may already contain a company, I have added what i believe to be the right in line script into the update action.
The logs accurate show that it gets the sys_ids. But on update, it is just clearing the value!
Could anyone point me in the right direction?
(function execute(inputs, outputs) {
var userCriteriaSysId = fd_data.flow_var.user_criteria_sys_id;
var customerSysId = fd_data.flow_var.customer_sys_id;
gs.info("MTTA1: userCriteriaSysId: " + userCriteriaSysId);
gs.info("MTTA2: customerSysId: " + customerSysId);
var grUserCriteria = new GlideRecord('user_criteria');
if (grUserCriteria.get(userCriteriaSysId)) {
gs.info("MTTA3: User criteria record found.");
var currentCompanies = grUserCriteria.company.toString();
gs.info("MTTA4: currentCompanies: " + currentCompanies);
if (currentCompanies === '') {
gs.info("MTTA5: Companies list is empty. Returning: " + customerSysId);
return customerSysId; // List is empty, just add the ID
} else {
// Check if the customerSysId is already in the companies list
var companiesArray = currentCompanies.split(',');
if (companiesArray.indexOf(customerSysId) === -1) {
gs.info("MTTA6: Adding customerSysId to list. Returning: " + currentCompanies + ',' + customerSysId);
return currentCompanies + ',' + customerSysId; // List has values, add the new ID
} else {
gs.info("MTTA7: customerSysId already in list. Returning original list: " + currentCompanies);
return currentCompanies; // customerSysId already in the list, return original list.
}
}
} else {
gs.info("MTTA8: User criteria record NOT found.");
return ''; // Exit if record not found
}
})(inputs, outputs);
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-14-2025 07:49 AM
Thanks for spending the time, but I actually found the issue!
I mistakingly wrapped it all up in a function with inputs and outputs! I removed it from the function and just did the simple returns. Its now updating as expected
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-14-2025 06:35 AM
Hi M_iA,
Your script is returning values instead of actually updating the company field in the user_criteria record. In ServiceNow's server-side scripting, returning a value does not update the field. You need to use setValue() and update() to ensure the record is modified.
Fixed Script
Replace your script with the following to correctly update the record:
(function execute(inputs, outputs) {
var userCriteriaSysId = fd_data.flow_var.user_criteria_sys_id;
var customerSysId = fd_data.flow_var.customer_sys_id;
gs.info("MTTA1: userCriteriaSysId: " + userCriteriaSysId);
gs.info("MTTA2: customerSysId: " + customerSysId);
var grUserCriteria = new GlideRecord('user_criteria');
if (grUserCriteria.get(userCriteriaSysId)) {
gs.info("MTTA3: User criteria record found.");
var currentCompanies = grUserCriteria.getValue('company'); // Fetch existing companies
gs.info("MTTA4: currentCompanies: " + currentCompanies);
if (!currentCompanies) {
gs.info("MTTA5: Companies list is empty. Assigning: " + customerSysId);
grUserCriteria.setValue('company', customerSysId); // Set the new sys_id directly
} else {
// Convert to array, check for existing entry, and update field
var companiesArray = currentCompanies.split(',');
if (!companiesArray.includes(customerSysId)) {
gs.info("MTTA6: Adding customerSysId to list.");
companiesArray.push(customerSysId);
grUserCriteria.setValue('company', companiesArray.join(',')); // Update with new list
} else {
gs.info("MTTA7: customerSysId already exists. No update needed.");
}
}
grUserCriteria.update(); // Save the record!
gs.info("MTTA8: User criteria record updated successfully.");
} else {
gs.info("MTTA9: User criteria record NOT found.");
}
})(inputs, outputs);
Key Fixes
* Use setValue() and update() instead of return
* Retrieve the current field value correctly using getValue('company')
* Check for duplicates before appending the new sys_id
* Ensure the updated value is actually saved with update()
This should prevent the field from being cleared and correctly append new company records.
Kindly mark it as "Accepted Solution"/"helpful", as it resolves your query. Please press like button for the resolution provided.
With Regards,
Krishna Kumar M - Talk with AIT3ch
LinkedIn: https://www.linkedin.com/in/mkrishnak4/
YouTube: https://www.youtube.com/@KrishAIT3CH
Topmate: https://topmate.io/mkrishnak4 [ Connect for 1-1 Session]

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-14-2025 06:48 AM
its being updated by Flow Designer "update record" action so i was following the path that I just need to return these values? Is this not right?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-14-2025 07:24 AM
Hi M_iA,
You're right that in Flow Designer's "Update Record" action, you usually don't need to manually call setValue() and update(). Instead, you return the value that should be set in the field.
However, the issue is that returning a string in your script does not properly handle multi-reference fields (like company in sys_user_criteria).
Flow Designer treats the return value as a complete replacement for the field.
If currentCompanies contains a comma-separated list of sys_ids, simply returning a concatenated string might not be recognized correctly by ServiceNow as a Glide List update.
Multi-reference fields require an array format to avoid corruption.
How to Fix It?
Modify your script to return an array of sys_ids instead of a string:
(function execute(inputs, outputs) {
var userCriteriaSysId = fd_data.flow_var.user_criteria_sys_id;
var customerSysId = fd_data.flow_var.customer_sys_id;
gs.info("MTTA1: userCriteriaSysId: " + userCriteriaSysId);
gs.info("MTTA2: customerSysId: " + customerSysId);
var grUserCriteria = new GlideRecord('sys_user_criteria');
if (grUserCriteria.get(userCriteriaSysId)) {
gs.info("MTTA3: User criteria record found.");
var currentCompanies = grUserCriteria.getValue('company'); // Get existing companies list
gs.info("MTTA4: currentCompanies: " + currentCompanies);
var companiesArray = currentCompanies ? currentCompanies.split(',') : [];
// Check if the customerSysId is already in the list
if (!companiesArray.includes(customerSysId)) {
gs.info("MTTA5: Adding customerSysId to list.");
companiesArray.push(customerSysId);
} else {
gs.info("MTTA6: customerSysId already exists.");
}
gs.info("MTTA7: Returning updated list: " + JSON.stringify(companiesArray));
return companiesArray; // Return as an array instead of a string
} else {
gs.info("MTTA8: User criteria record NOT found.");
return [];
}
})(inputs, outputs);
* Returns an array instead of a string (Flow Designer handles it correctly).
* Preserves existing values and adds the new company without clearing the field.
* Prevents duplicates by checking if customerSysId already exists.
Conclusion
Use this script in Flow Designer's "Update Record" action.
Map the return value to the company field in the update action.
Test the flow by running it on a record with an existing company and ensure it appends the new value instead of overwriting.
This should solve your issue! Let me know if you need further assist, happy to help.
Kindly mark it as "Accepted Solution"/"helpful", as it resolves your query. Please press like button for the resolution provided.
With Regards,
Krishna Kumar M - Talk with AIT3ch
LinkedIn: https://www.linkedin.com/in/mkrishnak4/
YouTube: https://www.youtube.com/@KrishAIT3CH
Topmate: https://topmate.io/mkrishnak4 [ Connect for 1-1 Session]

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-14-2025 07:49 AM
Thanks for spending the time, but I actually found the issue!
I mistakingly wrapped it all up in a function with inputs and outputs! I removed it from the function and just did the simple returns. Its now updating as expected