Get current catalog item in default value script

kristenankeny
Tera Guru

We have a variable on a form that I need to auto-populate with a date, based on the delivery time on the catalog item. ServiceNow provided a script to put in the "default value" field on the variable. It works if I hard code the sys_id of the catalog item in the glide record query, but it's a shared variable, so I need to dynamically get the current catalog item sys_id. This is the script with all attempts and results in parentheses at the end. (They're commented out because they don't work).

javascript:

// Get the datetime now

var nowGdt = new GlideDate();

// The name of the schedule

var myScheduleName = '9-5 weekdays excluding holidays';

// The basis of our calculation

var dueDays = 0;

var cat = new GlideRecord('sc_cat_item');

//cat.addQuery('sys_id','3e125e653703de0056d9a9c2b3990e81'); //(sets right date)

//cat.addQuery('sys_id',g_form.getUniqueValue()); (blank)

//cat.addQuery('sys_id',current.cat_item.sys_id); (current date)

//cat.addQuery('sys_id',g_form.getParameter('sysparm_id')); (blank)

//cat.addQuery('sys_id',current.sys_id); (current date)

//cat.addQuery('sys_id',current.sysparm_id); (current date)

//cat.addQuery('sys_id',g_form.sys_id); (blank)

//cat.addQuery('sys_id',$("sysparm_id")); (blank)

//cat.addQuery('sys_id',g_form.getValue($('sysparm_id')); (blank)

//cat.addQuery('sys_id',g_form.getValue('sysparm_id')); (blank)

//cat.addQuery('sys_id',g_form.getValue('sys_id')); (blank)

//cat.get($("sysparm_id")); (blank)

cat.query();

if(cat.next()){

var dur = cat.delivery_time.dateNumericValue();

dueDays = dur/24/60/60/1000;

}

var dueWorkingHours = 8;

// The amount of time we want for the duration

var dueSeconds = dueDays*dueWorkingHours*60*60;

var leadTime = new GlideDuration(dueSeconds*1000);

// Calculate the Due Date!

var dueDateGdt;

var schedRec = new GlideRecord('cmn_schedule');

if (schedRec.get('name', myScheduleName)) {          

        var sched = new GlideSchedule(schedRec.sys_id);

        dueDateGdt = sched.add(nowGdt, leadTime, '');

}

dueDateGdt;

1 ACCEPTED SOLUTION

Abhinay Erra
Giga Sage

Here you go just add these lines to get the catalog item sys_id


var item_id=gs.action.getGlideURI().getMap().get('sysparm_id');




javascript:


// Get the datetime now


var nowGdt = new GlideDate();


// The name of the schedule


var myScheduleName = '9-5 weekdays excluding holidays';


// The basis of our calculation


var dueDays = 0;


var item_id=gs.action.getGlideURI().getMap().get('sysparm_id');


var cat = new GlideRecord('sc_cat_item');


cat.get(item_id);


var dur = cat.delivery_time.dateNumericValue();


dueDays = dur/24/60/60/1000;


var dueWorkingHours = 8;



// The amount of time we want for the duration


var dueSeconds = dueDays*dueWorkingHours*60*60;


var leadTime = new GlideDuration(dueSeconds*1000);



// Calculate the Due Date!


var dueDateGdt;


var schedRec = new GlideRecord('cmn_schedule');


if (schedRec.get('name', myScheduleName)) {      


        var sched = new GlideSchedule(schedRec.sys_id);


        dueDateGdt = sched.add(nowGdt, leadTime, '');


}


dueDateGdt;


View solution in original post

5 REPLIES 5

Abhinay Erra
Giga Sage

Here you go just add these lines to get the catalog item sys_id


var item_id=gs.action.getGlideURI().getMap().get('sysparm_id');




javascript:


// Get the datetime now


var nowGdt = new GlideDate();


// The name of the schedule


var myScheduleName = '9-5 weekdays excluding holidays';


// The basis of our calculation


var dueDays = 0;


var item_id=gs.action.getGlideURI().getMap().get('sysparm_id');


var cat = new GlideRecord('sc_cat_item');


cat.get(item_id);


var dur = cat.delivery_time.dateNumericValue();


dueDays = dur/24/60/60/1000;


var dueWorkingHours = 8;



// The amount of time we want for the duration


var dueSeconds = dueDays*dueWorkingHours*60*60;


var leadTime = new GlideDuration(dueSeconds*1000);



// Calculate the Due Date!


var dueDateGdt;


var schedRec = new GlideRecord('cmn_schedule');


if (schedRec.get('name', myScheduleName)) {      


        var sched = new GlideSchedule(schedRec.sys_id);


        dueDateGdt = sched.add(nowGdt, leadTime, '');


}


dueDateGdt;


Thank you so much! I've been waiting on the ServiceNow help desk to get back to me since yesterday. This did the trick. Much appreciated!


Hi Abhinay,



I'm using below script to set completion date . for 3 days sla its showing 3 days and 3 hours extra instead just 3 days. I believe its due to time zone (correct me if i'm wrong) How can i set to show current SLA   in GMT time zone?



javascript:


// Get the datetime now


var nowGdt = new GlideDateTime();




// The name of the schedule


var myScheduleName = '8-5 weekdays';



// The basis of our calculation


var dueDays = 3;


var dueWorkingHours = 8; but its



// The amount of time we want for the duration


var dueSeconds = dueDays*dueWorkingHours*60*60;


var leadTime = new GlideDuration(dueSeconds*1000);



// Calculate the Due Date!


var dueDateGdt;


var schedRec = new GlideRecord('cmn_schedule');


if (schedRec.get('name', myScheduleName)) {          


        var sched = new GlideSchedule(schedRec.sys_id);


        dueDateGdt = sched.add(nowGdt, leadTime, '');


}



dueDateGdt;


Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

current.getUniqueValue() ? I can't see that in your testing.