Posts

Showing posts with the label Server Scripts

Count Number of Days

SI Function:  daysToDueDate: function(incident) {          var totalDuration;           var now = new GlideDateTime().getLocalDate();       if (!incident.dueDate.nil()) {               var dueDate = new GlideDateTime(incident.duedate).getDate();               totalDuration = new GlideDateTime.subtract(now, dueDate);               var numberofDays = parseInt(totalDuration.getNumericValue() / 86400000);               }     return nubmerOfDays; },                                             

How to Get a list field values as comma seperated

SI Function:   inAListFied: function(incident) {     var userID = gs.getUserID();     var watchList = incident.getValue('watchlist');     if (watchList) {          var arrIncident = watchList.split(",");          var ListValues = (arrIncident.indexOf(userID) >= 0);       }     return ListValues; },

BR- Send email to Removed users in watchList

 var watchListArray = []; var oldWatchList = previous.u_watchlist.split(','); var newWatchList = current.u_watchlist.split(','); for (var i = 0; i < oldWatchList.length; i++) {   if (newWatchList.join(',').indexOf(oldWatchList[i]<0)){     watchListArray.push(oldWatchList[i].toString());   } }   gs.eventQueue("event_name",current, watchListArray.toString()); }

Types of Business Rules in SNOW

BEFORE : Use before insert/update business rule to do some validation and if validation fails stop form submission Use before insert/update business rule to set values for current record based on some other fields on the form such as calculated field AFTER : Use after insert/update business rule to insert/update data in related list table Use to update information on related objects that need to be displayed immediately, such as GlideRecord queries. ASYNC: Use to update information on related objects that do not need to be displayed immediately, such as calculating metrics and SLAs. Mostly used to call 3rd party SOAP or REST endpoints DISPLAY : Mostly used to send field value from server side to client side; for the fields which are not available on the form, this value can be used in onload, onchange, onsubmit client scripts

Background Scripts

BACKGROUND SCRIPT CODE VAULT RECORD QUERIES ACTIVE REQUESTS WITHOUT REQUESTED ITEMS (function() { var grRequest = new GlideRecord("sc_request"); grRequest.addEncodedQuery("active=true"); grRequest.query(); gs.print('Active Requests without Requested Items :('); while (grRequest.next()) { var gaRequestedItem = new GlideAggregate("sc_req_item"); gaRequestedItem.addQuery("request",grRequest.sys_id); gaRequestedItem.addAggregate('COUNT'); gaRequestedItem.query(); var req = 0; if (gaRequestedItem.next()) { req = gaRequestedItem.getAggregate('COUNT'); if (req == 0) { gs.print(grRequest.number); } } } })(); DISTINCT OPERATING SYSTEMS OF CIS (function() { var gaServer = new GlideAggregate('cmdb_ci_server'); //GlideAggregate query gaServer.addAggregate('count'); //Count aggregate (only necessary for a count of items of each OS) gaServer.orderByAggregate('count'); //Count aggregate ordering gaServe...

Glide Record Cheat Sheet

Query Can also be used in Client scripts and UI policies A standard GlideRecord query follows this format. var  gr  =   new  GlideRecord ( 'incident' ) ;   //Indicate the table to query from //The 'addQuery' line allows you to restrict the query to the field/value pairs specified (optional) //gr.addQuery('active', true); gr. query ( ) ;   //Execute the query while  ( gr. next ( ) )   {   //While the recordset contains records, iterate through them     //Do something with the records returned     if ( gr. category   ==   'software' ) {       gs. log ( 'Category is '   +  gr. category ) ;     } } UPDATE:  This same function applies to client-side GlideRecord queries! If at all possible, you should use an  asynchronous  query from the client.  See this post  for details. var  gr  =   new  GlideRecord ( 'sys_user' ) ; ...