Comparing List Collector Values with another variable, if it matches show alert

Nasir1
Tera Expert

Hi,

Sorry it might be a noob question but I have catalog item that has a list collector (employee_current_roles) which auto-populates user's roles with all the roles user is member of, I have another variable (role) on the form that is reference field and users can pick up any roles and log the request.

What I want to do is If any of the roles in list collector matches the value in referenced field I want a pop up to say User is already member of the role but I am not sure if what I am doing is correct, please help.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var urole = g_form.getValue('role');
    var gr = g_form.getValue('employee_current_roles'); //  getting the List Collector Variable value
    var ar = gr.split(','); // split the list collector by comma
    
    for (var i = 0; i < ar.length; i++); 

    if (ar.indexOf(urole) == -1) //if field values are the same then send alert
       { 
        alert('User is already Member of the Selected role');
        g_form.clearValue('role');
    }

}
   

 

1 ACCEPTED SOLUTION

That's strange issue,

Can you try adding below alert to see what values those variables are storing:

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	var urole = g_form.getValue('role');
	var gr = g_form.getValue('employee_current_roles'); //  getting the List Collector Variable value

	if (gr.indexOf(urole) > -1) //if field values are the same then send alert
	{ 
                alert("gr value :"+gr+" urole : "+urole);
		alert('User is already Member of the Selected role');
	}

}

with new alert you might understand the exact issue else show me the alert data you are getting so that I can help you further.

Let me know if you have any further queries.

Please mark this as Correct or Helpful if it helps.

Thanks and Regards,
Abhijit
Community Rising Star 2022

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

View solution in original post

32 REPLIES 32

oh no !

try this i have update the script 

let me know if its not working  and share the alert that you getting for alert(gr); if it s not working

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var urole = g_form.getValue('role');
    var gr = g_form.getValue('employee_current_roles'); //  getting the List Collector Variable value
alert(gr);
 
    if (gr.indexOf(urole)!=-1) //if field values are the same then send alert
       { 
        alert('User is already Member of the Selected role');
        g_form.clearValue('role');
    }

}

 

Abhijit4
Mega Sage

Try below,

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	var urole = g_form.getValue('role');
	var gr = g_form.getValue('employee_current_roles'); //  getting the List Collector Variable value

	if (gr.indexOf(urole) > -1) //if field values are the same then send alert
	{ 
		alert('User is already Member of the Selected role');
	}

}

Let me know if you have any further queries.

Please mark this as Correct or Helpful if it helps.

Thanks and Regards,
Abhijit
Community Rising Star 2022

 

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

Abhijit4
Mega Sage

Try below,

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	var urole = g_form.getValue('role');
	var gr = g_form.getValue('employee_current_roles'); //  getting the List Collector Variable value

	if (gr.indexOf(urole) > -1) //if field values are the same then send alert
	{ 
		alert('User is already Member of the Selected role');
	}

}

Let me know if you have any further queries.

Please mark this as Correct or Helpful if it helps.

Thanks and Regards,
Abhijit
Community Rising Star 2022

 

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

Hi Abhijit,

 

Thanks I have already tried that, @amankumar already mentioned it before but unfortunately the pop up displays everytime I change the value in the field. 

 

Regards

Nasir

Yes, it will show popup every time you change the value if that role exist, what's issue then?

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP