UI Action to assign Incidents to Problem

Tom Lapkowicz
Tera Expert

I would like to create a list choice UI Action on the Incidents table where a user can select multiple Incidents, then select "Assign to Problem" from the Actions on Selected Rows drop-down and then a List Choice box of all Problems will appear for the user to select a Problem and then the Incident.Problem value is updated with the Problem selected.  Is this do-able?

 

See attached screen shot for more details (I created a UI Action called Assign to Problem but there's no code in it currently)

 

Thank you.

6 REPLIES 6

Runjay Patel
Giga Sage

Hi @Tom Lapkowicz ,

 

Yes, You can it.

Do the following.

  1. Create UI action and make it client callable to run client side script.
  2. Create one ui page and design to select problem record.
  3. Use make of glideModel() to render ui page.
  4. Once user select problem on pop up and click on save call server side script.
  5. In server side UI action get the problem sys_id and update it to problem filed of incident.

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

Part 2. In this video i have talked about overview on ServiceNow platform/tool. How you can opt for personal dev instance (PDI)? how to login in ServiceNow instance and navigation to OOB modules. For document please visit: https://servicenowwithrunjay.com/ Follow Facebook page for latest update on

Thank you @Runjay Patel .

 

I'm not much of a coder, but using Chat GPT I came up with the coding below but it still doesn't seem to be working (it's not even prompting me for the Problem).  Do you happen to see any glaring issues with the code?  Thank you.

 

Script Includes (Client Callable box is checked): 

 

var AssignIncidentsToProblem = Class.create();
AssignIncidentsToProblem.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    process: function() {
        var problemId = this.getParameter('sys_id');
        var incidentIds = this.getParameter('incident_ids');

        // Validate input
        if (!problemId || !incidentIds) {
            return "Problem Sys ID and selected incident IDs are required.";
        }

        // Validate the Problem record
        var problemRecord = new GlideRecord('problem');
        if (!problemRecord.get(problemId)) {
            return "Invalid Problem Sys ID. Please verify.";
        }

        // Process each selected incident
        var incidentRecord = new GlideRecord('incident');
        incidentRecord.addQuery('sys_id', 'IN', incidentIds);
        incidentRecord.query();

        var count = 0;
        while (incidentRecord.next()) {
            incidentRecord.problem_id = problemId;  // Set the Problem ID
            incidentRecord.update();
            count++;
        }

        if (count > 0) {
            return count + " incident(s) successfully assigned to Problem: " + problemRecord.number;
        } else {
            return "No incidents were updated.";
        }
    }
});
 
UI Action Script:
 
function onActionClick() {
    // Get selected incidents
    var selectedIds = GlideList2.get('incident').getChecked();
    if (selectedIds.length === 0) {
        alert("Please select at least one incident.");
        return;
    }

    // Create a GlideModal and set its content
    var modal = new GlideModal('assign_problem_modal');
    modal.setTitle('Assign Problem to Selected Incidents');
    modal.setBody('<div><label for="problem_id">Problem Sys ID:</label><input type="text" id="problem_id" class="form-control" placeholder="Enter Problem Sys ID"></div>');
   
    // Modal Submit handler
    modal.on('submit', function () {
        var problemId = document.getElementById('problem_id').value;

        if (!problemId) {
            alert("Please enter a valid Problem Sys ID.");
            return;
        }

        // Send the problem ID and selected incident IDs to the server-side script
        var ga = new GlideAjax('AssignIncidentsToProblem');
        ga.addParam('sys_id', problemId);
        ga.addParam('incident_ids', selectedIds.join(','));
        ga.getXMLAnswer(function(response) {
            var message = response.responseXML.documentElement.getAttribute("answer");
            alert(message);
            location.reload(); // Optionally reload the page
        });
    });

    modal.render(); // Show the modal
}

Hi @Tom Lapkowicz ,

 

I have spent lots of time to make this work for you. I have tested it, working fine. Just create below script as it is.

 

