HPSM JavaScript Snippets


Maybe you already noticed that I’ve started writing ServiceNow posts…well, I’ve moved my focus on this awesome platform! ServiceNow is not only ITSM, ServiceNow is ITAM, ITOM, also ITSM, PPM, HR, everything!!

Before completely leaving HP Service Manager, I’d like to write my last post on this topic and share with you some JavaScript functions I’ve used during last years, hoping this will help someone of you 🙂

function callLinkline(fileRecord, fieldName, linkName) {
 var strRc = new SCDatum();
 
 var rteNames = new SCDatum();
 rteNames.push("record"); 
 rteNames.push("name"); 
 rteNames.push("string1"); 
 rteNames.push("prompt"); 
 rteNames.push("boolean1"); 
 
 var rteValues = new SCDatum(); 
 rteValues.push(fileRecord);
 rteValues.push(fieldName);
 rteValues.push(linkName);
 rteValues.push("fill");
 rteValues.push(true);
 
 system.functions.rtecall("callrad", strRc, "us.link", rteNames, rteValues, false);
 
 return fileRecord;
}

function callRadProcess(record) {
 var rteNames = new SCDatum();
 var rteValues = new SCDatum();
 var rteReturnValue = new SCDatum();
 
 rteNames.push("file"); 
 rteNames.push("name");
 rteNames.push("boolean1");
 
 rteValues.setType(8);
 rteValues=system.functions.insert(rteValues, 0, 1, record);
 rteValues=system.functions.insert(rteValues, 0, 1, "<process_name>");
 rteValues=system.functions.insert(rteValues, 0, 1, true);
 
 system.functions.rtecall("callrad", rteReturnValue, "se.call.process", rteNames, rteValues, false);
}

function callRadScript(record) { 
 var rteNames = new SCDatum();
 var rteValues = new SCDatum();
 var rteReturnValue = new SCDatum();
 
 rteNames.push("file"); 
 rteNames.push("name"); 
 rteNames.push("boolean1"); 
 
 rteValues.setType(8);
 rteValues=system.functions.insert(rteValues, 0, 1, "<record>");
 rteValues=system.functions.insert(rteValues, 0, 1, "<script_name>");
 rteValues=system.functions.insert(rteValues, 0, 1, true);
 
 system.functions.rtecall("callrad", rteReturnValue, "script.execute", rteNames, rteValues, false); //false to run in same thread, true to run in new thread
}

function createActivity() {
 var fAct = new SCFile("activity");
 fAct.number = "<ticket_id>";
 fAct.datestamp = system.functions.tod();
 fAct.operator = system.functions.operator();
 fAct.type = "activity_type";

 var arrDesc = new Array();
 arrDesc[0] = "<activity_description>";
 fAct.description = arrDesc;
 fAct.cust_visible = false;

 //Search for calculation of negdatestamp in sc.activity RAD application:
 //negdatestamp in $L.activity='31/12/2199 17:00:00' - tod()
 fAct.negdatestamp=system.functions.parse_evaluate("'31/12/2199 17:00:00' - tod()", 1);
 
 var rcAct = fAct.doInsert();
}

function createEmailSchedule(recipients) {
 var newSchedule = new SCFile("schedule");
 var today = new Date();
 
 newSchedule.application = "message.bg";
 newSchedule._class = "problem";
 newSchedule.name = "message processor record";
 newSchedule.expiration = today;
 newSchedule.strings[0] = "1";
 newSchedule.strings[1] = "email";
 newSchedule.strings[3]= "Email subject..."; 
 newSchedule.times[0] = today;
 
 var Descr = new Array();
 Descr.push("<html>");
 [...]
 Descr.push("<html>");
 newSchedule.description = Descr;
 
 if (newSchedule.description.length() == 0) {
 print("Schedule not created - Description length is 0");
 return -2;
 }
 
 newSchedule.strings1 = recipients;
 
 var rc = newSchedule.doInsert();
 if (rc == RC_SUCCESS) {
 print ("Schedule created successfully");
 return 0;
 }
}

/*
dateAddDuration
Parameter:
 date - sc date type - SC date. Should be val’d to type 3.
 dura - string - an SC formatted duration. "-"'s are allowed. MUST be less than 23:59:59
 
Returns:
 xmldate - string - SC formatted date as a string.
 
Example usage:
 var now = system.functions.tod();
 var dur = '00:03:00';
 print("now : " + system.functions.val(now, 2));
 print("then: " + dateAddDuration(now, dur))
*/

function dateAddDuration(date, dura) {
 // Turn the SC date string into an XML date Object
 // We will use the addDuration method below.
 var xmldate = new XMLDate(date);
 
 // Split on the ' '.
 var temp = dura.split(' ');
 
 // If no days were submitted, temp.length is only
 // 1, so we set days to "0"
 var days = (temp.length > 1 ) ? temp[0] : "0";
 
 // Here we figure out if it is a negative duration
 sign = ( days[0] == '-' ) ? "-" : "";
 
 // Then we strip off the - because we need it
 // infront of the "P"
 days = days.replace('-', '');
 
 // If no days were submitted, temp.length is only
 // 1, so we use the first element of the array
 var hours = (temp.length > 1) ? temp[1] : temp[0];
 
 // The days in XML Dates need all this other stuff
 // so we concatenate here. Then we add the hours
 xmldate.addDuration(sign + "P" + days + "D");
 xmldate.addDuration(hours);
 // Finally, return the SC formatted string.
 return xmldate.getSCDateTimeString();
}

