Create Normal change via form context menu Ui action

Krishna142
Tera Contributor

In the form context menu, I need to add UI actions for Standard change, Normal change, and Emergency change.

1 ACCEPTED SOLUTION

@Krishna142 You can check OOTB UI Actions for these on Incident table and with slight changes create a new UI Action for sc_task table.

 

 

Create a new reference field (references change_request table) called with Label Change Request  and name as u_rfc.

 

AnveshKumarM_0-1701863802788.png

 

 

 

Normal Change:

  1. Open the link https://YOUR_INSTANCE_NAME.service-now.com/sys_ui_action.do?sys_id=30c9566dc61122740030e173564c1c74
  2. From context menu click on Insert and Stay
  3. Change the table name to sc_task
  4. Then condition to current.state != 3 && current.state != 4 && current.state != 7 && gs.hasRole("itil,sn_change_write") && gs.fieldExists('sc_task', 'u_rfc') && current.u_rfc.nil()
  5. Then in the script field use the following code and save the record.
(function(current, previous, gs, action) {
	var stdChgCatalogActive = GlidePluginManager.isActive("com.snc.change_management.standard_change_catalog");

	if (stdChgCatalogActive) {
		var target = {};
		target.table = current.getTableName();
		target.sysid = current.getUniqueValue();
		target.field = 'u_rfc';
		try {
			target.isWorkspace = (typeof RP == 'undefined');
		}
		catch (err) {
			target.isWorkspace = false;
		}
		gs.getSession().putProperty('change_link', target);
	}

	var changeRequest = ChangeRequest.newNormal();
	changeRequest.setValue("short_description", current.short_description);
	changeRequest.setValue("description", current.description);
	changeRequest.setValue("cmdb_ci", current.cmdb_ci);
	if (changeRequest.hasValidChoice('priority', current.priority))
		changeRequest.setValue("priority", current.priority);
	changeRequest.setValue("sys_domain", current.sys_domain);
	changeRequest.setValue("company", current.company);
	var sysId = changeRequest.insert();

	if (!stdChgCatalogActive) {
		current.u_rfc = sysId;
		current.update();
		var sctUrl = "<a href='" + current.getLink(true) + "'>" + current.getDisplayValue() + "</a>";
		gs.addInfoMessage(gs.getMessage("Normal Change {0} created from {1}", [changeRequest.getValue("number"), sctUrl]));
	}

	action.setReturnURL(current);
	action.setRedirectURL(changeRequest.getGlideRecord());
})(current, previous, gs, action);

 

Standard Change:

 

Repeat the same steps as in Normal change, with the following script changes.

Condition: current.state != 3 && current.state != 4 && current.state != 7 && gs.hasRole("itil,sn_change_write") && gs.fieldExists('sc_task', 'u_rfc') && current.u_rfc.nil()

 

Script:

action.setRedirectURL(new StdChangeUtils().getURLForTask(current, 'u_rfc'));

 

Emergency Change:

 

Repeat the same steps as in Normal change, with the following script changes.

Condition: current.state != 3 && current.state != 4 && current.state != 7 && gs.hasRole("itil,sn_change_write") && gs.fieldExists('sc_task', 'u_rfc') && current.u_rfc.nil()

 

Script:

(function(current, previous, gs, action) {
	var target = {};
	target.table = current.getTableName();
	target.sysid = current.getUniqueValue();
	target.field = 'u_rfc';
	try {
		target.isWorkspace = (typeof RP == 'undefined');
	}
	catch (err) {
		target.isWorkspace = false;
	}

	gs.getSession().putProperty('change_link', target);

	var changeRequest = ChangeRequest.newEmergency();
	changeRequest.setValue("short_description", current.short_description);
	changeRequest.setValue("description", current.description);
	changeRequest.setValue("cmdb_ci", current.cmdb_ci);
	if (changeRequest.hasValidChoice('priority', current.priority))
		changeRequest.setValue("priority", current.priority);
	changeRequest.setValue("sys_domain", current.sys_domain);
	changeRequest.setValue("company", current.company);
	changeRequest.insert();

	action.setReturnURL(current);
	action.setRedirectURL(changeRequest.getGlideRecord());

})(current, previous, gs, action);

 

That's it!

 

Please mark my answer helpful and accept as a solution if it helped 👍✔️

Thanks,
Anvesh

View solution in original post

6 REPLIES 6

AnveshKumar M
Tera Sage
Tera Sage

Hi @Krishna142 

 

