create a subtask with scripting
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2022 01:52 AM
Hello,
I am working on a form that has a list collector variable with servers. I need to create a subtask per selected server with a script and the assignment group should be the one hardcoded below. Below is the script I put together but I'm afraid it is not working. If you have another version for the same functionality, kindly share!
Any help will be appreciated.
Thank you!
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// var serverArray = [];
var serverList = g_form.getValue('Server_name');
var servers = serverList.split(',');
for(var i=0; i < servers.lenght; i++) {
var gr = new GlideRecord('cmdb_ci');
gr.query('sys_id', servers[i]);
gr.query();
while(gr.next()){
var grTask = new GlideRecord('sc_task');
grTask.initialize();
grTask.request_item = server[i].sys_id;
grTask.description = "test";
grTask.short_description = "Application Access Request - ";
grTask.assignment_group = 'MY ASSIGNMENT GROUP';
grTask.insert();
}
}
//Type appropriate comment here, and begin script below
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2022 01:58 AM
Hi,
This looks weird. I see traces of an onChange client script, but you have server code. Is that true?
Can you tell me more about the requirements that led you down to this place? We can try to come up with a better solution for it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2022 02:05 AM
Thanks, Goran. I'm a rookie so there is a good chance it looks off. Perhaps, I will just articulate what I need. I have a form where people select servers on a list collector. Every selected server should give rise to a subtask. I'm not sure whether the script should be placed in the workflow or in the list collector. Any input/ideas will be much appreciated.
Thanks!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2022 04:26 AM
No problem 😃 Let's see what we can do. So we want a form where they select x servers, and when it's submitted, the only thing created is a task. Now, you talk about subtasks. Are these subtasks connected to a parent record?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2022 10:33 AM
Thanks Goran! The following query dealt with my quesiton. I'll leave it here in case it helps someone else: https://www.servicenow.com/community/developer-forum/multiple-catalog-are-created-depending-of-list-...
My code ended up as follows:
var varReferenceTable = 'cmdb_ci';
var temp = current.variables.Server_name;
var tasks = temp.toString().split(",");
var variableRec = new GlideRecord(varReferenceTable);
for (var i = 0; i < tasks.length; i++) {
var gr = new GlideRecord('sc_task');
var user = new GlideRecord('sys_user');
user.get(tasks[i]);
gr.initialize();
gr.request_item = current.sys_id;
gr.assignment_group = '0d1acdc31b15995032769a54b24bcbef';
variableRec.initialize();
variableRec.get(tasks[i]);
gr.short_description = variableRec.getDisplayValue();
gr.cmdb_ci = variableRec.setValue(variableRec.getDisplayValue());
gr.state = 1;
gr.insert();
}