Error Script creating two tasks

Joshua Comeau
Kilo Sage

Script I am using is creating two tasks and the date is not filling out the data from the sctask level please help!

Script:

try{
 
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
//add your requested item to the cart by sys_id of the catalog item
var item = cart.addItem('136cdab01b50311070fb4002dd4bcb24', 1);
 
//fill in the variables on the request item form
cart.setVariable(item, "asset", "136cdab01b50311070fb4002dd4bcb24"); // asset here is variable name and since it's reference we have used sysId
var rc = cart.placeOrder();
gs.info(rc.number);
 
var ritm = new GlideRecord('sc_req_item');
if(ritm.get('request', rc.sys_id)){
var rec = new GlideRecord('sc_task');
rec.initialize();
rec.request = rc.sys_id;
rec.request_item = ritm.getUniqueValue();
rec.assignment_group = 'b101adf8dbe65b407f0264904b961972'; 
rec.short_description = 'Maintenance Plan - Facility Task';
rec.description = 'Replace every light on every floor';
sc_task.actual_work_start = '2023-07-25 15:08:09';
sc_task.actual_work_end = '2023-07-26 15:08:11';
rec.insert();
}
 
}
catch(ex){
gs.info(ex);
}
1 ACCEPTED SOLUTION

Joshua Comeau
Kilo Sage

Fixed the issue with the following code:

 

try{

var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
//add your requested item to the cart by sys_id of the catalog item
var item = cart.addItem('136cdab01b50311070fb4002dd4bcb24', 1);

//fill in the variables on the request item form
cart.setVariable(item, 'what_can_we_help_you_with', "Fix lights on floor 1");
cart.setVariable(item, 'can_you_give_us_some_more_details', "this is more information");
cart.setVariable(item, 'are_you_a_member_of_the_facilities_team', "Yes");
cart.setVariable(item, 'actual_work_start', "2023-07-25 15:08:09");
cart.setVariable(item, 'actual_work_end', "2023-07-26 15:08:11");
var rc = cart.placeOrder();
gs.info(rc.number);
}
catch(ex){
gs.info(ex);
}

View solution in original post

6 REPLIES 6

Chetan Mahajan
Kilo Sage
Kilo Sage

Hello @Joshua Comeau ,

                                     It looks like there's a minor issue with your script. You're trying to set the "actual_work_start" and "actual_work_end" fields on an GlideRecord named "sc_task," which is not defined in your script. Instead, you should use the "rec" GlideRecord that represents the newly created sc_task record. Let's fix the script by updating these lines:

 

rec.actual_start = '2023-07-25 15:08:09';
rec.actual_end = '2023-07-26 15:08:11';

 

Also avoid hardcoded value here. 

Kindly mark correct and helpful if applicable 

 

When I add those lines it does not update the variable. Also two tasks are being created I believe it is because the workflow has a task its following the flow creating the task in the workflow but creating that task cause of the script how to get it to bypass the flow and also update the actual start and end date 

if once task is creating by wf you can update that task with new values instead inserting new record 

 

var rec= new GlideRecord('sc_task');
rec.addQuery('request_item', ritm.sys_id); // RITM sys_is
rec.query();
if(rec.next()){
rec.assignment_group = 'b101adf8dbe65b407f0264904b961972'; 
rec.short_description = 'Maintenance Plan - Facility Task';
rec.description = 'Replace every light on every floor';
rec.actual_work_start = '2023-07-25 15:08:09';
rec.actual_work_end = '2023-07-26 15:08:11';
rec.update();
}

 

@Chetan Mahajan so I can remove the entire code I had and just put this?