Mapping RITM Variable Formatter to Incident on Create UI Action

PRATIKSHA PAWAR
Tera Expert

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?

2 ACCEPTED SOLUTIONS

Krushna R Birla
Kilo Sage

Hi @PRATIKSHA PAWAR 

 

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

View solution in original post

Thank you Krushna, it worked for me.

View solution in original post

4 REPLIES 4

Deepak Shaerma
Kilo Sage

Hi @PRATIKSHA PAWAR 

 

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

Hi Deepak, I wanted the variables which captured under variable formator should be shown on incident.

 

Krushna R Birla
Kilo Sage

Hi @PRATIKSHA PAWAR 

 

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

Thank you Krushna, it worked for me.