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 Change UI Action query

Cirrus
Kilo Sage

Hi,

We would like to create a change from a catalog task, and can achieve this simply by using the following script in a ui action:

var url = new GlideURL("change_request.do");
url.set("sys_id" , "-1");
url.set("sysparm_query" , "^parent=" + current.sys_id);
action.setRedirectURL(url.toString()+"");
 
is there a way to amend the script to set other fields, for example the Short Description, from the parent catalog task??
1 ACCEPTED SOLUTION

Community Alums
Not applicable

Yes, you need to add it on one set(). Like this:

var url = new GlideURL("change_request.do");
url.set("sys_id" , "-1");
//url.set("sysparm_query" , "^parent=" + current.sys_id);
url.set("sysparm_query" , "^short_description=" + current.short_description +^parent=" + current.sys_id);

action.setRedirectURL(url.toString()+"");

View solution in original post

10 REPLIES 10

Sohail Khilji
Kilo Patron

Hi @Cirrus ,

 

Create a UI action on the catalog task table with below script :

 

 

(function createChangeRequest() {

    var changeRequest = new GlideRecord('change_request');
    changeRequest.initialize();
    changeRequest.short_description = 'New Change Request'; 
    changeRequest.description = 'Description of the change'; 
    // Set other fields as needed
    var sysId = changeRequest.insert();
    
    if (sysId) {
        gs.addinfoMessage('Change Request created successfully ');
action.setRedirectURL("/change_request_list.do?sys_id=sysId);
    } else {
        gs.addinfoMessage('Failed to create Change Request');
    }
})();

 


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

Thanks Sohail. The code is throwing up an error on the following line:

action.setRedirectURL("/change_request_list.do?sys_id=sysId);

Kieran Anson
Kilo Patron

Hey,

An easier method if you just want to direct the user to a pre-filled out record is to use action.openGlideRecord (example below). It's a more familiar syntax style as you effectively create a GlideRecord representation of what you want, without submitting it to the database

 

action - Scoped, Global | ServiceNow Developers

Thanks Kieran, I tried the following as we arent using a template, but whilst it populated the short description as desired, it no longer populates the Parent with the SCTASK number

 

var change = new GlideRecord("change_request");
change.initialize();
change.short_description = current.short_description;
change.description = current.u_details;
change.cmdb_ci = current.u_service;
change.priority = current.priority;
change.requested_by = current.caller_id;
change.assignment_group.setDisplayValue('Change & Release');
change.u_status = 'New';
change.parent = current.number;
//change.applyTemplate("standard_rfc");
current.rfc = change.insert();
current.comments = 'Change ' + change.number + ' created.';

var mySysID = current.update();

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