Javascript split() function not working

matt_kean
Tera Guru

I am writing a 'Script Action' to respond to event 'session.established'. In this script action, I am trying to use the basic javascript function split(), but the function is not producing an array as expected. Below is my script:

setOnNetwork();

function setOnNetwork() {

        //get the current session IP address

        var ipAddress = gs.getSession().getClientIP().toString();

        gs.log('Script Action - Set OnNetwork Session Value: ' + ipAddress);

        //split the ip address by period

        var ipSplit = ipAddress.split(".");

        gs.log('Script Action - Set OnNetwork Session Value: ' + ipSplit[0]);

        gs.log('Script Action - Set OnNetwork Session Value: ' + ipSplit[1]);

        gs.log('Script Action - Set OnNetwork Session Value: ' + ipSplit[2]);

        gs.log('Script Action - Set OnNetwork Session Value: ' + ipSplit[3]);

}

Here are the resulting log messages:

InformationScript Action - Set OnNetwork Session Value: 192.189.129.17
InformationScript Action - Set OnNetwork Session Value: undefined
InformationScript Action - Set OnNetwork Session Value: undefined
InformationScript Action - Set OnNetwork Session Value: undefined
InformationScript Action - Set OnNetwork Session Value: undefined

1 ACCEPTED SOLUTION

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Looks like it's not actually getting set to a string. Try this instead on line 4:



var ipAddress = gs.getSession().getClientIP() + '';



I tested in an instance and that seemed to work.



View solution in original post

3 REPLIES 3

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Looks like it's not actually getting set to a string. Try this instead on line 4:



var ipAddress = gs.getSession().getClientIP() + '';



I tested in an instance and that seemed to work.



Thank you! Very strange.   So, that means the toString() function is actually what isn't working.   While your suggestion works, I wonder what impact that will have on the rest of the scripts I have out there.


I was struggling with this issue from an hour... Thanks Brad, it helped a lot