Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Krishna142 

It is almost OOTB, you need to just copy. & change it to Sc_task table. 

 

LearnNGrowAtul_0-1701860220051.png

 

https://INSTANCENAME.service-now.com/now/nav/ui/classic/params/target/sys_ui_action_list.do%3Fsysparm_query%3DnameLIKEchange%255Etable%253Dincident%26sysparm_first_row%3D1%26sysparm_view%3D

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Hi @Krishna142 

 

You can follow this method and change will created in change table. If you want to show the change on SC task , then need to create a new filed of reference type and refer to change table. 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************