Blog

Syntax editor macros

Script macros provide shortcuts for typing commonly used code. To insert macro text into a script field, enter the macro keyword followed by the Tab.

Here is a collection of script macros I am currently using. Let us know in the comments if you have a macro to add to the list!

Name: aclscript
Application: Global
Comments: ACL Script
Text:

(function() {
	answer = false;
	if (condition) {
		answer = true;
	}
})();

Name: csajax
Application: Global
Comments: Client Side Ajax Example
Text:

function onLoad() {
	var ga = new GlideAjax('global.SNEClientUtil');
	ga.addParam('sysparm_name', 'getUserInfo');
	ga.addParam('sysparm_user', g_form.getValue('requested_for'));
	ga.getXML(processAnswer);
}
function processAnswer (response)  {
	var answer = response.responseXML.documentElement.getAttribute("answer");
	var obj = JSON.parse(answer);
	g_form.setValue('department', obj.department);
}

Name: csajax
Application: Global
Comments: Client Side Ajax Example
Text:

function onLoad() {
	var ga = new GlideAjax('global.SNEClientUtil');
	ga.addParam('sysparm_name', 'getUserInfo');
	ga.addParam('sysparm_user', g_form.getValue('requested_for'));
	ga.getXML(processAnswer);
}
function processAnswer (response)  {
	var answer = response.responseXML.documentElement.getAttribute("answer");
	var obj = JSON.parse(answer);
	g_form.setValue('department', obj.department);
}

Name: csajaxsi
Application: Global
Comments: Script Include for Client Side Ajax Example
Text:

var SNEClientUtil = Class.create();
SNEClientUtil.prototype = 
	Object.extendsObject(AbstractAjaxProcessor, {
	getData: function() {
		var usr = this.getParameter('sysparm_user');
		var obj = {};
		var gr = new GlideRecord('sys_user');
		gr.addQuery('sys_id', usr);
		gr.query();
		if(gr.next()) {
			obj.phone = gr.phone.toString();
		}
		var json = new JSON();
		var data = json.encode(obj);
		return data;
	},
	type: 'SNEClientUtil'
});

Name: csalert
Application: Global
Comments: Client Side Alert
Text:

function onChange(control, oldValue, newValue, isLoading) {
 if (isLoading || newValue == '') {
return;
 }
 if (newValue == 'mike_awesome') {
alert('Yes this is true');
 }
}

Name: doc
Application: Global
Comments: Documentation Header
Text:

/**
 * Description: $0
 * Parameters: 
 * Returns:
*/

Name: findsysid
Application: Global
Comments: Find Record by Sys ID
Text:

(function() {
    var sysID = '4430191ddb8ce7c0d893f8621f9619d9';  //Replace with SysID
    var grObject = new GlideRecord('sys_db_object');
    grObject.addEncodedQuery('nameNOT LIKEts_^sys_update_nameISNOTEMPTY^nameISNOTEMPTY');
    grObject.addEncodedQuery('nameNOT LIKEnp$');
    grObject.query();
    while (grObject.next()) {
        var tableName = grObject.getValue('name');
        grTable = new GlideRecord(tableName);
        grTable.addQuery('sys_id',sysID);
        grTable.query();
        if (grTable.next()) {
            gs.print(gs.getProperty('glide.servlet.uri') + tableName + '.do?sys_id=' + sysID);
        }
    }
})();

Name: findtasktypes
Application: Global
Comments: Find Task Types Used
Text:

findTaskTypesUsed();
function findTaskTypesUsed() {
var count = new GlideAggregate('task');
count.addAggregate('COUNT', 'sys_class_name');
count.query();
while (count.next()) {
var taskType = count.sys_class_name;
var taskTypeCount = count.getAggregate('COUNT', 'sys_class_name');
gs.log("The are currently " + taskTypeCount + " tasks with a task type of " + taskType);
}
}

Name: findunique
Application: Global
Comments: Find Unique Values
Text:

findUnique('cmdb_ci_computer','os');//put the table and field you want to find unique
function findUnique(table,field) {
  var au = new ArrayUtil();
  var uniqueArray = [];
  var gr = new GlideRecord(table);
  gr.orderBy(field);
  gr.addNotNullQuery(field);
  gr.query();
  while (gr.next()) {
    uniqueArray.push(gr[field].toString());
  }
  gs.print('Unique Values: ' +au.unique(uniqueArray));
  //return au.unique(uniqueArray);
}

