add values to list type field

Lior Kaitel
Tera Guru

hello,

on the create new incident form, i have a list type field named "u_prev_callers"

i want to add each caller to the u_prev_callers list, without removing the previous caller.

so that once the caller field changes i can see all the history of the caller field.

im having trouble with the script 

how can i manage to do that?

1 ACCEPTED SOLUTION

Hi,
Please do not push the same user again.

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var pre='';
	var ccaller=current.getValue('caller_id');
	if(current.getValue('u_prev_callers'))
		pre=current.getValue('u_prev_callers').split(",");	

	if(previous.caller_id)
	{
		pre.push(previous.getValue('caller_id'));
	}
	var au=new ArrayUtil();
	current.setValue('u_prev_callers',au.unique(pre).join(","));

})(current, previous);

 

Please mark the answer as correct, If I answered your query. It will be helpful for others who are looking for similar questions.

Regards
Saurabh





Thanks and Regards,

Saurabh Gupta

View solution in original post

4 REPLIES 4

Sagar Pagar
Tera Patron

Hi @Lior Kaitel,

Can you share your scripts here? Also, I'm bit confused due to line -

i want to add each caller assigned to the caller field(caller_id) to the u_prev_callers list.

 

Please share the correct/meaningful requirement/question.

 

Thanks,
Sagar Pagar

The world works with ServiceNow

Lior Kaitel
Tera Guru

i tried with business rule:

(function executeRule(current, previous /*null when async*/ ) {
var caller_name = g_form.getValue('caller_id');
var ut = new GlideRecord("sys_user");
var gr = new GlideRecord("incident", getCaller);

function getCaller(gr) {
var caller_name = gr.caller_id;
}

var listArr = gr.u_prev_callers.toString().split(','); //creates an array of current List field values
listArr.push(caller_name);
gr.setValue('u_prev_callers', listArr.join(','));
gr.update();
current.caller_name.toString();
})(current, previous);

Hi,
Please do not push the same user again.

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var pre='';
	var ccaller=current.getValue('caller_id');
	if(current.getValue('u_prev_callers'))
		pre=current.getValue('u_prev_callers').split(",");	

	if(previous.caller_id)
	{
		pre.push(previous.getValue('caller_id'));
	}
	var au=new ArrayUtil();
	current.setValue('u_prev_callers',au.unique(pre).join(","));

})(current, previous);

 

Please mark the answer as correct, If I answered your query. It will be helpful for others who are looking for similar questions.

Regards
Saurabh





Thanks and Regards,

Saurabh Gupta

Mohit_Gupta
Tera Guru
Tera Guru

Hi @Lior Kaitel ,

 

Please create before update business rule on incident table and apply the filter caller changes

and run below code it will work

(function executeRule(current, previous /*null when async*/) {

// Add your code here


var callerid= previous.caller_id;
var arr=[];
arr.push(current.u_prev_callers);
arr.push(callerid);
current.u_prev_callers=arr.toString();


})(current, previous);

 

I hope this helps 

Please mark my answer correct if this helps you 

Thanks

Mohit