Prepopulate multirow variable with form variable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2024 06:57 AM
Hi,
I have a variable inside a multirow variable set named 'user type' i have a similar variable in the form outside of the multirow. I want to make it so, once i select the user_type outside of the multirow, and once i open the mrv this variable is auto-populated with the previous response. Any idea how i can do this?
Thanks,
Miguel Santos Palmeira
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2024 07:01 AM
To achieve this functionality in ServiceNow, you can use Client Scripts or UI Policies. Here's a general guide on how you can implement this:
-
Client Script Approach:
- Write a client script that triggers when the form is loaded.
- Check if the user type field outside the multi-row variable (MRV) has a value.
- If it does, then set the value of the corresponding user type field inside the MRV to that value.
-
UI Policy Approach:
- Create a UI policy that runs on the MRV field.
- Conditionally set the value of the MRV field to the value of the user type field outside the MRV when certain conditions are met.
or -
function onLoad() {
// Get the value of the user type field outside the MRV
var userTypeOutsideMRV = g_form.getValue('user_type_outside_mrv');// Set the value of the user type field inside the MRV
g_form.setValue('user_type_inside_mrv', userTypeOutsideMRV);
}
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
YouTube: https://www.youtube.com/@learnservicenowwithravi
LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2024 07:13 AM
Hi,
Thanks for the quick answer,
I've tried this approach however the script doesn't seem to reach the outside variable if its applied to the variable set.
I also tried it with a script that applies to the regular form and although in this case it can read the value correctly, it doesn't seem to be able to change the value of the variable.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2024 07:20 AM
can you try the below
function onLoad() {
// Get the value of the user type field outside the MRV
var userTypeOutsideMRV = g_form.getValue('user_type_outside_mrv');
// Set the value of the user type field inside the MRV
var userTypeIdInsideMRV = 'user_type_inside_mrv'; // Change this to the actual field name
setUserTypeInsideMRV(userTypeIdInsideMRV, userTypeOutsideMRV);
}
function setUserTypeInsideMRV(fieldId, value) {
var ga = new GlideAjax('ScriptIncludeHelper');
ga.addParam('sysparm_name', 'setUserTypeInsideMRV');
ga.addParam('field_id', fieldId);
ga.addParam('value', value);
ga.getXMLAnswer(function(response) {
// Handle the response if needed
// For example, you can log the response to the browser console
console.log(response);
});
}
-----------------
var ScriptIncludeHelper = Class.create();
ScriptIncludeHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {
setUserTypeInsideMRV: function() {
var fieldId = this.getParameter('field_id');
var value = this.getParameter('value');
// Set the value of the field inside the MRV
var gr = new GlideRecord('variable_set_table_name'); // Replace 'variable_set_table_name' with the actual table name of your variable set
// Add a condition if needed to narrow down the record you want to update
gr.setValue(fieldId, value);
gr.update();
return 'Field updated successfully';
}
});
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
YouTube: https://www.youtube.com/@learnservicenowwithravi
LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2024 08:31 AM
Hi, thank you for the quick reply,
I found a solution on:
Solved: Pre populate a variable in a multirow based on a v... - ServiceNow Community