Name: for
Application: Global
Comments: For Array Loop
Text:

for (var i=0; i< myArray.length; i++) {
 //myArray[i];
}

Name: grdelete
Application: Global
Comments: Deletes a single record.
Text:

var gr = new GlideRecord("$0");
gr.addQuery('query');
gr.query();
if (gr.next()){
gr.deleteRecord();
}

Name: grencodedquery
Application: Global
Comments: Adds an encoded query to other queries that may have been set.
Text:

var gr = new GlideRecord("$0");
gr.addEncodedQuery(queryString);
gr.query();
if (gr.next()) {
   
}

Name: grget
Application: Global
Comments: Returns the specified record in an instantiated GlideRecord object.
Text:

var gr = new GlideRecord("$0");
gr.get($1);

Name: grgetdisplay
Application: Global
Comments: Retrieves the display value for the current record.
Text:

var gr = new GlideRecord("$0");
gr.get($1);
gs.info(gr.getDisplayValue());

Name: grinsert
Application: Global
Comments: Inserts a new record using the field values that have been set for the current record.
Text:

var gr = new GlideRecord("$0");
gr.initialize();
gr.setValue("field","value"); //set field values
gr.insert();

Name: grlog
Application: Global
Comments: Adds a log statement for debugging a gliderecord query
Text:

var gr = new GlideRecord("$0");
gr.addQuery("name", "value");
gr.query();
gs.log('Function| gr Query: ' + gr.getEncodedQuery() + ' = ' + gr.getRowCount());
if (gr.next()) {
   
}

Name: grorquery
Application: Global
Comments: Example GlideRecord Or Query
Text:

var gr = new GlideRecord('$0');
var qc = gr.addQuery('field', 'value1');
qc.addOrCondition('field', 'value2');
gr.query();
while (gr.next()) {

}

Name: grquery
Application: Global
Comments: Example GlideRecord Query
Text:

var gr = new GlideRecord("$0");
gr.addQuery("name", "value");
gr.query();
if (gr.next()) {
   
}

Name: grupdate
Application: Global
Comments: Updates the GlideRecord with any changes that have been made. If the record does not exist, it is inserted.
Text:

var gr = new GlideRecord("$0");
gr.addQuery('name','=','');
gr.query();
if (gr.next()){
gr.setValue("field","value"); //set field values
gr.update();
}

Name: grupdatenoworkflow
Application: Global
Comments: Enables or disables the update to the fields sys_updated_by, sys_updated_on, sys_mod_count, sys_created_by, and sys_created_on. This is often used for manually updating field values on a record while leaving historical information unchanged.
Text:

var gr = new GlideRecord("$0");
gr.addQuery('name','=','');
gr.query();
if (gr.next()){
gr.autoSysFields(false);  // Do not update sys_updated_by, sys_updated_on, sys_mod_count, sys_created_by, and sys_created_on
gr.setWorkflow(false);    // Do not run any other business rules
gr.setValue("field","value"); //set field values
gr.update();
}

Name: if
Application: Global
Comments: If statement
Text:

if (condition) {
	//block of code to be executed if the condition is true
} else { 
	//block of code to be executed if the condition is false
}

Name: messageerror
Application: Global
Comments: Error Message
Text:

gs.addErrorMessage(gs.getMessage("$0"));

Name: messageinfo
Application: Global
Comments: Info Message
Text:

gs.addInfoMessage(gs.getMessage("$0"));

Name: switch
Application: Global
Comments: switch
Text:

switch(expression) {
	case 0:
		//Case 0 code block
		break;
	case 1:
		//Case 1 code block
		break;
	default:
		//Other cases code block
}

Name: try
Application: Global
Comments: Try
Text:

try {
	//block of code to try
}
catch(err) {
	//block of code to handle errors
	//e.g. gs.log(err);
} 
finally {
	//block of code to be executed regardless of the try / catch result
}

Name: while
Application: Global
Comments: While loop
Text:

while (condition) {
	//code block to be executed
}