The CreatorCon Call for Content is officially open! Get started here.

Array push not working properly!!

gokulraj
Giga Expert

Hi Team,

Below is my script include and catalog clien script code:

catalog clien script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

//Type appropriate comment here, and begin script below
var arr=[];
arr.push(g_form.getValue('watch_list'));

for(var i =0;i<arr.length;i++)
{
var ga = new GlideAjax('watch');
ga.addParam('sysparm_name', 'getEmail');
ga.addParam('sysparm_user_name', arr[i]);
ga.getXML(HelloWorldParse);
}

function HelloWorldParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
g_form.setValue('short_description',answer);


}

}

 

Script include

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

getEmail: function()
{
var abc=[];
var et = this.getParameter('sysparm_user_name');
gs.log('check value'+et);
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id','IN',et);
gr.query();
while(gr.next())
{
abc.push(gr.email);

}
return abc.join(',');
},

type: 'watch'
});

 

I am getting output like below

When I add more than one user , the 1st user's email ID is replaced by second one,

Let say user 1 email ID : abc@test.com

           user 2 email ID : xyz@test.com

When I add the user 1 it shows like this abc@test.com,

When I add the second user It looks like this : xyz@test.com,xyz@test.com

 

But what I need is like below

abc@test.com,xyz@test.com

 

Can anyone help me to findout where the exact issue is??

Note: please refer this URL for more details

1 ACCEPTED SOLUTION

Harsh Vardhan
Giga Patron

here i have updated my script in below thread.

 

https://community.servicenow.com/community?id=community_question&sys_id=fb67f089db9cbf44feb1a851ca96...

 

Updated Script:

 

Script Include:

 

var watch = Class.create();
watch.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getEmail: function()
	{
		var abc=[];
		var arr=[];
		var et = this.getParameter('sysparm_user_name');
		gs.log('value of is val length'+et);
		var res = et.split(',');
		gs.log('length of array is'+res.length);
		for(var i=0;i<res.length;i++){
		var gr = new GlideRecord('sys_user');
		gr.get('sys_id',res[i]);
		abc.push(gr.email);
		}
		return abc.join(',');
	},
	
	type: 'watch'
});

 

client Script:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}
	
	//Type appropriate comment here, and begin script below
	
	var abc= g_form.getValue('watch_list').toString();
	
	var ga = new GlideAjax('watch');
	ga.addParam('sysparm_name', 'getEmail');
	
	ga.addParam('sysparm_user_name', abc);
	
	ga.getXML(HelloWorldParse);
	
	
	
	
	function HelloWorldParse(response) {
		var answer = response.responseXML.documentElement.getAttribute("answer");
		
		g_form.setValue('description',answer);
	}
	
	
}

 

View solution in original post

6 REPLIES 6

Jim Coyne
Kilo Patron

Change this:

abc.push(gr.email);

...to this:

abc.push(gr.getValue("email"));

This will retrieve a string version of the email field instead of just a pointer, which is why you were getting duplicates.

I have tried your suggestion but it returns "null"

I think the problem is in while loop , it doesn't the length so it is updating the lastly entered user's email ID.

Try using below code:

abc.push(gr.email+'');

Do those records actually have email addresses?

The problem is definitely the use of just "gr.email".  That will insert a pointer into the array and the array values will change as you loop through the record set.  You could try "gr.email.toString()" as an alternative.