How can i fill a duration field using a bakground script

samuelscott
Tera Expert

 

I created an SLA that must obtain results for "task_sla" records that have been created before the creation of the SLA. Therefore, I created a background script that fills in most of the fields from the task_sla record. However, I want to fill in the duration fields but I find it quite difficult. Also, I wasn't able to change the stage field from "In progress" to "Complete", because I believe there are some security rules that overpower a background script (I still have my doubts though).

 

var readonly = true;
var tsk = new GlideRecord('sc_task');
tsk.addQuery('short_description', "Please confirm the requestor proposal.");
tsk.query();

while(tsk.next()){
	var gr = new GlideRecord('contract_sla');
	gr.addQuery('name','RFS - Prepare Proposal');
	gr.query();
	
	if (gr.next()){
		var tsla = new GlideRecord('task_sla');
		tsla.addQuery('task', tsk.request_item);
		tsla.addQuery('sla', gr.sys_id);
		tsla.query();

		if(tsla.next()){
			gs.print("task number:" +tsk.request_item.getDisplayValue() + " RITM # in TASK SLA " + tsla.task.getDisplayValue() + " found, then skip creation.");
		}else{	
			tsla.initialize();
			tsla.sla = gr.sys_id;
			tsla.task = tsk.request_item;
			tsla.start_time = tsk.sys_created_on;
			tsla.end_time = tsk.closed_at;
			if(readonly){
				gs.print(tsla.sla + ";" + tsla.task.getDisplayValue() + ";" + tsla.start_time + ";" + tsla.end_time);
			}else{
				tsla.insert();
			}
		}
	}
}

Below is a screenshot that shows the fields I want to fill.

 

 

1 ACCEPTED SOLUTION

Oh, those fields. The 'Actual Elapsed Time' is calculated 24x7, no matter what. The 'Business Elapsed Time' is calculated based off the predefined schedule that you have attached to that SLA. To update them, they're expecting 'GlideDuration' values.

In your sub-production environment, create a record with an SLA you are willing to modify. Then copy the sys_id of the SLA record that's attached and use the following script to see how the numbers can be modified/manipulated:

 

var tSla = new GlideRecord('task_sla');
tSla.addQuery('sys_id', '<sys_id_goes_here>');
tSla.query();

if (tSla.next()) {
    // 3 days, 11hr:11m:11s, you don't have to include days if you just want to change time.
    tSla.business_duration = new GlideDuration('3 11:11:11'); 
    tSla.update();
}

 

If this answers your question, please mark this as correct.

View solution in original post

8 REPLIES 8

Coleton
Kilo Guru

Match the date convention with the value you input and update the record with the record object name.update();

samuelscott
Tera Expert

I'm not sure I understand...I want to fill in the duration fields, for that, I was thinking on using something like: "var duration = GlideDateTime.subtract(start, end)", but calculating business elapsed time could be a little complex. What do you mean "the value you input"? All values have been insterted (using initialize syntax) with a background script.

 

THANKS!

So, I guess I would need a bit more clarification on the requirements. You might also be doing redundant work in your queries which is looking like it could potentially create a lot of overhead. From the 'task_sla' table, you have access to the SLA being used, the task record (referenced so you can dot walk to task fields), etc.

 

Additionally, the screenshot is not rendering on my end, could you please insert the image into your response message?

samuelscott
Tera Expert

Yes...I see what you mean with the redundancy, I initially tried dotwalking but it didn't seem to work, that is why I "reached" for the sla sys id. Below is the screenshot.

 

find_real_file.png

 

find_real_file.png