1. ui page

name: assign_problem_modal

HTML code

 

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">

<g:ui_form onsubmit="return invokeConfirmCallBack('Save');">
<div>
	<label for="problem_id">Problem:</label>
	<!-- <input type="text" id="problem_id" class="form-control" placeholder="Enter Problem Sys ID"></input> -->

<g:ui_reference name="QUERY:active=true" id="problem_id" table="problem" completer="AJAXTableCompleter" />
	

</div>




<div class="modal-footer">
				   
	<span class="pull-right">
		<button class="btn btn-default" id="cancel_button" onclick="invokePromptCallBack('cancel')" style="min-width: 5em;" title="" type="submit">
			Cancel
		</button>
		<button class="btn btn-primary" id="ok_button" onclick="invokePromptCallBack('Save')" style="min-width: 5em;" title="" type="submit">
			Save
		</button>
	</span>
</div>

</g:ui_form>
</j:jelly>

 

Client Script Code:

 

function invokePromptCallBack(type) {

    var gdw = GlideDialogWindow.get();
    if (type == 'Save') {

        var selectedIds = gdw.getPreference('selectedIds');
        var problemId = document.getElementById('problem_id').value;
        alert(selectedIds + problemId);

        if (!problemId) {
            alert("Please enter a valid Problem.");
            return;
        }

        var ga = new GlideAjax('AssignIncidentsToProblem');
        ga.addParam('sysparm_name', 'processInc');
        ga.addParam('sysparam_sys_id', problemId);
        ga.addParam('sysparam_inc_ids', selectedIds);
        ga.getXML(serverResponse);



    }


    gdw.destroy();
    return false;
}

function serverResponse(response) {
    var answer = response.responseXML.documentElement.getAttribute('answer');
alert(answer);
	location.reload();
  
}

 

 

2. UI Action

name: Assign to Problem

action name: assign_to_problem

code

 

function callMe() {

    var selectedIds = GlideList2.get('incident').getChecked();
    if (selectedIds.length === 0) {
        alert("Please select at least one incident.");
        return;
    }

    var modal = new GlideModal('assign_problem_modal');
    modal.setTitle('Assign Problem to Selected Incidents');
	modal.setPreference('selectedIds', selectedIds);
    modal.render(); 
    }

 

RunjayPatel_0-1731070425797.pngRunjayPatel_1-1731070455428.png

 

3. Script Include

name: AssignIncidentsToProblem 

code

 

var AssignIncidentsToProblem = Class.create();
AssignIncidentsToProblem.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    processInc: function() {
        var problemId = this.getParameter('sysparam_sys_id');
        var incidentIds = this.getParameter('sysparam_inc_ids');

        gs.log('Runjayp' + problemId + ' : ' + incidentIds);
        // Process each selected incident
        var incidentRecord = new GlideRecord('incident');
        incidentRecord.addQuery('sys_id', 'IN', incidentIds);
        incidentRecord.query();

        var count = 0;
        while (incidentRecord.next()) {
            incidentRecord.problem_id = problemId; // Set the Problem ID
            incidentRecord.update();
            count++;
        }

        if (count > 0) {
            return count + " incident(s) successfully assigned to Problem: ";
        } else {
            return "No incidents were updated.";
        }
    },


    type: 'AssignIncidentsToProblem '
});

 

 

RunjayPatel_2-1731070551456.png

 

 

Output

RunjayPatel_1-1731070775810.png

 

RunjayPatel_0-1731070717239.png

 

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

 

 

 

 

 

Hi @Runjay Patel .  Thank you so much for your help on this!  I set up the steps but apparently did not do something correct - "Assign to Problem" shows in the Actions on Selected Rows drop-down and it prompts to select the Problem, but then once a Problem is selected it goes to another screen asking for the Problem to be selected.  I've attached screen shots of the UI Page, UI Action and Script Includes and the error.  Any ideas what I'm doing wrong?  Thanks!