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

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();
}

 

@robbcmd1234 

I believe I have provided you the correct answer.

you could have simply updated the variable from const to var

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

vishwajeet5550
Mega Guru

Yes, it is possible to update a variable in a variable editor using a scheduled job. Your approach to calculating the Days Before Target Completion Date by subtracting two date fields is valid. However, there are a few things that need to be adjusted in your script, as well as a clarification regarding how you are working with the variables in the sc_task record and updating them

 

 

(function() {
    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;
        if (start && end) {
            const startDateTime = new GlideDateTime(start);
            const endDateTime = new GlideDateTime(end);
            const diffInMillis = endDateTime.getNumericValue() - startDateTime.getNumericValue();
            const diffInDays = diffInMillis / (1000 * 60 * 60 * 24);
            scTaskGr.variables.days_left_before_target_completion_date = diffInDays;
            scTaskGr.update();
       

 

 

Handling Null or Missing Dates: The script assumes that both target_commencement_date and target_completion_date are available. If either is missing, it will skip that task. You may want to add additional logic to handle cases where the dates are not set.

Logging and Error Handling: You may want to add some logging for better tracking of script execution, especially if you need to debug any issues with the data

Hi,

 

Target Commencement Date and Target Completion Date are mandatory in the form. I also adding logging with gs.info for debugging like you suggested.

 

Thanks