The CreatorCon Call for Content is officially open! Get started here.

UI Action : Create Incident button and need it to open a new tab instead of loading the same page.

Bird1
Mega Sage

Hello,

I am trying to create a UI action "create incident" button on the Interaction Form (not from Agent Workspace). With below script, it could create incident once clicking on a button but it will load from the same tab instead of open a new tab.

Could anyone please advise how to?

 

Condition:  new GlideRecord("incident").canCreate() && ((current.isNewRecord() && current.canCreate()) || !current.isNewRecord())

 

Script: 

 

var canCreateIncident = false;
if ((current.isNewRecord() && current.canCreate()) || (!current.isNewRecord() && current.canWrite()))
canCreateIncident = current.update();
else
canCreateIncident = true;

if (canCreateIncident) {
var inc = new GlideRecord("incident");
inc.initialize();
inc.caller_id = current.opened_for;
inc.short_description = current.short_description;
inc.description = current.u_description;
inc.insert();
//action.openGlideRecord(inc);
var url = 'https://'+ gs.getProperty('instance_name') + '.service-now.com/incident.do?sys_id=' + inc.sys_id;
action.setRedirectURL(url);

}

1 ACCEPTED SOLUTION

Sourav16
Kilo Guru

Hi Bird,

In that case you have to use g_navigation api which is a client side api in your ui action and you would require a client callable script include to create the incident.

UI action code:-

Make sure to check client checkbox in ui action and specify the function name in the onClick section

find_real_file.png 

 

find_real_file.png

function createIncident() {
	var recordDetails = {
		opened_for: g_form.getValue('opened_for'),
		short_description: g_form.getValue('short_description'),
		description: g_form.getValue('description')
	};
	var ga = new GlideAjax('IncidentUtil');
	ga.addParam('sysparm_name', 'createIncident');
	ga.addParam('sysparm_recordDetails', JSON.stringify(recordDetails));
	ga.getXMLAnswer(openIncRecord);
	
	function openIncRecord(answer) {
		if (answer) {
			g_navigation.open(answer, '_blank');
		}
		else {
			g_form.addErrorMessage("There was an issue");
		}
	}
}

 

Script include:- 

Make sure to check the client callable check box

find_real_file.png

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

	createIncident: function() {
		try {
			var recDetails = JSON.parse(this.getParameter('sysparm_recordDetails'));
			var incGr = new GlideRecord('incident');
			incGr.newRecord();
			incGr.setValue('caller_id', recDetails.caller_id);
			incGr.setValue('short_description', recDetails.short_description);
			incGr.setValue('description', recDetails.description);
			var incSysID = incGr.insert();
			
			return "/incident.do?sys_id=" + incSysID;
			
		}
		catch(ex) {
			gs.error("Incident creation error : " + ex);
			return null;
		}
	},

	type: 'IncidentUtil'
});

 

Thanks

Sourav

View solution in original post

14 REPLIES 14

Hi,

then you need to make that UI action as client side as with server side this is not possible.

Client checkbox - true

OnClick - showRecord()

Script:

function showRecord(){

var inc = new GlideRecord("incident");
	inc.initialize();
	inc.caller_id = g_form.getValue('opened_for');
	inc.short_description = g_form.getValue('short_description');
	inc.description = g_form.getValue('u_description');
	inc.insert();
	var url = '/incident.do?sys_id=' + inc.sys_id;
	g_navigation.open(url, '_blank');

}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thanks. It can open a new tab as expected but not sure why it said "Record not found"

 

find_real_file.png

@Bird 

It should work

did you check what came in inc.sys_id

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Sourav16
Kilo Guru

Hi Bird,

In that case you have to use g_navigation api which is a client side api in your ui action and you would require a client callable script include to create the incident.

UI action code:-

Make sure to check client checkbox in ui action and specify the function name in the onClick section

find_real_file.png 

 

find_real_file.png

function createIncident() {
	var recordDetails = {
		opened_for: g_form.getValue('opened_for'),
		short_description: g_form.getValue('short_description'),
		description: g_form.getValue('description')
	};
	var ga = new GlideAjax('IncidentUtil');
	ga.addParam('sysparm_name', 'createIncident');
	ga.addParam('sysparm_recordDetails', JSON.stringify(recordDetails));
	ga.getXMLAnswer(openIncRecord);
	
	function openIncRecord(answer) {
		if (answer) {
			g_navigation.open(answer, '_blank');
		}
		else {
			g_form.addErrorMessage("There was an issue");
		}
	}
}

 

Script include:- 

Make sure to check the client callable check box

find_real_file.png

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

	createIncident: function() {
		try {
			var recDetails = JSON.parse(this.getParameter('sysparm_recordDetails'));
			var incGr = new GlideRecord('incident');
			incGr.newRecord();
			incGr.setValue('caller_id', recDetails.caller_id);
			incGr.setValue('short_description', recDetails.short_description);
			incGr.setValue('description', recDetails.description);
			var incSysID = incGr.insert();
			
			return "/incident.do?sys_id=" + incSysID;
			
		}
		catch(ex) {
			gs.error("Incident creation error : " + ex);
			return null;
		}
	},

	type: 'IncidentUtil'
});

 

Thanks

Sourav

This one is workable. Thanks.