Workflow script writes to RITM instead of REQ

Ken Berger
Giga Guru

Hi folks,

 

I have a script action on a workflow that is set to the sc_request table.  The script creates a new CMDB CI based on a form field and then is supposed to raise a request and associated tasks.  I have 2 issues that I am thinking are related:

 

1. The request fields are being populated in the request item, not the request.

2. The tasks are not getting the request sysId to the parent field, so they are not appearing the related list of the request.

 

Not really sure what is wrong here, as I thought that setting the tabe to be sc_request in the workflow properties would allow me to set the values of the request by using 'current' but this is not working.  Neither is 'current.request'

 

As always, any help is appreciated.  Here is the script:

if(current.variable_pool.device_type == 'server'){
	
	//Create a server CI and set the values from the variables
	var cmdbCi = new GlideRecord('cmdb_ci_server');
	cmdbCi.initialize();
	cmdbCi.name = current.variable_pool.device_name;
	cmdbCi.u_physical_virtual = current.variable_pool.physical_virtual;
	cmdbCi.install_status = current.variable_pool.install_status;
	cmdbCi.location = current.variable_pool.site;
	cmdbCi.short_description = current.variable_pool.purpose;
	cmdbCi.classification = current.variable_pool.classification;
	cmdbCi.u_application_owner = current.variable_pool.owner; 
	cmdbCi.os = current.variable_pool.operating_system;
	cmdbCi.u_domain = current.variable_pool.domain;
	cmdbCi.ip_address = current.variable_pool.ip_address;
	cmdbCi.u_patching_method = current.variable_pool.patching_method;
	cmdbCi.u_patch_window = current.variable_pool.patch_window;
	cmdbCi.maintenance_schedule = current.variable_pool.maintenance_window;
	cmdbCi.u_notes = "Redundancy, DR Requirements: " + current.variable_pool.redundancy_dr_req + "\n Support, Escalation: " + current.variable_pool.support_escalation + "\n Firewall Requirements: " + current.variable_pool.firewall_requirements + "\n Power Requirements: " + current.variable_pool.power_requirements + "\n Backup Requirements: " + current.variable_pool.backup_requirements;
	cmdbCi.sys_class_name = "Server";

	// Save the server CI and get the sys_id
	var ciSysId = cmdbCi.insert(); 
	
	//Array containing tasks
	var tasks = [];
	tasks = ["Install FIRST agent", "Install SECOND agent and notify security", "Install APP1, THIRD agent", "Setup SNMP service", "Apply all required GPO's", "Update firmware", "Add server to correct OU", "Install all required patches", "Enroll breakglass and other secrets in SAFE"];

	//Link for the confirmation pop-up
	var link = '<a class="breadcrumb" href="cmdb_ci_server.do?sys_id=' + ciSysId + '">' + cmdbCi.number + '</a>';

} else if (current.variable_pool.device_type == 'network') {
	
	//Create a network CI and set the values from the variables	
	var cmdbCi = new GlideRecord('cmdb_ci_netgear');
	cmdbCi.initialize();
	cmdbCi.name = current.variable_pool.device_name;
	cmdbCi.location = current.variable_pool.site;
	cmdbCi.short_description = current.variable_pool.purpose;
	cmdbCi.owned_by = current.variable_pool.owner; 
	cmdbCi.dns_domain = current.variable_pool.domain;
	cmdbCi.ip_address = current.variable_pool.ip_address;
	cmdbCi.maintenance_schedule = current.variable_pool.maintenance_window;
	cmdbCi.firmware_version = current.variable_pool.firmware_version;	
	cmdbCi.comments = "Redundancy, DR Requirements: " + current.variable_pool.redundancy_dr_req + "\n Support, Escalation: " + current.variable_pool.support_escalation + "\n Firewall Requirements: " + current.variable_pool.firewall_requirements + "\n Power Requirements: " + current.variable_pool.power_requirements + "\n Backup Requirements: " + current.variable_pool.backup_requirements;
	cmdbCi.sys_class_name = "Network Interface Device";

	// Save the request and get the sys_id
	var ciSysId = cmdbCi.insert();

	//Array containing tasks
	var tasks = [];
	tasks = ["Install SECOND agent and notify security", "Install APP, THIRD agent", "Complete NETWORK onboarding", "Setup SNMP service", "Apply all security management policies", "Update firmware", "Install all required patches", "Enroll breakglass and other secrets in SAFE"];

	//Link for the confirmation pop-up
	var link = '<a class="breadcrumb" href="cmdb_ci_netgear.do?sys_id=' + ciSysId + '">' + cmdbCi.number + '</a>';
}	

