Solved: UI Action : Create Incident button and need it to ... - ServiceNow Community

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

Bird1
Kilo 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

Vinayak Belgaon
Mega Guru
Mega Guru

Greetings!

For the condition, just below is fine.
Condition:  new GlideRecord("incident").canCreate();


as the UI action will not be visible if the user cannot create incident then you do not have to write extra validation in script.

script:
var inc = new GlideRecord("incident");
inc.initialize();
inc.caller_id = current.opened_for;
inc.short_description = current.short_description;
inc.description = current.u_description;
var incNew = inc.insert(); //assign the sys_id of new record to a variable

var url = 'https://'+ gs.getProperty('instance_name') + '.service-now.com/incident.do?sys_id=' + incNew ;
action.setRedirectURL(url);

 

Regards

Vinayak

Hello,

With your script, it could create incident but it still loads from the same page. What I need is once click on "create incident" button, it will open a new tab on browser and create incident.

 

find_real_file.png

Vinayak Belgaon
Mega Guru
Mega Guru

Ah i see, in that case, you can set the UI action as client and call your client function, note that incident will not be created, as it will be a -1 sys_id record, also you need to set the values of short_Description using client script.


find_real_file.png

Sourav16
Kilo Guru

Hi Bird,

Do you want the incident to be created manually after a new incident form opens in a new tab? or you want to create the incident automatically and open the incident in a new tab?

 

Thanks

Sourav