This page will serve as a reference for ServiceNow tips/tricks I have learned over
the years.
Note that some of these references are quoted from SN forums (in an effort to personally
preserve them).
The first parameter will sort in Ascending order whilst the latter in Descending order. APPEND THIS TO END OF URL.
&sysparm_group_sort=COUNT
&sysparm_group_sort=COUNTDESC
If the above doesn't work, try one of these 2 parameters
%26sysparm_group_sort%3DCOUNTDESC
%26sysparm_group_sort%3DCOUNT
If Polaris Experience, append
/now/nav/ui
to the end
of your base url.
base url: www.instance-name.service-now.com or www.instance-name.servicenowservices.com
Insert //Create a new Incident record and populate the fields with the specified values var gr = new GlideRecord('incident'); gr.initialize(); gr.short_description = 'Network problem'; gr.category = 'software'; gr.caller_id.setDisplayValue('Joe Employee'); gr.insert(); GlideAggregate //Retrieve all active incidents and log a count of records to the system log var gr = new GlideAggregate('incident'); gr.addQuery('active', true); gr.addAggregate('COUNT'); //COUNT, SUM, MIN, MAX, AVG. gr.addActiveQuery(); gr.query(); gs.print(gr.getRowCount()); var incidents = 0; if (gr.next()){ incidents = gr.getAggregate('COUNT'); gs.log('Active incident count: ' + incidents); }
This code will count the number of occurences of an item in an array. function getOccurrence(array, value) { var count = 0; array.forEach((v) => (v === value && count++)); return count; }
This code will filter an array for duplicates and remove them. function uniq(array) { var seen = {}; return array.filter(function(item) { return seen.hasOwnProperty(item) ? false : (seen[item] = true); }); }
This code will allow you to calculate business days out from a field and take action on it. var today = new GlideDate(); // Hold today var endDate = new GlideDate(FIELD/VARIABLE NAME HERE); //Hold end_date var days = 2; //2 days days = days * 9; //9 business hours per day var twoBusDays = new GlideDuration(60 * 60 * 1000 * days); //2 business days var schedule = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828'); //8-5 weekdays excluding holidays var twoBusDaysDate = schedule.add(today, twoBusDays).getDate(); //find two business days from today if (today.getDate().compareTo(endDate.getDate()) == 0) { //if today is end date //action here } else if (endDate.getDate().compareTo(twoBusDaysDate.getDate()) == 0) { //if two business days is end date //action here }
This code will allow you to cancel a flow for a certain record, provided a source sys_id is given. Assuming the flow is trigged on creation of that record. THIS IS DANGEROUS. var gr = new GlideRecord('sys_flow_context'); gr.addQuery('source_record', current.sys_id); gr.query(); if (gr.next) { gr.state = 'CANCELLED'; gr.update(); }
This code is for the due date of an approval and can be modified for Flow Designer (currently for workflow). var endDateTime can be modified to adjust the amount of days. Note that business days have 9 hours. 5 business days is 1 week, which is 45 business hours.
function hoursFromHere(startDateSTR, durationHRS, scheduleID, exactly) { // startDateSTR must be GMT format to use with schedule! // If exactly is true, durationHRS 'Business' hours from startDateSTR, given the scheduleID // If exaclty is false, At least durationHRS of 'Calendar' hours from startDateSTR, with return // being earliest date/time during 'Business' hours of scheduleID. // Example usage for dictionary default (the 8 to 5 except weekends and holidays calender) // javascript:hoursFromHere(gs.nowNoTZ(),4,'090eecae0a0a0b260077e1dfa71da828', true); var startDateGDT = new GlideDateTime(startDateSTR); var durationMS = durationHRS * 3600000; var endDateGDT = GlideDateTime(); var schedule = new GlideSchedule(scheduleID); if (exactly == undefined || exactly) { // Go find an end date/time which gives us the exact number of business duration hours // given the schedule if (schedule) { var durationGD = new GlideDuration(durationMS); endDateGDT = schedule.add(startDateGDT, durationGD); } else { var startDateMS = startDateGDT.getNumericValue(); endDateGDT.setNumericValue(startDateMS + durationMS); } } else { // This is not an exact business time request. It is a minimum amount of calendar time request. // Go find the earliest end date/time, that is during business hours, and gives us at least the required number of // duration hours. // Assume first we don't run into non-business hours var startDateMS = startDateGDT.getNumericValue(); endDateGDT.setNumericValue(startDateMS + durationMS); // Now check to see if we did. var timeTillNextBusinessWindowMS = schedule.whenNext(endDateGDT); if (timeTillNextBusinessWindowMS > 0) { endDateGDT.setNumericValue(startDateMS + durationMS + timeTillNextBusinessWindowMS); } } return endDateGDT.getValue(); // Return in GMT timezone } var startDateTime = new GlideDateTime(); //take our start date var endDateTime = new GlideDateTime(hoursFromHere(startDateTime, 90, '090eecae0a0a0b260077e1dfa71da828', true)); ///find 10 9-hour business days as a date var dur = new DurationCalculator(); dur.setSchedule('090eecae0a0a0b260077e1dfa71da828'); //use schedule to check hours dur.calcScheduleDuration(startDateTime.getValue(), endDateTime.getValue()); //check difference var secs = dur.getSeconds(); var totalSecs = dur.getTotalSeconds(); answer = totalSecs; //set approval date 5 business days out
This code will allow you to adjust a date by a certain number of days in a client script without having to rely on Glide Ajax or a Widget for server-side code. var dateMS = getDateFromFormat(newValue, g_user_date_format); dateMS += 28 * 24 * 60 * 60 * 1000; // first variable is days, rest is static (28 days) var newDT = new Date(); newDT.setTime(dateMS); g_form.setValue('end_access', formatDate(newDT, g_user_date_format)); OR var today = new Date(); var dateMS = getDateFromFormat(today, g_user_date_format); dateMS += 30 * 24 * 60 * 60 * 1000; // first variable is days, rest is static (30 days) var newDT = new Date(); newDT.setTime(dateMS); g_form.setValue('day_limit', formatDate(newDT, g_user_date_format));
This code will allow you to verify a date is NOT earlier than today in a client script without having to rely on Glide Ajax or a Widget for server-side code. NOTE: The selectedDateNum variable requires a newValue, this can be adjusted to a getValue of a date field. function onChange(control, oldValue, newValue, isLoading, isTemplate) { if (isLoading || newValue === '') { return; } var selectedDateNum = getDateFromFormat(newValue,g_user_date_time_format); var today_date = new Date(); var today_dateStr = formatDate(today_date, g_user_date_time_format); var todayDateNum = getDateFromFormat(today_dateStr, g_user_date_time_format); if(selectedDateNum < todayDateNum) { g_form.showErrorBox("expected_start","Expected date cannot be less than today's date",true); } else { g_form.hideFieldMsg('expected_start',true); } }
Scripted Approvals in Flow Designer