Comparison between two string fields data

kumar37
Kilo Contributor

Hi All,

I Have a requirement to compare the two string fields data and the difference need to be populated in the 3rd field.

1st field name:  user input 

2nd field name: existing data

3rd field name: missing data:

1 st field data :  1,2,3,4,5,6

2nd field data : 2,5,6

 i need to compare the data between 1st field and 2nd field and difference should be populate in 3rd field.

3rd field data: 1,3,4

 

Thanks in advance,

kumar

 

1 ACCEPTED SOLUTION

I created an item in my dev instance and re-did the code.  Try this you will just need to update the variable names as I created them as a, b, and c.

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	//Type appropriate comment here, and begin script below
	var a = g_form.getValue('a').toString().split(',');
	var b = g_form.getValue('b').toString();
	getDifference(a, b);
}
function getDifference(a, b)
{
//	var j = 0;
	var result = [];
	for (var i = 0; i < a.length; i++){
		if (b.toString().indexOf(a[i]) < 0){
			result.push(a[i]);
		}
	}
	
	g_form.setValue('c', result);
}

View solution in original post

30 REPLIES 30

Hi Ankur,

thanks for your reply.

 i tried to execute this code. but it is not working in on change client script. giving the below error.

can you please help on it.

 

onChange script error: ReferenceError: ArrayUtil is not defined function onChange_change_request_u_cmdb_ci_data_3(control, oldValue, newValue, isLoading, isTemplate) { if (isLoading || newValue === '') { return; } var field1Data = g_form.getValue('u_impacted_ci'); var strArray = field1Data.split(","); var field2Data =g_form.getValue('u_cmdb_ci_data'); var str1Array = field2Data.split(","); var arrayUtil = new ArrayUtil(); var diffArray = arrayUtil.diff(strArray, str1Array); alert(diffArray); }

Hi Kumar,

That script would be server side. You want to execute it in client side?

Regards

Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

yes Ankur,

i want to execute in client script. why because am not fetching any data from database. am taking data from form fields only.

Hi Kumar,

Do you want the user to see the difference before saving the form?

If no then you can have this in after insert/update business rule and update it in backend

If yes then it has to be done in client side and you cannot use ArrayUtil in client side.

Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

yes. user should see the missing data.