Onchange Client script - Using an array

matt_a
Kilo Guru

Hi, I have been tasked with moving a teams IP Subnets from an old lotus notes database over to the ServiceNow CMDB!

In order to make this work and capture their engagement, i am trying to replicate the functions from lotus in ServiceNow and wondering whether anyone would have some scripts that might assist!

I have a string field that will contain the subnet (field: subnet): e.g 10.10.10.10

I have a subnet bits(field: u_subnet_bits) value dropdown with values of: 2 - 32

I then have a calculated field which adds them together(field: u_network_address): e.g 10.10.10.10/24

Now, my knowledge of networks is pretty non existent which doesnt help me!

So, in their lotus database, they use the below formula to validate the fields.

Seems quite easy to read except that being IBM, its a bit backwards! 

I can see that I need to utilise an onchange script. The subnet field would need to go into an array to give me my 4 Vars (oct1, oct2, oct3, oct4)

I would then need to use an if and put all of the below into a switch statement and != one of the valid ranges, then add a warning message to the field.

I think I understand what to do, but array's are not really my forte! could someone point me in the right direction please?

exp := @Explode(network_address; ".");
oct1 := @Subset(exp;1);
oct2 := @Subset(@Subset(exp;2);-1);
oct3 := @Subset(@Subset(exp;-2);1);
oct4 := @Subset(exp;-1);


