Server Scripts

 

GlideRecord

var count = 0;
var gr = new GlideRecord('pm_project'); 
gr.addQuery('state', 2);   //Active
gr.addQuery('number', 'PRJ0146055');  
gr.addEncodedQuery('u_sap_stateIN30^u_sap_state=30'); 
gr.query();
//gs.info("Records in Project table: " + gr.getRowCount());
while(gr.next()){
    count++;
    gr.u_sap_state = 90;
   // gr.update();
    gs.print (gr.sys_id);
}
gs.print ("DONE " + count);

Calling Script Include from Server

    var getEDP = new BS_Company();
    var orgInfo = getEDP.getUsersCompanyExtendedDemandProcess(gs.getUserID());
    if (orgInfo == true) {
    }

GlideDateTime and GlideDate

To get this black background put the script into a background script and copy it 
#### Using GlideDateTime and GlideDate
var todaydt1 = new GlideDateTime();
Result:  2023-07-11 13:48:32  // This is todays date and time

var gdt = new GlideDateTime(current.end_date);  // Gets the date and Time
var plannedenddate = gdt.getDate();  // will return  2023-07-11

var todaydt2 = new GlideDate();
Result:  2023-07-11     // This is todays date
gs.log("JJJ1 Todays date: " + todaydt1 + " --- " + todaydt2, "JJJJ");

#### Use GlideDateTime to subtract between 2 dates and get only the number of days
var taskGr = new GlideRecord('sc_task');
taskGr.addEncodedQuery("stateIN-5,1^assignment_group=5948b2abdbd513009a9867a3ca961903");
taskGr.query();
if(taskGr.next()) {  
var currDate = new GlideDateTime();
    var pastDate = new GlideDateTime(taskGr.sys_updated_on);
    var dur = new GlideDuration();
    var dur = GlideDateTime.subtract(pastDate,currDate);
    var days = dur.getDayPart();
  gs.print("No of days:" + days + " ---" + dur.getDisplayValue());
}
gs.print("DONE");

Checks if a date lies between 2 dates.

/* 
This code checks if date lies between 2 dates.
Additionally regarding the 2 dates, they are altered to the first and last day of their respective months. 
*/

var bookingdate = '2023-03-25';
var bd = new GlideDateTime(bookingdate).getNumericValue();
var rpSysId = '67a340b01b4e2d90afe0ebd9bb4bcb04';
var gr = new GlideRecord('resource_plan');
gr.addQuery('sys_id', rpSysId);
gr.query();
while (gr.next()){
        var strtb = changedatefirstdayofthemonth(gr.start_date);
var strt = new GlideDateTime(strtb).getNumericValue();
        var edrte = changedatelastdayofthemonth(gr.end_date);
edrt = new GlideDateTime(edrte).getNumericValue();
        gs.print("Bookingdate: " + bookingdate + "(" + bd + ") " );
gs.print("Start date:  " + gr.start_date + "(" + strtb + ")(" + strt + ")");
gs.print("End date:    " + gr.end_date + " (" + edrte + ") (" + edrt + ")");
gs.print("---------"); 
    if (strt <= bd && edrt >= bd ){
gs.print ("The bookingdate is BETWEEN the start and end dates");
} else  {
gs.print ("The bookingdate is OUTSIDE the start and end dates");
}
}
gs.print ("DONE");
function changedatefirstdayofthemonth(strdate) {
    var yr = strdate.substring(0, 4);
    var mth = strdate.substring(5, 7);
    var x = daysInMonth(yr,mth);
    return yr + "-" + mth + "-" + "01";
}
function changedatelastdayofthemonth(strdate) {
    var yr = strdate.substring(0, 4);

    var mth = strdate.substring(5, 7);
    var x = daysInMonth(yr,mth);
    return yr + "-" + mth + "-" + x;
}
function daysInMonth(year, month) {
var d = new Date(year, month, 1);
d.setDate(d.getDate() - 1);
return d.getDate();
}

Using Dates on an activity in a workflow dateDiff

// Using Date on an activity in a workflow
var myDate = new GlideDateTime(current.variables.u_last_work_day);
//gs.log('myDate: ' +myDate,'EGP');
var actDate = new GlideDateTime().getDate();
//gs.log('actDate: ' +actDate,'EGP');
var date_state = myDate.onOrBefore(actDate);
//gs.log('answer: ' +answer,'EGP');
if (date_state == true){
answar = 0;
} else {
answer = gs.dateDiff(gs.nowDateTime(), current.variables.u_last_work_day, true);
 // if the 3rd option is true it will return the number of seconds
}

Regarding Dates from AD (Active Directory)

When updating dates from AD to ServiceNow
This code is from a field map in a Transform Map.
This is the source script. It converts the nanoseconds to a date. But it leaves the hours, mins and secs as 00.00.00.
In case Im still at Ronal, the code is in the import Users!
Notice it uses DateTimeUtils - Global
answer = (function transformEntry(source) {
   
    var dtUtil = new DateTimeUtils();

    var n = source.u_lastlogontimestamp;
    var s = n.toString();
   
    if (s.charAt(0) == 1) {
        dtUtil = new DateTimeUtils();
        var gDate = dtUtil.int8ToGlideDateTime(n);
        return gDate;
    } else {
        return null;
    }

})(source);


Comments

Popular posts from this blog

Email Templates and Emails

Arrays

ServiceNow tips