Check for other incidents already attached to a change when adding to caused_by change

Susan Davidson
Giga Guru

We are ending up with multiple incidents attached to changes via the caused_by field leading to more than one "master" ticket in the service desk.

 

I would like to put something in place that when the agents go to the caused_by field on the incident and search for a change number and select it then it would tell them if there are already other incidents that have had that same change number entered into the caused_by field and what those incident numbers are.

I cannot wrap my head around where to start with that one. Any ideas?

1 ACCEPTED SOLUTION

POOJA SINGH18
Mega Guru

Hi Susan,

 

One of the best practice of achieve your solution is to make a Ajax call. Writing GlideRecord in client script is not a good practice in ServiceNow. 

So write a function in a Script Include, follow below:

POOJASINGH18_0-1689573813264.png

 

and then make a Ajax call using "OnChange" Client script.


POOJASINGH18_1-1689573898505.png


The end result will be something like below:

POOJASINGH18_2-1689573956666.png

 

Hope it helps and please mark helpful if it solves the issue.

Thanks,

Pooja Singh

 

View solution in original post

6 REPLIES 6

Bert_c1
Kilo Patron

Hi Susan,

 

Pooja makes a good point on using GlideRecord in a client script. while it can work the better approach is using GlideAjax.  I was able to do that, all incidents are returned and shown in the popup. I am not aware why this approach, using GlideAjax, wouldn't work on "workspaces...".  I tried using the Agent workspace and found it doesn't work there.  I'm not familiar with that Feature/UI and can't comment on that aspect. I can share my code (somewhat different than above and works outside of 'Workspace') so you can cut-and-paste in your own Client Script and Script Include if desired.

Bert_c1
Kilo Patron

@Susan Davidson 

 

I've read KB0827632, and made a change to my Client Script, it works in Agent Workspace too now.

 

Screenshot 2023-07-17 180015.png

 

UI Type being set to 'All' worked. script:

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

   //Type appropriate comment here, and begin script below
   var ga = new GlideAjax('GetChgIncidents'); // GetChgIncidents is the script include name 
   ga.addParam('sysparm_name','incidentList'); // is the function in the script include that we're calling 
   var chg = g_form.getValue("caused_by");
   ga.addParam('sysparm_chg_sysid', chg); 

   /* CallGetChgIncidents and use the callback function chgParse() to return the result when ready */
   ga.getXMLAnswer(chgParse);  		
}

// callback function for returning the result from the script include
function chgParse(response) {
	if (response.length > 2)
		alert("Incidents with same Caused by Change: " + response);
}

 

Script Include:

Screenshot 2023-07-17 180309.png

Script:

 

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

	incidentList: function() {
        var chgSysID = this.getParameter("sysparm_chg_sysid");
		var result = "";

//		gs.info("GetChgIncidents Checking for CHG: " + chgSysID);

		// Build the payload. You can return additional data if needed.
		var incr = new GlideRecord('incident');
		incr.addQuery('caused_by', chgSysID);
		incr.query();
		var rowCnt = incr.getRowCount();
		var loopCnt = 0;
		if (incr.getRowCount() > 0) {
			while (incr.next()) {
				loopCnt++;
//				gs.info("GetChgIncidents INC: " + incr.number +" is also caused by the same.");
				if (loopCnt < rowCnt)
					result += incr.number + ',';
				else
					result += incr.number;
			}
//			gs.info("GetChgIncidents ressult: " + result);
		}
		else
			result = '';
		
        return JSON.stringify(result);
    },

    type: 'GetChgIncidents'
});

 

Try the above.