@If(
(Subnet_bits = "32") |
(Subnet_bits = "30" & oct4 = "0":"4":"8":"12":"16":"20":"24":"28":"32":"36":"40":"44":"48":"52":"56":"60":"64":"68":"72":"76":"80":"84":"88":"92":"96":"100":"104":"108":"112":"116":"120":"124":"128":"132":"136":"140":"144":"148":"152":"156":"160":"164":"168":"172":"176":"180":"184":"188":"192":"196":"200":"204":"208":"212":"216":"220":"224":"228":"232":"236":"240":"244":"248":"252") |
(Subnet_bits = "29" & oct4 = "0":"8":"16":"24":"32":"40":"48":"56":"64":"72":"80":"88":"96":"104":"112":"120":"128":"136":"144":"152":"160":"168":"176":"184":"192":"200":"208":"216":"224":"232":"240":"248") |
(Subnet_bits = "28" & oct4 = "0":"16":"32":"48":"64":"80":"96":"112":"128":"144":"160":"176":"192":"208":"224":"240") |
(Subnet_bits = "28" & oct4 = "0":"16":"32":"48":"64":"80":"96":"112":"128":"144":"160":"176":"192":"208":"224":"240") |
(Subnet_bits = "27" & oct4 = "0":"32":"64":"96":"128":"160":"192":"224")|
(Subnet_bits = "26" & oct4 = "0":"64":"128":"192" )|
(Subnet_bits = "25" & oct4 = "0":"128" )|
(Subnet_bits = "24" & oct4 = "0" )|


(Subnet_bits = "23" & oct4 = "0" & oct3 = "0":"4":"8":"12":"16":"20":"24":"28":"32":"36":"40":"44":"48":"52":"56":"60":"64":"68":"72":"76":"80":"84":"88":"92":"96":"100":"104":"108":"112":"116":"120":"124":"128":"132":"136":"140":"144":"148":"152":"156":"160":"164":"168":"172":"176":"180":"184":"188":"192":"196":"200":"204":"208":"212":"216":"220":"224":"228":"232":"236":"240":"244":"248":"252") |
(Subnet_bits = "22" & oct4 = "0" & oct3 = "0":"8":"16":"24":"32":"40":"48":"56":"64":"72":"80":"88":"96":"104":"112":"120":"128":"136":"144":"152":"160":"168":"176":"184":"192":"200":"208":"216":"224":"232":"240":"248") |
(Subnet_bits = "21" & oct4 = "0" & oct3 = "0":"16":"32":"48":"64":"80":"96":"112":"128":"144":"160":"176":"192":"208":"224":"240") |
(Subnet_bits = "20" & oct4 = "0" & oct3 = "0":"16":"32":"48":"64":"80":"96":"112":"128":"144":"160":"176":"192":"208":"224":"240") |
(Subnet_bits = "19" & oct4 = "0" & oct3 = "0":"32":"64":"96":"128":"160":"192":"224")|
(Subnet_bits = "18" & oct4 = "0" & oct3 = "0":"64":"128":"192" )|
(Subnet_bits = "17" & oct4 = "0" & oct3 = "0":"128" ) |
(Subnet_bits = "16" & oct4 = "0" & oct3 = "0" ) |


(Subnet_bits = "15" & oct4 = "0" & oct3 = "0" & oct2 = "0":"4":"8":"12":"16":"20":"24":"28":"32":"36":"40":"44":"48":"52":"56":"60":"64":"68":"72":"76":"80":"84":"88":"92":"96":"100":"104":"108":"112":"116":"120":"124":"128":"132":"136":"140":"144":"148":"152":"156":"160":"164":"168":"172":"176":"180":"184":"188":"192":"196":"200":"204":"208":"212":"216":"220":"224":"228":"232":"236":"240":"244":"248":"252") |
(Subnet_bits = "14" & oct4 = "0" & oct3 = "0" & oct2 = "0":"8":"16":"24":"32":"40":"48":"56":"64":"72":"80":"88":"96":"104":"112":"120":"128":"136":"144":"152":"160":"168":"176":"184":"192":"200":"208":"216":"224":"232":"240":"248") |
(Subnet_bits = "13" & oct4 = "0" & oct3 = "0" & oct2 = "0":"16":"32":"48":"64":"80":"96":"112":"128":"144":"160":"176":"192":"208":"224":"240") |
(Subnet_bits = "12" & oct4 = "0" & oct3 = "0" & oct2 = "0":"16":"32":"48":"64":"80":"96":"112":"128":"144":"160":"176":"192":"208":"224":"240") |
(Subnet_bits = "11" & oct4 = "0" & oct3 = "0" & oct2 = "0":"32":"64":"96":"128":"160":"192":"224")|
(Subnet_bits = "10" & oct4 = "0" & oct3 = "0" & oct2 = "0":"64":"128":"192" )|
(Subnet_bits = "9" & oct4 = "0" & oct3 = "0" & oct2 = "0":"128" ) |
(Subnet_bits = "8" & oct4 = "0" & oct3 = "0" & oct2 = "0" ) |


(Subnet_bits = "7" & oct1 = "0":"4":"8":"12":"16":"20":"24":"28":"32":"36":"40":"44":"48":"52":"56":"60":"64":"68":"72":"76":"80":"84":"88":"92":"96":"100":"104":"108":"112":"116":"120":"124":"128":"132":"136":"140":"144":"148":"152":"156":"160":"164":"168":"172":"176":"180":"184":"188":"192":"196":"200":"204":"208":"212":"216":"220":"224":"228":"232":"236":"240":"244":"248":"252") |
(Subnet_bits = "6" & oct4 = "0" & oct3 = "0" & oct2 = "0" & oct1 = "0":"8":"16":"24":"32":"40":"48":"56":"64":"72":"80":"88":"96":"104":"112":"120":"128":"136":"144":"152":"160":"168":"176":"184":"192":"200":"208":"216":"224":"232":"240":"248") |
(Subnet_bits = "5" & oct4 = "0" & oct3 = "0" & oct2 = "0" & oct1 = "0":"16":"32":"48":"64":"80":"96":"112":"128":"144":"160":"176":"192":"208":"224":"240") |
(Subnet_bits = "4" & oct4 = "0" & oct3 = "0" & oct2 = "0" & oct1 = "0":"16":"32":"48":"64":"80":"96":"112":"128":"144":"160":"176":"192":"208":"224":"240") |
(Subnet_bits = "3" & oct4 = "0" & oct3 = "0" & oct2 = "0" & oct1 = "0":"32":"64":"96":"128":"160":"192":"224")|
(Subnet_bits = "2" & oct4 = "0" & oct3 = "0" & oct2 = "0" & oct1 = "0":"64":"128":"192" )|
(Subnet_bits = "1" & oct4 = "0" & oct3 = "0" & oct2 = "0" & oct1 = "0":"128" )
; @Success ; "Subnet Bits do not match ip address")
1 ACCEPTED SOLUTION