//Add the request fields
current.request.short_description = "New " + current.variable_pool.device_type + " creation request";
current.request.description = "A new " + current.variable_pool.device_type + " CI has been created for " + current.variable_pool.device_name + ". Please complete the configuration and sign off on all required Tasks.";
current.request.due_date = current.variable_pool.required_date;
current.request.assignment_group = 'd4024b97db9ba600abebdee5ce96193c';	
current.request.comments = "Redundancy, DR Requirements: " + current.variable_pool.redundancy_dr_req + "\n Support, Escalation: " + current.variable_pool.support_escalation + "\n Firewall Requirements: " + current.variable_pool.firewall_requirements + "\n Power Requirements: " + current.variable_pool.power_requirements + "\n Backup Requirements: " + current.variable_pool.backup_requirements;

// Add tasks to the request
for (var i = 0; i < tasks.length; i++) {
	var taskGr = new GlideRecord('sc_task');
	taskGr.initialize();
	taskGr.assignment_group = 'd4024b97db9ba600abebdee5ce96193c';
	taskGr.short_description = tasks[i];
//	taskGr.parent = current.request; // Link task to the request
	taskGr.parent = current.request.sysId; // Link task to the request

	taskGr.insert();
}

//Create the information message
var message = 'A new CMDB CI ' + link + ' has been created for ' + current.variable_pool.device_type + ' ' + current.variable_pool.device_name + '. ';
message += 'Thank you for your submission.';

//Add the information message
gs.addInfoMessage(message);

 

Thanks!

Ken

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

It sounds like you are trying to go against the service request architecture, so I'm wondering if there is a reason.  A request (REQ / sc_request) is meant to be the parent of one or more Request Item (RITM / sc_req_item) records, each of which can have one or more Catalog Task (SCTASK, sc_task) records.  Catalog Item variables are stored in an object which is available to both SCTASK and RITM records.  When a variable is updated on one record, it maintains the same value in all other records.  It's fine to have a workflow that runs on a sc_request record, but REQs generally do not have variables or Catalog Tasks directly related to them.

View solution in original post

5 REPLIES 5

Brad Bowman
Kilo Patron
Kilo Patron

It sounds like you are trying to go against the service request architecture, so I'm wondering if there is a reason.  A request (REQ / sc_request) is meant to be the parent of one or more Request Item (RITM / sc_req_item) records, each of which can have one or more Catalog Task (SCTASK, sc_task) records.  Catalog Item variables are stored in an object which is available to both SCTASK and RITM records.  When a variable is updated on one record, it maintains the same value in all other records.  It's fine to have a workflow that runs on a sc_request record, but REQs generally do not have variables or Catalog Tasks directly related to them.

Hi Brad,

 

Thanks for the advice.  I suppose that having the RITM be the place where the tasks are listed would work, and makes sense.  I will have to rethink my original idea and how to code this.

 

Thanks,

Ken

A Service Request sounds like the right approach.  Create a Catalog Item with the variables of data to collect at request time or along the way as tasks are completed, then add whatever tasks and scripts, like to create the CI, in the workflow fit the process.  If you need / want to spawn this request from another request / script, that can be done, but let it create the new REQ and RITM, which will then create the SCTASK(s) as the workflow runs.

Brad,

 

Thanks.  I hadn't looked at it that way but the more I think about it, this approach provides more flexibility and options for the workflow.  I was able to get the tasks associated with the workflow and see that I can incorporate approvals on the requested CI and other cool stuff 😉

 

Thanks!

Ken