- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2025 06:38 AM
Hi friendly crowd,
I have thought nut to crack, and I hope someone might help me with it
We have a change request from where we have many additional questions that are attached to the main change request from as a variables.
I also have a Copy Change button on the back end form, to help submitting changes based on the previous same change. However I came across the problem, when the form is submitted via record producer and have variables attached, I can't copy these variables if I need to use Copy button, It there a way to duplicate the variables and reattach them at the new record?
Many thanks for meaningful answers
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2025 08:44 AM
Managed to do it without the use of Catalog UI policies, but with some exclusions, this is my current UI Action to Copy Change (with variables)
function OnCopyChangeClick() {
try {
// Get the Sys ID of the source change request
var srcSysId = g_form.getUniqueValue();
if (!srcSysId) {
g_form.addErrorMessage('Source record not found.');
return;
}
// Create a new change request record
var newChange = new GlideRecord('change_request');
newChange.initialize();
// Copy relevant fields from the current form using g_form
newChange.short_description = g_form.getValue('short_description') || 'Copy of Change Request';
newChange.category = g_form.getValue('category') || '';
newChange.priority = g_form.getValue('priority') || '';
newChange.risk = g_form.getValue('risk') || '';
newChange.impact = g_form.getValue('impact') || '';
newChange.type = g_form.getValue('type') || '';
newChange.u_location = g_form.getValue('u_location') || '';
newChange.assignment_group = g_form.getValue('assignment_group') || '';
newChange.assigned_to = g_form.getValue('assigned_to') || '';
newChange.description = g_form.getValue('description') || '';
newChange.justification = g_form.getValue('justification') || '';
newChange.change_plan = g_form.getValue('change_plan') || '';
newChange.risk_impact_analysis = g_form.getValue('risk_impact_analysis') || '';
newChange.backout_plan = g_form.getValue('backout_plan') || '';
newChange.test_plan = g_form.getValue('test_plan') || '';
// Insert the new change request record
var newSysId = newChange.insert();
if (!newSysId) {
g_form.addErrorMessage('Failed to create new change request record.');
return;
}
// Get the question answers from the original change request
var qaGR = new GlideRecord('question_answer');
qaGR.addQuery('table_name', 'change_request');
qaGR.addQuery('table_sys_id', srcSysId);
qaGR.query();
// If there are no question answers to copy, return
if (!qaGR.hasNext()) {
g_form.addErrorMessage('No question answers found to copy.');
return;
}
// List of variables to exclude from copying
var excludedVariables = [
'additional_questions', 'initial_note', 'u_important_info', 'note', 'risk', 'impact',
'justification', 'change_plan', 'backout_plan', 'risk_impact_analysis', 'test_plan',
'u_implementer_s_contact_details', 'u_services_affected', 'user_has_itil_role',
'description', 'u_network_monitoring_pause_details', 'type', 'change_request_created_by'
];
// Process each question answer and insert it into the new change request
var copiedVariables = [];
while (qaGR.next()) {
var questionGR = new GlideRecord('question');
if (questionGR.get(qaGR.question)) {
// Only copy variables that are not excluded and have a non-empty value
if (excludedVariables.indexOf(questionGR.name) === -1 && qaGR.value && qaGR.value.trim() !== '') {
var newQA = new GlideRecord('question_answer');
newQA.initialize();
newQA.table_name = 'change_request';
newQA.table_sys_id = newSysId;
newQA.question = qaGR.question;
newQA.value = qaGR.value;
newQA.insert();
// Store the copied variable for the message
copiedVariables.push(questionGR.name);
}
}
}
// Generate the URL for the new change request
var newRecordUrl = '/change_request.do?sys_id=' + newSysId;
// Prepare the success message with a link to the new record
var message = 'Change request copied successfully. <a href="' + newRecordUrl + '" target="_self">Click here</a> to view the new Change Request.<br>';
// Show the success message
g_form.addInfoMessage(message);
} catch (e) {
// Handle any errors that occur during the process
g_form.addErrorMessage('An unexpected error occurred: ' + e.message);
gs.error('Error in OnCopyChangeClick: ' + e.message);
}
}
Two things I did not manage to do it, as it was breaking script, sort the added variables a-z, and redirect from to anew record at the end. Instead I decided to put link to a new CR in the message o top of the form, when coping is completed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2025 11:39 AM - edited 03-05-2025 02:28 PM
Hello @mattmg
I would request to raise a HI Case with ServiceNow to find it whether it is possible or not
I would also recommend you to run below background script to identify whether you are able to print variables from the change request form or not. If not then I believe it is not possible (may be I am incorrect) and if it is possible per ServiceNow's response on HI Ticket then, please do share here so that we all will learn something new(if not known already).
var grCR = new GlideRecord('change_request');
grCR.addQuery('sys_id', 'sys_id_of_your_change_reqeuest; // enter the sys_id of change request
grCR.query();
if(grCR.next()){
for (var key in grCR) {
gs.info('Attribute Name: ' + key + ' Value: ' + grCR[key]);
}
}
Screenshots:
Change Request showing one dummy variable in the native UI Change Request form:
Output of above background script not displaying variable name and it's value:
Hope that helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2025 12:33 PM
it does print, but it prints all available variables, even if they are hidden or unpopulated
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2025 02:26 PM - edited 03-05-2025 02:27 PM
Hello @mattmg
You can try below Background script to create new Change Request from an existing Change Request to find out whether variables are copied or not.
If there are certain variables to exclude then you can exclude the way I did for sys_id and number.
var grCR = new GlideRecord('change_request');
grCR.get('sys_id'); // enter the sys_id of change request
if (grCR.isValidRecord()) {
var newCR = new GlideRecord('change_request');
newCR.initialize();
for (var key in grCR) {
if (grCR[key] !== undefined && key !== 'sys_id' && key !== 'number') { // Exclude sys_id and number
newCR[key] = grCR[key]; // Copy all values
}
}
newCR.insert();
gs.info('New CR ' + newCR.number);
}
Here is the UI Action Script
if (current.isValidRecord()) {
var newCR = new GlideRecord('change_request');
newCR.initialize();
for (var key in current) {
if (current[key] !== undefined && key !== 'sys_id' && key !== 'number') { // Excluding sys_id and number
newCR[key] = current[key]; // Copy field values
}
}
var newSysId = newCR.insert();
if (newSysId) {
action.setRedirectURL(newCR); // Redirect to the new Change Request
gs.log('New CR# ' + newCR.number);
} else {
gs.addErrorMessage('Failed to create new Change Request.');
}
}
If you want to hide some variables always on the Change Request form then you can have create catalog UI policy on this record producer with only "Applies on the Target Record" as selected and Catalog UI policy actions Visible False for all those Variables which needs to be hidden.
Hope that helps!
Hope that helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2025 08:44 AM
Managed to do it without the use of Catalog UI policies, but with some exclusions, this is my current UI Action to Copy Change (with variables)
function OnCopyChangeClick() {
try {
// Get the Sys ID of the source change request
var srcSysId = g_form.getUniqueValue();
if (!srcSysId) {
g_form.addErrorMessage('Source record not found.');
return;
}
// Create a new change request record
var newChange = new GlideRecord('change_request');
newChange.initialize();
// Copy relevant fields from the current form using g_form
newChange.short_description = g_form.getValue('short_description') || 'Copy of Change Request';
newChange.category = g_form.getValue('category') || '';
newChange.priority = g_form.getValue('priority') || '';
newChange.risk = g_form.getValue('risk') || '';
newChange.impact = g_form.getValue('impact') || '';
newChange.type = g_form.getValue('type') || '';
newChange.u_location = g_form.getValue('u_location') || '';
newChange.assignment_group = g_form.getValue('assignment_group') || '';
newChange.assigned_to = g_form.getValue('assigned_to') || '';
newChange.description = g_form.getValue('description') || '';
newChange.justification = g_form.getValue('justification') || '';
newChange.change_plan = g_form.getValue('change_plan') || '';
newChange.risk_impact_analysis = g_form.getValue('risk_impact_analysis') || '';
newChange.backout_plan = g_form.getValue('backout_plan') || '';
newChange.test_plan = g_form.getValue('test_plan') || '';
// Insert the new change request record
var newSysId = newChange.insert();
if (!newSysId) {
g_form.addErrorMessage('Failed to create new change request record.');
return;
}
// Get the question answers from the original change request
var qaGR = new GlideRecord('question_answer');
qaGR.addQuery('table_name', 'change_request');
qaGR.addQuery('table_sys_id', srcSysId);
qaGR.query();
// If there are no question answers to copy, return
if (!qaGR.hasNext()) {
g_form.addErrorMessage('No question answers found to copy.');
return;
}
// List of variables to exclude from copying
var excludedVariables = [
'additional_questions', 'initial_note', 'u_important_info', 'note', 'risk', 'impact',
'justification', 'change_plan', 'backout_plan', 'risk_impact_analysis', 'test_plan',
'u_implementer_s_contact_details', 'u_services_affected', 'user_has_itil_role',
'description', 'u_network_monitoring_pause_details', 'type', 'change_request_created_by'
];
// Process each question answer and insert it into the new change request
var copiedVariables = [];
while (qaGR.next()) {
var questionGR = new GlideRecord('question');
if (questionGR.get(qaGR.question)) {
// Only copy variables that are not excluded and have a non-empty value
if (excludedVariables.indexOf(questionGR.name) === -1 && qaGR.value && qaGR.value.trim() !== '') {
var newQA = new GlideRecord('question_answer');
newQA.initialize();
newQA.table_name = 'change_request';
newQA.table_sys_id = newSysId;
newQA.question = qaGR.question;
newQA.value = qaGR.value;
newQA.insert();
// Store the copied variable for the message
copiedVariables.push(questionGR.name);
}
}
}
// Generate the URL for the new change request
var newRecordUrl = '/change_request.do?sys_id=' + newSysId;
// Prepare the success message with a link to the new record
var message = 'Change request copied successfully. <a href="' + newRecordUrl + '" target="_self">Click here</a> to view the new Change Request.<br>';
// Show the success message
g_form.addInfoMessage(message);
} catch (e) {
// Handle any errors that occur during the process
g_form.addErrorMessage('An unexpected error occurred: ' + e.message);
gs.error('Error in OnCopyChangeClick: ' + e.message);
}
}
Two things I did not manage to do it, as it was breaking script, sort the added variables a-z, and redirect from to anew record at the end. Instead I decided to put link to a new CR in the message o top of the form, when coping is completed