Sorted with:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var exp = g_form.getValue('subnet');
    var sBits = g_form.getValue('u_subnet_bits');
    var exp1 = exp.split('.');
    var oct = exp1[0];
    var oct1 = exp1[1];
    var oct2 = exp1[2];
    var oct3 = exp1[3];

    if (sBits == 32) {
        g_form.showFieldMsg('subnet', '32 Subnet Bits cannot be used', 'error');
    }
    if ((sBits == 30) && (oct3 % 4)) {
        g_form.showFieldMsg('subnet', 'Subnet Bits do not match ip address', 'error');
    }
    if ((sBits == 29) && (oct3 % 8)) {
        g_form.showFieldMsg('subnet', 'Subnet Bits do not match ip address', 'error');
    }
    if ((sBits == 28) && (oct3 % 16)) {
        g_form.showFieldMsg('subnet', 'Subnet Bits do not match ip address', 'error');
    }
	if ((sBits == 27) && (oct3 % 32)) {
        g_form.showFieldMsg('subnet', 'Subnet Bits do not match ip address', 'error');
    }
	if ((sBits == 26) && (oct3 % 64)) {
        g_form.showFieldMsg('subnet', 'Subnet Bits do not match ip address', 'error');
    }
	if ((sBits == 25) && (oct3 % 128)) {
        g_form.showFieldMsg('subnet', 'Subnet Bits do not match ip address', 'error');
    }
	if ((sBits == 24) && (oct3 != '0')) {
        g_form.showFieldMsg('subnet', 'Subnet Bits do not match ip address', 'error');
    }
}

The numbers were divisible so used that instead of all the numbers

View solution in original post

4 REPLIES 4

matt_a
Kilo Guru

As a trial, I tried the following:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
        if (isLoading || newValue === '') {
                  return;
        }
var exp = g_form.getValue('subnet');
var sBits = g_form.getValue('u_subnet_bits');
var exp1 = exp.split('.');
var oct = exp1.split[0];
var oct1 = exp1.split[1];
var oct2 = exp1.split[2];
var oct3 = exp1.split[3];

    if (g_form.getValue('sBits') == '30' && oct3 != '4') {
        g_form.showFieldMsg('subnet','Subnet Bits do not match ip address','error');
    }
}

But I get the following error:

matt_a
Kilo Guru

As an attempt, I have tried the following in an onChange script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
        if (isLoading || newValue === '') {
                  return;
        }
var exp = g_form.getValue('subnet');
var sBits = g_form.getValue('u_subnet_bits');
var exp1 = exp.split('.');
var oct = exp1.split[0];
var oct1 = exp1.split[1];
var oct2 = exp1.split[2];
var oct3 = exp1.split[3];

    if (sBits == '30' && oct3 != '4') {
        g_form.showFieldMsg('subnet','Subnet Bits do not match ip address','error');
    }
}

But this just gives the following error:

find_real_file.png

I worked out what the error was as I didnt need the .split for the variables.

I am now working with:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
        if (isLoading || newValue === '') {
                  return;
        }
var exp = g_form.getValue('subnet');
var sBits = g_form.getValue('u_subnet_bits');
var exp1 = exp.split('.');
var oct = exp1[0];
var oct1 = exp1[1];
var oct2 = exp1[2];
var oct3 = exp1[3];
	
    if (sBits == 32) {
        g_form.showFieldMsg('subnet','32 Subnet Bits cannot be used','error');
    }
		if (sBits == 30  && oct3 != '0') {
        g_form.showFieldMsg('subnet','Subnet Bits do not match ip address','error');
}
}

This works!

Would anyone know a quick way to get all the values from the top lotus formula or will I have to use a lot of || oct3 != queries?!

Sorted with:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var exp = g_form.getValue('subnet');
    var sBits = g_form.getValue('u_subnet_bits');
    var exp1 = exp.split('.');
    var oct = exp1[0];
    var oct1 = exp1[1];
    var oct2 = exp1[2];
    var oct3 = exp1[3];

    if (sBits == 32) {
        g_form.showFieldMsg('subnet', '32 Subnet Bits cannot be used', 'error');
    }
    if ((sBits == 30) && (oct3 % 4)) {
        g_form.showFieldMsg('subnet', 'Subnet Bits do not match ip address', 'error');
    }
    if ((sBits == 29) && (oct3 % 8)) {
        g_form.showFieldMsg('subnet', 'Subnet Bits do not match ip address', 'error');
    }
    if ((sBits == 28) && (oct3 % 16)) {
        g_form.showFieldMsg('subnet', 'Subnet Bits do not match ip address', 'error');
    }
	if ((sBits == 27) && (oct3 % 32)) {
        g_form.showFieldMsg('subnet', 'Subnet Bits do not match ip address', 'error');
    }
	if ((sBits == 26) && (oct3 % 64)) {
        g_form.showFieldMsg('subnet', 'Subnet Bits do not match ip address', 'error');
    }
	if ((sBits == 25) && (oct3 % 128)) {
        g_form.showFieldMsg('subnet', 'Subnet Bits do not match ip address', 'error');
    }
	if ((sBits == 24) && (oct3 != '0')) {
        g_form.showFieldMsg('subnet', 'Subnet Bits do not match ip address', 'error');
    }
}

The numbers were divisible so used that instead of all the numbers