Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Objects in function to return multiple data

davidgustafsson
Tera Expert

I am trying to create a function that returns multiple data by using a object as return value. The issue is that this is first time I am defining an object and obviously did not understand how it works. Please help me in the right direction as I can“t find anything googling this. What could be wrong with?

This is a Business rule that run after.

 

(function executeRule(current, previous /*null when async*/)
{
var returnValue = taskFieldsWithUsers(current.sys_id);  /returning a 
var users = returnValue.users;
var nrOfUsersFound = returnValue.i;
var fieldsUsersFoundIn = returnValue.fieldsUsersFoundIn;

gs.log('This is the users found (totally ' + nrOfUsersFound + ' users) fields: ' + fieldsUsersFoundIn);
gs.log('This is the users found (totally ' + nrOfUsersFound + ' users): ' + users);

}

function taskFieldsWithUsers(taskID)
{
var returnValue = {};
returnValue.i = 0;
var task = new GlideRecord('task');
task.addQuery('sys_id',taskID);
task.query();
while (task.next())
{
//var users = [];var i = 0;var fieldsUsersFoundIn = "";
//add caller fƤltet

//Add any user in field sys_updated_by to list to be verified
if (!task.sys_updated_by.nil())
{
var updatedByUser = new GlideRecord('sys_user');
updatedByUser.addQuery('user_name',task.sys_updated_by);
updatedByUser.query();
while (updatedByUser.next())
{
returnValue.users[i] = updatedByUser.sys_id;
returnValue.fieldsUsersFoundIn = returnValue.fieldsUsersFoundIn + "updated_by, ";
gs.log('This is the : ' + task.sys_updated_by);
returnValue.i++;
}
}

//Add any user in field assigned_to to list to be verified
if (!task.assigned_to.nil())
{
returnValue.users[i] = task.assigned_to;
returnValue.fieldsUsersFoundIn = returnValue.fieldsUsersFoundIn + "assigned_to, ";
returnValue.i++;
}

}

return returnValue;

}

8 REPLIES 8

you should encode each individual JSON object and then add them to the array. and while reading also, read the array and then decode individual objects.

-Anurag

please mark my answer correct and close the thread if the issue is resolved.

-Anurag

davidgustafsson
Tera Expert

t_anurag: I never got your version to work. I guess that I was not good enough at JSON. Anyhow I found out how to do it with normal server side coding. So here are the final version that I implemented:

(function executeRule(current, previous /*null when async*/) {
	var fields = 'sys_updated_by,assigned_to,closed_by,sys_created_by,opened_by,additional_assignee_list,watch_list,u_inc_caller';
	var userList = taskFieldsWithUsers(current.sys_id,fields);
	var k=0;
	while (k<userList.length && k < 50)
	{
		gs.log(current.number + ' Testutskriften lista user '+ k +': ' + userList[k].user + ' in field: ' + userList[k].fieldUserFoundIn);
		k++;
	}	
	
})(current, previous);


function taskFieldsWithUsers(taskID,fields)
{
	var fieldlist = fields.split(',');
	var j = 0;
	var returnValueList = [];
	var task = new GlideRecord('task');
	task.addQuery('sys_id',taskID);
	task.query();
	while (task.next())
	{
		for (j = 0;j < fieldlist.length; j++)
		{
			var returnValue = {};
	//Special case for fields that are not stored as a reference to retrieve the sys_id
			if (fieldlist[j] == 'sys_updated_by' || fieldlist[j] == 'sys_created_by')
			{
				var uid = new GlideRecord('sys_user');
				uid.addQuery('user_name',task[fieldlist[j]]);
				uid.query();
				while (uid.next())
				{
					returnValue.user = uid.sys_id;
					returnValue.fieldUserFoundIn = fieldlist[j];					
				}
			}
			else
			{
				returnValue.user = task[fieldlist[j]];
				returnValue.fieldUserFoundIn = fieldlist[j];
			}
			returnValueList[j] = returnValue;
		}
	}
	return returnValueList;
}

 

as long as it worked šŸ™‚

-Anurag