Update a variable in variable editor using scheduled job

robbcmd1234
Tera Expert
Hi, is it possible to update a variable in a variable editor using scheduled job. I plan to set a variable called 'Days Before Target Completion Date' in my item daily by subtracting 2 Date fields, 'Target Commencement Date' and 'Target Completion Date'.
 
I already tried this but not working. Any idea?
 

 

const scTaskGr = new GlideRecord('sc_task');
scTaskGr.addActiveQuery();
scTaskGr.addQuery('cat_item', '55ae8727e05b461093590cbbafe05143');
scTaskGr.addQuery('request_item.stage', 'fulfillment');
scTaskGr.query();

while (scTaskGr.next()) {
    const start = scTaskGr.variables.target_commencement_date;
    const end = scTaskGr.variables.target_completion_date;

    scTaskGr.variables.days_left_before_target_completion_date = GlideDate.subtract(start, end).getDisplayValue();
    scTaskGr.updateWithReferences();
}

 

2 ACCEPTED SOLUTIONS

@robbcmd1234 

then store the duration

const scTaskGr = new GlideRecord('sc_task');
scTaskGr.addActiveQuery();
scTaskGr.addQuery('cat_item', '55ae8727e05b461093590cbbafe05143');
scTaskGr.addQuery('request_item.stage', 'fulfillment');
scTaskGr.query();

while (scTaskGr.next()) {
    const start = scTaskGr.variables.target_commencement_date;
    const end = scTaskGr.variables.target_completion_date;

	var ritm = new GlideRecord('sc_req_item');
	ritm.get(scTaskGr.request_item);
    ritm.variables.days_left_before_target_completion_date = GlideDate.subtract(start, end).getDisplayValue();
    ritm.update();
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

Hi,

Thanks for this, but this one worked for me. I've used var instead of const inside the while loop. And I've accessed the variables in sc_task table.

gs.info('Set Remaining Days Left Before Target Completion Date Scheduled Job is running...');

var scTaskGr = new GlideRecord('sc_task');
scTaskGr.addActiveQuery();
scTaskGr.addQuery('cat_item', '55ae8727e05b461093590cbbafe05143');
scTaskGr.addQuery('request_item.stage', 'fulfillment');
scTaskGr.orderBy('number');
scTaskGr.query();

gs.info('Row Count: ' + scTaskGr.getRowCount());

while (scTaskGr.next()) {
    // gs.info('Current: ' + scTaskGr.variables.days_left_completion_date);

    var start = new GlideDate();
    start.setValue(scTaskGr.variables.target_commencement_date);

    var end = new GlideDate();
    end.setValue(scTaskGr.variables.target_completion_date);

    var daysLeft = GlideDate.subtract(start, end);

    gs.info('SCTASK Number: ' + scTaskGr.number);
    gs.info('Target Commencement Date: ' + start);
    gs.info('Target Completion Date: ' + end + '\n');
    gs.info("Days Left: " + daysLeft.getDisplayValue());

    if (start.getDisplayValue() == end.getDisplayValue()) {
        scTaskGr.variables.days_left_completion_date = '0 Day';
    } else {
        scTaskGr.variables.days_left_completion_date = daysLeft.getDisplayValue();
    }

    scTaskGr.update();
}

 

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

@robbcmd1234 

yes it's possible

but difference between 2 dates will be duration

what's the variable type for Days Before Target Completion Date?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi, 

 

its single-line text with no validation.

@robbcmd1234 

then store the duration

const scTaskGr = new GlideRecord('sc_task');
scTaskGr.addActiveQuery();
scTaskGr.addQuery('cat_item', '55ae8727e05b461093590cbbafe05143');
scTaskGr.addQuery('request_item.stage', 'fulfillment');
scTaskGr.query();

while (scTaskGr.next()) {
    const start = scTaskGr.variables.target_commencement_date;
    const end = scTaskGr.variables.target_completion_date;

	var ritm = new GlideRecord('sc_req_item');
	ritm.get(scTaskGr.request_item);
    ritm.variables.days_left_before_target_completion_date = GlideDate.subtract(start, end).getDisplayValue();
    ritm.update();
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@robbcmd1234 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader