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.

Call a UI Action from a Client Script to open a Change

RWeathersby1
Kilo Explorer

**I'm sure this has been done, before just can't find anything on the forum so I'm asking.**

I am trying to call a UI Action from a Client Script. I have a onChange Client Script running from the Incident table that if the 'confirm' value is True I'd like to automatically open a related Change record. But it's not working...thoughts?

Here's the code that I wrote that's not working.

function onChange(control, oldValue, newValue, isLoading) {
if (newValue == '7' && !isLoading && g_form.getValue('rfc')=='') {
var rfc = g_form.getReference('rfc');
var answer = confirm('Was this Incident resolved by a Change? If yes press OK to create a Change Record. If No press Cancel.')
//force the Resolved by Change to Mandatory if OK
if (answer == true)
gsftSubmit(null, g_form.getFormElement('name'), 'Create Change');
else
g_form.setMandatory('rfc', false);
return;
}
}

13 REPLIES 13

You have to add it to the UI action not the client script.


RWeathersby1
Kilo Explorer

So just to make sure that I'm not missing something:

Here's my Client Script and UI action
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (newValue == '7' && !isLoading && g_form.getValue('rfc')=='') {
var rfc = g_form.getReference('rfc');
var answer = confirm('Was this Incident resolved by a Change? If yes press OK. If No press cancel.')
//Call Create Change UI Action is true
if (answer == true)
gsftSubmit(null, g_form.getFormElement(), 'change_request');
else
g_form.setMandatory('rfc', false);
return;
}
}


Create Change UI Action:
var change = new GlideRecord("change_request");
var ch = new GlideRecord('change');
change.short_description = current.short_description;
change.description = current.description;
change.cmdb_ci = current.cmdb_ci;
change.priority = current.priority;
change.requested_by = current.assigned_to;
change.assigned_to = current.assigned_to;
change.work_notes = current.work_notes;
change.justification = "This RFC is to document an emergency break/fix change that was made during an active incident investigation ir order to restore service.";
change.u_work_characterization = "Emergency Break/Fix";
var sysID = change.insert();

current.rfc = sysID;
current.state = 4;
var mySysID = current.update();

gs.addInfoMessage("Change " + change.number + " created");
action.setRedirectURL(change);
action.setReturnURL(current);

ch.insert();
action.setRedirectURL(ch);// to redirect the the user to the newly created change record.


Try this....




//Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (newValue == '7' && !isLoading && g_form.getValue('rfc')=='') {
var rfc = g_form.getReference('rfc');
var answer = confirm('Was this Incident resolved by a Change? If yes press OK. If No press cancel.')
//Call Create Change UI Action is true
if (answer == true)
gsftSubmit(null, g_form.getFormElement(), 'change_request');
else
g_form.setMandatory('rfc', false);

}
}

//Create Change UI Action:
var change = new GlideRecord("change_request");
change.short_description = current.short_description;
change.description = current.description;
change.cmdb_ci = current.cmdb_ci;
change.priority = current.priority;
change.requested_by = current.assigned_to;
change.assigned_to = current.assigned_to;
change.work_notes = current.work_notes;
change.justification = "This RFC is to document an emergency break/fix change that was made during an active incident investigation ir order to restore service.";
change.u_work_characterization = "Emergency Break/Fix";
var sysID = change.insert();

current.rfc = sysID;
current.state = 4;
current.update();

gs.addInfoMessage("Change " + change.number + " created");
action.setRedirectURL(change); // to redirect the the user to the newly created change record.


I think you need to initialize the change.


So after


var change = new GlideRecord("change_request");


do


change.initialize();



This will give you a clean record to add values to.


RWeathersby1
Kilo Explorer

It just goes back to the Incident Queue. :0(