Script does not return value

olufsen
Kilo Sage

Hi, I think I need another eye to check my script. I've tried this in the background script, but it does not return a value.

What could be the issue on the subjectRoom attribute that's failing the query condition?

 

 

subj = "[Alert] London 101 is down";
var zRoom = subj.toString().split('[Alert] '); //split the subject
var subjectRoom = zRoom[1];
gs.info(subjectRoom.toString());

var grRoom = new GlideRecord('cmdb_ci_iot');
grRoom.addQuery('name','STARTSWITH',subjectRoom);
grRoom.query();
while(grRoom.next())
{
gs.log(grRoom.support_group.getDisplayValue());
}

 

1 ACCEPTED SOLUTION

olufsen
Kilo Sage

This results after adding a few more lines to return only the first word after Alert.

 

subj = "[Alert] London 101 is down";
var array = subj.split(" ");
var alrt = array[0];
var subjectRoom = array[1];

for(var i=1;i<alrt.length;i++){
array[i];
}
gs.print(array[1])

var grRoom = new GlideRecord('cmdb_ci_iot');
grRoom.addQuery('name','STARTSWITH',subjectRoom[0]);
grRoom.query();
if(grRoom.next())
{
gs.print(grRoom.support_group.getDisplayValue());
}

 

View solution in original post

8 REPLIES 8

Community Alums
Not applicable

If you are sure the city name is right after {alert} - then you can use extra split :

var subjectRoomPre = zRoom[1].split(" ");
var subjectRoom = subjectRoomPre[0];

Pavankumar_1
Mega Patron

Hi @olufsen ,

I don't see any issue if its name is London 101 is down then it will return the support group name. try with valid record or  share what you are looking for?

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Community Alums
Not applicable

Firstly, not sure why you use global variable for subj - better put  - var subj

Second, for BG script better use print or error instead of log.

Also, try adding something like - gs.error("Record num : " + grRoom.sys_id + "" ); in the while loop - what is the outcome ?

olufsen
Kilo Sage

This results after adding a few more lines to return only the first word after Alert.

 

subj = "[Alert] London 101 is down";
var array = subj.split(" ");
var alrt = array[0];
var subjectRoom = array[1];

for(var i=1;i<alrt.length;i++){
array[i];
}
gs.print(array[1])

var grRoom = new GlideRecord('cmdb_ci_iot');
grRoom.addQuery('name','STARTSWITH',subjectRoom[0]);
grRoom.query();
if(grRoom.next())
{
gs.print(grRoom.support_group.getDisplayValue());
}