Scheduled Job script to create REQ, RITM, SCTASK with variables

Tim Hannah
Tera Contributor

We need to create a repeat SCTASK periodically throughout the day with variables filled in (variables are set as default values on the Catalog Item) 

I know the Variables are on the RITM level, so I assumed we need to order an item via script and let the workflow create the REQ, RITM and then SCTASK 

We are using the below script which does the job fine, except every ticket is being logged against me as the 'opened by' and 'request for' this needs to be 'automation' user as it does with other scripted scheduled jobs 

var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('automation check', 1);
var rc = cart.placeOrder();

I have tried 

cart.setVariable(item, 'requested_for', 'automation');

but to no avail. Any help? 

4 REPLIES 4

harishdasari
Tera Guru

Hello Tiim,

We have also faced the same issue.

How we have fixed this issue is, in the workflow we have created the run script activity in the workflow and added the below code.

Please make the necessary modification as per your requirement.

find_real_file.png

 

find_real_file.png

 

var userName = current.variables.user_name;
var RequestdFor = current.request;
		var req = new GlideRecord('sc_request');
		req.addQuery('sys_id', RequestdFor);
		req.query();
		if(req.next())
			{
				req.requested_for = userName;
				req.u_approving_manager = userName.manager;
				req.update();
			}

 

Thank you.

 

Hope this is Helpful.

This still didnt work, i adapted this for my use and it still created all the tasks (REQ, RITM, SCTASK) opened by, and request for

Jyoti8
Kilo Guru

Hi Tim Hannah,

Try this once, I hope it will work...:)

One RIQ With Multiple RITMs

createRequest();

function createRequest() {

var cart = new Cart();

// Add in cart, sys_id for Internal Service Desk Request catalog item

var item = cart.addItem('423e61c9d1ef91802f5ee754eb9fe2d1');

// Set requested for, sys_id for requestor

cart.setVariable(item, 'requested_for', '0380621d901364002f5ec8b94177cdb7');

//Set date needed to current date

var gdt = new GlideDateTime();

cart.setVariable(item,'date_needed',gdt);

//Set business service to conference room

cart.setVariable(item, 'business_service', 'b2ca516c801aa1001a4b5af6f2de4bb7');

//Set request type to general

cart.setVariable(item, 'request_type', 'general');

//Set short description

cart.setVariable(item, 'short_description', 'Patch conference room computers');

//Set notes

cart.setVariable(item, 'notes', 'Patch conference room PC with latest windows patches');

// Places order and creates request

var rc = cart.placeOrder();

var item2 = cart.addItem('asdfasdf');

cart.setVariable(item2, 'variablename', 'value');

}

 

 

Script for SC_task

function () {

 

        var task = new GlideRecord('sc_task');

 

        task.newRecord();

 

        task.short_description = 'Created by a scheduled job'; // change as appropriate.

 

        task.request_item = RITM_SYS_ID; // enter the sys_id of the requested item here

 

        task.insert();

 

})();

 

 

RITM Variable for Task

 

function addVars(taskSysid){

 

  //these are the variables from the request item you want to be displayed in the task

 

  var task_vars_include_list = ""acct_request_type_label,bulk_request,create_new_email,provide_login_id,requested_by,requested_for";

 

 

 

  var my_sc_item_option_mtom = new GlideRecord ('sc_item_option_mtom');

 

  my_sc_item_option_mtom.addQuery('request_item', current.sys_id);

 

  my_sc_item_option_mtom.addQuery('sc_item_option.item_option_new.name', 'IN', task_vars_include_list);

 

  my_sc_item_option_mtom.query();

 

  while (my_sc_item_option_mtom.next()){

 

 

 

      var myTaskVar = new GlideRecord('sc_item_variables_task');

 

      myTaskVar.initialize();

 

      myTaskVar.task = taskSysid;

 

      myTaskVar.variable = my_sc_item_option_mtom.sc_item_option.item_option_new;

 

      myTaskVar.insert();

 

  }

 

}

 

If I have answered your question, please mark my response as correct and/or helpful so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list.

Thanks

Where am i creating these scripts?