Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

thank you bricast. i will check the issue

Hi bricast,

just small help.

the proposed solution is having issue with case sensitive.

input: ADCADM03,

database: adcadm03,

even though it is present in the database with lower case letters . it is giving the input as missing servers.

output missing servers: ADCADM03,

can you please help me on it.

 

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var a = g_form.getValue('u_impacted_ci').toString().split(',');
var b = g_form.getValue('u_cmdb_ci_data').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('u_missing_servers', result);

}

 

thanks,

dileep peddi

for case sensitive you can trying making the changes to the following code.

var a = g_form.getValue('u_impacted_ci').toString().toLowerCase().split(',');
var b = g_form.getValue('u_cmdb_ci_data').toString().toLowerCase();

hi bricast,

thank you. issue has been fixed.

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Kumar,

So basically it is a difference you need to get

Here is the sample script

var field1Data = '1,2,3,4,5,6';
var strArray = field1Data.split(",");
var field2Data = '2,5,6';

var str1Array = field2Data.split(",");

var arrayUtil = new ArrayUtil();
var diffArray = arrayUtil.diff(strArray, str1Array);

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