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.

Update records in other tables using UI Action

jinhui son
Tera Contributor

This is my UI Action(incident table) Scriptfind_real_file.png

 
if (!current.universal_request){
var gr = new GlideRecord('universal_request');
gr.initailize();
gr.short_description = current.short_description;
gr.description = current.description;
gr.business_service = current.business_service;
gr.cmdb_ci = current.cmdb_ci;
gr.insert();
 
action.setRedirectURL(gr);
action.setReturnURL(current);
 
} else {
var gr2 = new GlideRecord('universal_request');
gr2.addQuery('number', current.universal_request);
gr2.query();
 
while (gr2.next()){
gr2.state = 2;
gr2.update();
 
}

 

Press the "Not Incident" button in the form header for the generated incident
1. If there is a connected UR, change the status of the UR to "In Progress"
2. If there is no connected UR, I want to create a new UR

Number 2 works normally, but number 1 doesn't work.
What's wrong with my script?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

update as this

1) you will have to update current record as well once you create the UR

if (current.universal_request == ''){
	var gr = new GlideRecord('universal_request');
	gr.initailize();
	gr.short_description = current.short_description;
	gr.description = current.description;
	gr.business_service = current.business_service;
	gr.cmdb_ci = current.cmdb_ci;
	gr.insert();
	current.universal_request = gr.number; // I assume universal_request holds number and not sysId
	current.update();
	action.setReturnURL(current);
	action.setRedirectURL(gr);

} else {
	var gr2 = new GlideRecord('universal_request');
	gr2.addQuery('number', current.universal_request); // I assume universal_request holds number and not sysId
	gr2.query();
	if (gr2.next()){
		gr2.state = 2; // ensure the state choice value is correct
		gr2.update();
	}
}

Regards
Ankur

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

View solution in original post

2 REPLIES 2

Mohith Devatte
Tera Sage
Tera Sage

Hello,

Did you try putting a gs.info() in else loop ?

and also what does current.universal_request return ?is it a number for sure?

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

update as this

1) you will have to update current record as well once you create the UR

if (current.universal_request == ''){
	var gr = new GlideRecord('universal_request');
	gr.initailize();
	gr.short_description = current.short_description;
	gr.description = current.description;
	gr.business_service = current.business_service;
	gr.cmdb_ci = current.cmdb_ci;
	gr.insert();
	current.universal_request = gr.number; // I assume universal_request holds number and not sysId
	current.update();
	action.setReturnURL(current);
	action.setRedirectURL(gr);

} else {
	var gr2 = new GlideRecord('universal_request');
	gr2.addQuery('number', current.universal_request); // I assume universal_request holds number and not sysId
	gr2.query();
	if (gr2.next()){
		gr2.state = 2; // ensure the state choice value is correct
		gr2.update();
	}
}

Regards
Ankur

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