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.

Add new SCTASK(s) to RITM

Leon Els
Tera Expert

Greetings, 
I am trying to create a UI Action that will allow the Service Desk to add Additional SCTASK(s) to any Open RITM.
Thank you in advance for any guidance 🙂 

1 ACCEPTED SOLUTION

Amrit4
Tera Guru

Copy the below script to your UI action:

UI Action name - " whatever "

Table - sc_req_item

 

Client - keep it un-ticked (false)

 

 

 

var sc = new GlideRecord('sc_task');
sc.newRecord();
sc.setValue('short_description', current.getValue('short_description'));
sc.setValue('description', current.getValue('description'));
sc.setValue('assignment_group', current.getValue('assignment_group'));
sc.setValue('request_item', current.getValue('sys_id'));
sc.setValue('request', current.getValue('request'));

// add required fields

var recSysId = sc.insert(); // create catalog task

// to copy variable on catalog task
var gr = new GlideRecord('sc_item_option_mtom');
gr.addQuery('request_item.numberSTARTSWITH' + current.getValue('number'));
gr.query();
while (gr.next()) {

var grt = new GlideRecord('sc_item_variables_task');
grt.newRecord();
grt.setValue('task', recSysId);
grt.setValue('variable', gr.sc_item_option.item_option_new.toString());
grt.insert();
}

 

 

 

 

Please be kind enough to mark my answer correct and helpful.

View solution in original post

9 REPLIES 9

DUGGI
Giga Guru

@Leon Els Sure, here's a sample UI Action script for adding additional SCTASK(s) to an open RITM in ServiceNow:

(function() {
  var ritmSysId = g_form.getUniqueValue(); // Get the unique ID of the RITM record
  var gr = new GlideRecord('sc_task'); // Create a new GlideRecord object for the SCTASK table
  gr.initialize(); // Initialize the GlideRecord object
  gr.request_item.setDisplayValue(ritmSysId); // Set the Request Item field to the RITM sys_id
  gr.short_description = "New SCTASK"; // Set a default short description
  gr.insert(); // Insert the new SCTASK record

  // Refresh the RITM form to show the new SCTASK
  var ritmForm = g_form.getFormElement();
  ritmForm.action = ritmForm.action + "&sysparm_clear_stack=true";
  ritmForm.submit();
})();

Rahul Kumar17
Tera Guru

Hi,

 

To create a UI action in ServiceNow that allows the Service Desk to add additional SCTASK(s) to any Open RITM, you can follow these steps:

  1. Navigate to the RITM (Requested Item) form and click on the "UI Actions" related list.

  2. Click the "New" button to create a new UI action.

  3. Fill out the form to create the UI action. Here are some suggested values for the fields:

    • Name: Add Additional SCTASK(s)
    • Table: Requested Item [sc_req_item]
    • Action name: add_sctask
    • Client: true
    • Onclick: openAdditionalSCTask()
  4. Save the UI action.

  5. Create a script include to contain the code that will be executed when the UI action is clicked. Here is some example code:

function openAdditionalSCTask() {
var ga = new GlideAjax('AddSCTaskAjax');
ga.addParam('sysparm_name', 'getForm');
ga.addParam('sysparm_ritm', g_form.getUniqueValue());
ga.getXML(function(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == "true") {
var url = '/nav_to.do?uri=' + encodeURIComponent('task.do?sysparm_query=request_item=' + g_form.getUniqueValue());
window.open(url);
}
});
}

 

Create a script include with the name "AddSCTaskAjax" to contain the server-side code. Here is some example code:

 

var AddSCTaskAjax = Class.create();
AddSCTaskAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getForm: function() {
var ritmID = this.getParameter('sysparm_ritm');
var ritmGR = new GlideRecord('sc_req_item');
if (ritmGR.get(ritmID)) {
var taskGR = new GlideRecord('sc_task');
taskGR.initialize();
taskGR.request_item = ritmGR.sys_id;
taskGR.insert();
return true;
} else {
return false;
}
},
type: 'AddSCTaskAjax'
});

 

Thanks,

Rahul Kumar

If my response helped please mark it correct and close the thread.

Thanks,
Rahul Kumar

Thank you SO much for your guidance!  I am so fortunate to have 3 responses and will advise soon  🙂

Leon Els
Tera Expert

@Rahul Kumar17  - Big thank you again! I have a quick question, please.

Is the below part of the same UI Action?

Currently, I am not sure where to paste that code (Below screenshots show what I have so far.)

LeonEls_0-1681505832543.png

LeonEls_1-1681505891179.png

 

 



Create a script include with the name "AddSCTaskAjax" to contain the server-side code. Here is some example code:

 

var AddSCTaskAjax = Class.create();
AddSCTaskAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getForm: function() {
var ritmID = this.getParameter('sysparm_ritm');
var ritmGR = new GlideRecord('sc_req_item');
if (ritmGR.get(ritmID)) {
var taskGR = new GlideRecord('sc_task');
taskGR.initialize();
taskGR.request_item = ritmGR.sys_id;
taskGR.insert();
return true;
} else {
return false;
}
},
type: 'AddSCTaskAjax'
});

I have provided the steps.

Please go one by one

If my response helped please mark it correct and close the thread.

Thanks,
Rahul Kumar