- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2024 08:55 AM
I'm working on a Request Item (RITM) form. I have a UI action button that creates a new incident from RITM. I want all the variables captured in the RITM form to automatically populate the corresponding variables in the new incident form (all the question & answers captured in RITM I need to populate on Incident form)
How can I achieve this?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2024 11:09 PM
As I understand you want to create an incident and add all the variables from RITM to that incident in the variable formatter. To achieve this you need to add below script in your UI Action script,
var ritmSysID = current.getUniqueValue();
var now_GR = new GlideRecord('sc_req_item');
if (now_GR.get(ritmSysID)) {
var variables = now_GR.variables.getElements();
var values = []; // Array to store values and questions
for (var i = 0; i < variables.length; i++) {
var variable = variables[i];
var question = variable.getQuestion();
var displayValue = question.getDisplayValue(); // Get display value
values.push({
questionLabel: question.getLabel(), // Store question sys_id
value: displayValue // Store display value
});
}
var queAns = [];
for (var j = 0; j < values.length; j++) {
var grVarQuestion = new GlideRecord('item_option_new');
grVarQuestion.addQuery('question_text', values[j].questionLabel);
grVarQuestion.query();
if (grVarQuestion.next()) {
queAns.push({
questionSysID: grVarQuestion.sys_id.toString(),
value: values[j].value
});
}
}
createINC(now_GR, queAns);
action.setRedirectURL(current);
}
function createINC(now_GR, queAns) {
var inc = new GlideRecord("incident");
inc.initialize();
inc.caller_id = now_GR.request.requested_for;
inc.parent = ritmSysID;
var incSysID = inc.insert();
for (var key in queAns) {
var grQA = new GlideRecord("question_answer");
grQA.initialize();
grQA.table_name = "incident";
grQA.table_sys_id = incSysID;
grQA.question = queAns[key].questionSysID;
grQA.value = queAns[key].value;
grQA.insert();
}
}
This will definitely helps you to resolved your issue. Let me know in case you need to understand the flow or you can DM on LinkedIn.
If this solution resolves your query, kindly mark it as the accepted solution and give it a thumbs up.
Best Regards,
Krushna Birla
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-17-2024 12:09 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2024 09:21 AM
Try this code in your ui actin script. Adjust the names and format according to your requirements.
var scItemGr = new GlideRecord('sc_req_item');
scItemGr.get(g_form.getUniqueValue());
var variables = {};
if (scItemGr.isValidRecord()) {
scItemGr.variables.forEach(function(variable) {
variables[variable.getName()] = variable.getDisplayValue();
});
}
// Create a new incident
var incidentGr = new GlideRecord('incident');
incidentGr.initialize();
incidentGr.short_description = 'Incident created from RITM: ' + g_form.getUniqueValue();
// Set additional fields as needed
// Copy variables to incident
for (var varName in variables) {
incidentGr.setValue(varName, variables[varName]);
}
var incidentSysId = incidentGr.insert();
// Redirect to the new incident record
if (incidentSysId) {
gs.addInfoMessage('Incident ' + incidentGr.number + ' created successfully.');
action.setRedirectURL(incidentGr);
} else {
gs.addErrorMessage('Failed to create incident.');
}
})();
please mark this Helpful and Accepted Solution if this helps you. Your action will help me and the community in understanding same requirements.
Thanks & Regards
Deepak Sharma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2024 11:28 PM
Hi Deepak, I wanted the variables which captured under variable formator should be shown on incident.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2024 11:09 PM
As I understand you want to create an incident and add all the variables from RITM to that incident in the variable formatter. To achieve this you need to add below script in your UI Action script,
var ritmSysID = current.getUniqueValue();
var now_GR = new GlideRecord('sc_req_item');
if (now_GR.get(ritmSysID)) {
var variables = now_GR.variables.getElements();
var values = []; // Array to store values and questions
for (var i = 0; i < variables.length; i++) {
var variable = variables[i];
var question = variable.getQuestion();
var displayValue = question.getDisplayValue(); // Get display value
values.push({
questionLabel: question.getLabel(), // Store question sys_id
value: displayValue // Store display value
});
}
var queAns = [];
for (var j = 0; j < values.length; j++) {
var grVarQuestion = new GlideRecord('item_option_new');
grVarQuestion.addQuery('question_text', values[j].questionLabel);
grVarQuestion.query();
if (grVarQuestion.next()) {
queAns.push({
questionSysID: grVarQuestion.sys_id.toString(),
value: values[j].value
});
}
}
createINC(now_GR, queAns);
action.setRedirectURL(current);
}
function createINC(now_GR, queAns) {
var inc = new GlideRecord("incident");
inc.initialize();
inc.caller_id = now_GR.request.requested_for;
inc.parent = ritmSysID;
var incSysID = inc.insert();
for (var key in queAns) {
var grQA = new GlideRecord("question_answer");
grQA.initialize();
grQA.table_name = "incident";
grQA.table_sys_id = incSysID;
grQA.question = queAns[key].questionSysID;
grQA.value = queAns[key].value;
grQA.insert();
}
}
This will definitely helps you to resolved your issue. Let me know in case you need to understand the flow or you can DM on LinkedIn.
If this solution resolves your query, kindly mark it as the accepted solution and give it a thumbs up.
Best Regards,
Krushna Birla
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-17-2024 12:09 AM
Thank you Krushna, it worked for me.