Upender Kumar
Mega Sage

Below are some more useful scripts

1. How to get data from server-side to client-side.

    Get single string value

   Create a client callable script include

var AjaxUtil = Class.create();
AjaxUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getUserDetails:function(){
//adding sysparam_ before parameter is not necessary. It is only required only with function name while calling from client side 
		var userID=this.getParameter('userID');
		var grUser=new GlideRecord('sys_user');
		if(grUser.get(userID)){
			return grUser.getDisplayValue();
		}
		return "";
		
	},
    type: 'AjaxUtil'
});

Client-side code

function onLoad() {
	//Type appropriate comment here, and begin script below
	var grAjax=new GlideAjax('AjaxUtil');
	grAjax.addParam('sysparm_name','getUserDetails');
	grAjax.addParam('userID',g_user.userID);
	grAjax.getXML(GetDetails);
	//Type appropriate comment here, and begin script below
	function GetDetails(response){
		var answer=response.responseXML.documentElement.getAttribute('answer');
		console.log('User Name:'+answer);
	}
}

Output-- check console logs

find_real_file.png

Get object/JSON

 

getUserDetails:function(){
		var user={};
		var userID=this.getParameter('userID');
		gs.info('Upender:'+userID);
		var grUser=new GlideRecord('sys_user');
		if(grUser.get(userID)){
			return JSON.stringify({'UserName':grUser.getDisplayValue(),'ID':grUser.getUniqueValue()});
		}
		return "";
		
	}

Output

find_real_file.png

Parse object values

var answer=response.responseXML.documentElement.getAttribute('answer');
var obj=JSON.parse(answer);
console.log('User Name:'+obj.UserName);
console.log('ID:'+obj.ID);

find_real_file.png

 

 Get Array

var users=[];
		var userID=this.getParameter('userID');
		var grUser=new GlideRecord('sys_user');
		if(grUser.get(userID)){
			users.push(grUser.getDisplayValue());
			users.push(grUser.getUniqueValue());
			return JSON.stringify(users);
		}
		return "";

Client-Side

var obj=JSON.parse(answer);
console.log('User List:'+obj);

Output

find_real_file.png

2. Make catalog variables read-only with client script based on the state after item submission.

function onLoad(){
		// use the correct state choice value
		if(g_form.getValue('state') == 'closed confirmed'){
			var fields = g_form.getEditableFields();
			for (var x = 0; x < fields.length; x++) {
				g_form.setReadOnly(fields[x], true);
			}
		}
	}

3. Get variables label, name, and value in BR.

var variables = current.variables.getElements();
	var str = '';
	for (var i = 0; i < variables.length; i++) {
		var question = variables[i].getQuestion();
		var variableName = question.getName();
		var variableLabel = question.getLabel();
		var variableValue = question.getDisplayValue();
		if(variableName.indexOf('Price')>-1 || variableLabel.indexOf('Price')>-1)
			continue;
		if (variableValue != '' && variableValue !='false') {
			str = str + variableLabel + ' - ' + variableValue + '\n';
		}
	}

4. Combine two object's key

var a = {
		"a": "1",
		"b": "2"
	};
	var b = {
		"c": "1",
		"d": "2"
	};
	gs.print(JSON.stringify(merge(a,b)))
	function merge(from,to){
		for (var name in from){
			var mached=false;
			for(var dt in to){
				if(dt==name)
					mached=true;
			}
			if(!mached)
				to[name]=from[name];

		}
		return to;
	}

Output

Script: {"c":"1","d":"2","a":"1","b":"2"}

 

Below is a reference to one more 

https://community.servicenow.com/community?id=community_article&sys_id=09c0e5d0db42b058a538826305961...

 

If the above scripts are helpful, Please mark helpful and bookmark.

Comments
Mohammed Kemal
Tera Guru

Hi Upender - great article with great content! Thank you for sharing!!!

Version history
Last update:
‎08-17-2021 11:21 PM
Updated by: