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

Anurag Tripathi
Mega Patron
Mega Patron

I use JSON object in case i have to return more than one value. Try that.

-Anurag

davidgustafsson
Tera Expert

Thank you for your reply but I have never used JSON. Is there no way of solving this using one kind of script?

Else is there some kind of simple guide for me to follow for this?

 

var myObj = {};

myObj.x='haha';

myObj.y='boo';

var json = new JSON();

var result = json.encode(myObj);//JSON formatted string

return result;

 

 

 

and to use the returned object.

var obj = result.evalJSON();

obj.x //accessible here

obj.y //accessible here

-Anurag

davidgustafsson
Tera Expert

Great and simple!

Just one additional question to solve all my issues. How do you add to this to be able to return a list of objects, is it like below?

 

var myObj = {};

var myObjList = [];

var i=0;

var length = 2;

while (i<length){

  myObj.x='haha';

  myObj.y='boo';

  myObjList[i] = myObj;

}

var json = new JSON();

var result = json.encode(myObjList);//JSON formatted string

return result;

 

 

And to use it...

var obj = result.evalJSON();

obj[0].x //accessible here

obj[0].y //accessible here