On which table form you want these UI Actions/Form Context Menus?

Thanks,
Anvesh

Hello Anvesh,

Greetings!!!

 

On "sc_task" table form, I need to implement these UI actions/Form context menus.

 

Thanks,

Krishna.

@Krishna142 You can check OOTB UI Actions for these on Incident table and with slight changes create a new UI Action for sc_task table.

 

 

Create a new reference field (references change_request table) called with Label Change Request  and name as u_rfc.

 

AnveshKumarM_0-1701863802788.png

 

 

 

Normal Change:

  1. Open the link https://YOUR_INSTANCE_NAME.service-now.com/sys_ui_action.do?sys_id=30c9566dc61122740030e173564c1c74
  2. From context menu click on Insert and Stay
  3. Change the table name to sc_task
  4. Then condition to current.state != 3 && current.state != 4 && current.state != 7 && gs.hasRole("itil,sn_change_write") && gs.fieldExists('sc_task', 'u_rfc') && current.u_rfc.nil()
  5. Then in the script field use the following code and save the record.
(function(current, previous, gs, action) {
	var stdChgCatalogActive = GlidePluginManager.isActive("com.snc.change_management.standard_change_catalog");

	if (stdChgCatalogActive) {
		var target = {};
		target.table = current.getTableName();
		target.sysid = current.getUniqueValue();
		target.field = 'u_rfc';
		try {
			target.isWorkspace = (typeof RP == 'undefined');
		}
		catch (err) {
			target.isWorkspace = false;
		}
		gs.getSession().putProperty('change_link', target);
	}

	var changeRequest = ChangeRequest.newNormal();
	changeRequest.setValue("short_description", current.short_description);
	changeRequest.setValue("description", current.description);
	changeRequest.setValue("cmdb_ci", current.cmdb_ci);
	if (changeRequest.hasValidChoice('priority', current.priority))
		changeRequest.setValue("priority", current.priority);
	changeRequest.setValue("sys_domain", current.sys_domain);
	changeRequest.setValue("company", current.company);
	var sysId = changeRequest.insert();

	if (!stdChgCatalogActive) {
		current.u_rfc = sysId;
		current.update();
		var sctUrl = "<a href='" + current.getLink(true) + "'>" + current.getDisplayValue() + "</a>";
		gs.addInfoMessage(gs.getMessage("Normal Change {0} created from {1}", [changeRequest.getValue("number"), sctUrl]));
	}

	action.setReturnURL(current);
	action.setRedirectURL(changeRequest.getGlideRecord());
})(current, previous, gs, action);

 

Standard Change:

 

Repeat the same steps as in Normal change, with the following script changes.

Condition: current.state != 3 && current.state != 4 && current.state != 7 && gs.hasRole("itil,sn_change_write") && gs.fieldExists('sc_task', 'u_rfc') && current.u_rfc.nil()

 

Script:

action.setRedirectURL(new StdChangeUtils().getURLForTask(current, 'u_rfc'));

 

Emergency Change:

 

Repeat the same steps as in Normal change, with the following script changes.

Condition: current.state != 3 && current.state != 4 && current.state != 7 && gs.hasRole("itil,sn_change_write") && gs.fieldExists('sc_task', 'u_rfc') && current.u_rfc.nil()

 

Script:

(function(current, previous, gs, action) {
	var target = {};
	target.table = current.getTableName();
	target.sysid = current.getUniqueValue();
	target.field = 'u_rfc';
	try {
		target.isWorkspace = (typeof RP == 'undefined');
	}
	catch (err) {
		target.isWorkspace = false;
	}

	gs.getSession().putProperty('change_link', target);

	var changeRequest = ChangeRequest.newEmergency();
	changeRequest.setValue("short_description", current.short_description);
	changeRequest.setValue("description", current.description);
	changeRequest.setValue("cmdb_ci", current.cmdb_ci);
	if (changeRequest.hasValidChoice('priority', current.priority))
		changeRequest.setValue("priority", current.priority);
	changeRequest.setValue("sys_domain", current.sys_domain);
	changeRequest.setValue("company", current.company);
	changeRequest.insert();

	action.setReturnURL(current);
	action.setRedirectURL(changeRequest.getGlideRecord());

})(current, previous, gs, action);

 

That's it!

 

Please mark my answer helpful and accept as a solution if it helped 👍✔️

Thanks,
Anvesh

Hello @AnveshKumar M ,

Greetings!!!

 

Thanks for your comments.

 

Regards,

Krishna.