function dateDiff(date1, date2) {
 date1 = Math.floor((new Date(date1).getTime()) / 1000);
 date2 = Math.floor((new Date(date2).getTime()) / 1000);
 
 days = Math.floor((date2-date1)/(60*60*24));
 print("days: " + days);
 date2 = date2 - (days*60*60*24);
 
 hours = padDigits(Math.floor((date2-date1)/(60*60)), 2);
 date2 = date2 - (hours*60*60);
 
 minutes = padDigits(Math.floor((date2-date1)/(60)), 2);
 date2 = date2 - (minutes*60);
 
 seconds = padDigits(date2-date1, 2);
 diff = system.functions.val(days+" "+ hours+ ":"+ minutes+ ":"+ seconds, 2);
 
 return diff;
}

function padDigits(n, totalDigits) {
 n = n.toString();
 var pd = '';
 if (totalDigits > n.length) {
 for (i=0; i < (totalDigits-n.length); i++) {
 pd += '0';
 }
 return pd + n.toString();
 }
}

function doSortedQuery() {
 var fSM = new SCFile("<file_name>");
 var rc = fSM.doSelect("<query>");

 var orderFields = new Array();
 orderFields.push("<field_1>");
 orderFields.push("<field_2>");
 orderFields.push("<field_3>");
 
 var ro = system.functions.setsort(fSM, orderFields, 0); //0 = ASC, 1 = DESC
 // Javascript function to sort fields: SCFile.setOrderBy(orderFields, 0/1);
 
 if (rc == RC_SUCCESS) {
 while (rc == RC_SUCCESS) {
 print(fSM.<field_name>);
 rc = fSM.getNext();
 }
 }
}

function getCommaValue(str) {
 var appo;
 if (system.functions.nullsub(str, "") != "") {
 appo=str.toString().replace(/\B(?=(\d{3})+(?!\dd))/g, ",");
 var pos=system.functions.index(".", appo);
 if (pos==0) {
 appo=appo+".00";
 }
 var len=system.functions.lng(appo);
 if (pos == (len-1)) {
 appo=appo+"0";
 }
 return appo;
 } else {
 appo="0";
 return appo;
 }
}

function getLeft(str, n) {
 if (n <= 0)
 return "";
 else if (n > String(str).length)
 return str;
 else
 return String(str).substring(0, n);
}

function getLicenseInfo() {
 var license = system.functions.get_module_license();

 for (var i in license) {
 var module = system.functions.strraw(license[i], ",");
 var module1 = module.split(",");

 if (module1[0] == "Self Service Ticketing") {
 print(module1[0] + " " + module1[2] + "(Unlimited)");
 } else {
 print(module1[0] + " Named License " + module1[1] + "(" + module1[3] + ")" + " Float License " + module1[2] + "(" + module1[4] + ")");
 }
 }
}

function getNumber(class) {
 var RC = new SCDatum();
 var ID = new SCDatum();
 var rcGet = system.functions.rtecall("getnumber", RC, ID, class);

 return ID;
}

function getRight(str, n) { 
 if (n <= 0) 
 return ""; 
 else if (n > String(str).length) 
 return str; 
 else { 
 var iLen = String(str).length; 
 return String(str).substring(iLen, iLen - n); 
 } 
}

function htmlToString(str) {
 str = str.replace(/</g, "&lt;");
 str = str.replace(/>/g, "&gt;");
 
 return str;
}

function isResourceLocked(lName) {
 var locks, lCount;
 var lRes = false;
 locks=system.functions.locks();
 lName=system.functions.str(lName);
 for (lCount = 0; lCount < locks.length(); lCount++) {
 if (locks[lCount].toString().indexOf(lName)>=0) {
 lRes = true;
 }
 }
 
 return lRes;
}

function toDateString(data) {
 var datatemp = new Date();
 datatemp = data;
 
 var varday = datatemp.getDate();
 if (varday < 10)
 varday = "0" + varday;

 var varmonth = datatemp.getMonth();
 varmonth += 1;
 if (varmonth < 10)
 varmonth = "0" + varmonth;

 var varyear = datatemp.getFullYear();

 var varhour = datatemp.getHours();
 if (varhour < 10)
 varhour = "0" + varhour;

 var varminute = datatemp.getMinutes();
 if (varminute < 10)
 varminute = "0" + varminute;

 var varseconds = datatemp.getSeconds();
 if (varseconds < 10)
 varseconds = "0" + varseconds;

 var retValue = varday + "/" + varmonth + "/" + varyear + " " + varhour + ":" + varminute + ":" + varseconds;

 return retValue;
}

function trim(str) {
 if (str != null) {
 str = str.replace(/(^\s*)|(\s*$)/g, "");
 }
 
 return str;
}

//logMsg is an Array to create the CSV format
function writeToFile(logMsg) { 
 filePath = "D:\\HPSM.csv"; //just an example, use your path...
 isMode = "a";
 fileObject = logMsg + "\n";
 
 writeFile(filePath, isMode, fileObject);
}

Bye guys!  🙂

Tagged with: , ,
Posted in HPSM, JavaScript

Leave a comment

Support this blog

Hi, first of all thank you for visiting my blog!

As you probably already noticed, this blog is completely accessible and free, there's no restricted content and everybody can find info about HP Service Manager.

I spend most of my free time trying to do my best to help people in trouble with technical topics on HPSM (plus some more general articles). If you find my articles interesting, know that you can support my work by donating using the button below - this will encourage me to go ahead with new articles ;-)

Thanks in advance!

Donate Button with Credit Cards

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 40 other subscribers