Code snippets
Virtual Agent
// change the color of the portal icon
// icons can also be updated
// add tbis to the portal's custom CSS
$sp-agent-chat-bg: white;
$sp-agent-chat-btn-close: url("/image-name-uploaded-to-image-table.png");
$sp-agent-chat-btn-open: url("/image-name-uploaded-to-image-table.png");
Field attributes
// allows displaying field contents in JSON format
json_view=true
Server side
// get user IP
GlideTransaction.get().getRemoteAddr();
// generate secure random string
GlideSecureRandomUtil.getSecureRandomString(length);
// gets the URL of current page on server side
GlideTransaction.get().getRequest().getRequestURL();
Service Portal / Catalog Item
// strip HTML
new GlideSPScriptable().stripHTML(yourHtmlStr);
// makes the g_form object available in the console in SP
var g_form = $('sp-variable-layout').scope().getGlideForm()
// loop through catalog item variables
var item = new GlideappCatalogItem.get('<sys_id of Item>');
var variables = item.getVariables();
while(variables.next()){
gs.info(variables.name + ' : ' + variables.variable_set.getDisplayValue() +' : '+ variables.type.getDisplayValue());
}
// widget finder
var widgetName = 'widget-name';
var widgetID = 'widget-id';
var searchWidgets = new global.GlideQuery('sp_widget')
.where('template', 'CONTAINS', widgetID)
.orWhere('script', 'CONTAINS', widgetID)
.orWhere('client_script', 'CONTAINS', widgetID)
.select('name', 'template', 'script', 'client_script')
.reduce(function(acc, cur){
var inHtml = cur.template.indexOf(widgetID) > -1;
var inServerScript = cur.script.indexOf(widgetID) > -1;
var inClientScript = cur.client_script.indexOf(widgetID) > -1;
return inHtml ? acc + ' HTML field of ' + cur.name
: (inServerScript ? acc + ' ServerScript field of ' + cur.name
: inClientScript ? acc + ' ClientScript field of ' + cur.name : ''
) + ';'
}, 'Widget "' + widgetName + '" is possibly used within:');
var searchVariables = new global.GlideQuery('item_option_new')
.whereNotNull('sp_widget')
.where('sp_widget.id', widgetID)
.select('name', 'question_text', 'variable_set$DISPLAY')
.reduce(function(acc, cur){
return acc + '[VSET: ' + cur.variable_set$DISPLAY + '] ' + cur.question_text + ' (' + cur.name + '); ';
}, 'Widget "' + widgetName + '" is possibly used by variables: ')
gs.info(searchWidgets);
gs.info(searchVariables);
Date / Time
var strConvertedDateTime = new GlideScheduleDateTime("2022-03-03 06:30:00").convertTimeZone("CET", "IST");
var gdtConvertedDateTime = new GlideDateTime(strConvertedDateTime);
gs.info(gdtConvertedDateTime);
Update Set
// push chosen record to active update set
var rec = new GlideRecord('cmn_schedule_span');
rec.addEncodedQuery('schedule=5b61b6130f54a600258ee64be1050ead');
rec.query();
while(rec.next()){
//Push the record into the current update set
var um = new GlideUpdateManager2();
um.saveRecord(rec);
}