How to find whether an array element has a value in it or not??

Anu9
Kilo Explorer

Hi All,

I have a REST call that creates a record on Table A in servicenow and add a some notes to the Work notes field on the record. Notes from the REST Message contains user responses to the some questions. Based of those responses we need to create some requests and close off the record. Here is the sample of the Notes from REST call 

 "notes" : "Customer aware that they can opt out any time: Yes \n Additional feedback on the enrollment process:  \n Welcome packet via email: No"

Attached copy of worknotes example

These notes will be added to Worknotes. To handle this requirement I created a after business rule that will parse the worknotes for the answers from the user and takes appropriate action.

var temp;
var notes = current.work_notes.getJournalEntry(-1);
var na = notes.split("\n\n"); // Splits the worknotes
gs.info(na);

for (var i = 0; i < na.length; i++) {  // Looks for exact text in each array element created from breaking down the worknotes

if(na[i].contains("Welcome packet via email")){ // when found the right notes it will be further broke down to see the user answers
var createReq =0 ;
var temp = na[i].split("\n");
for(var j =0; j < temp.length ; j++){
gs.info(temp[j] + " Position" + j );
var temp1 = temp[j].split(":");
if((temp1[0].contains("Welcome packet via email")) && (temp1[1].contains("No")) ){ // This works as expected 
createReq = 1;
}
if(temp1[0].contains("Additional feedback on the enrollment process") && (temp1[1]== "null") ){ // But for this condition i don't know how to check if the user has given a response or not. If he hasn't given any response what it will contain. Should I look for null value or empty space or undefined . I tried those value but it didn't work
createReq = 2;
}

if(createReq == 2) { current.state = "closed"; } 

gs.info(createReq);
}

Can someone help me to figure how to evalute this strings. Can someone share me regex to check whether there is a value or new line "\n" after the string Additional feedback on the enrollment process in the worknotes.

3 REPLIES 3

Prateek kumar
Mega Sage

Use ArrayUtil.contains

https://developer.servicenow.com/app.do#!/api_doc?v=london&id=r_AU-contains_A_O


Please mark my response as correct and helpful if it helped solved your question.
-Thanks

i am already using contains in my code. My point is to figure out if one of questions has no answer how to figure out , i couldn't use contains null or undefined.

 

 

Brad Tilton
ServiceNow Employee
ServiceNow Employee

I would use JSUtil.nil() to do that check: https://developer.servicenow.com/app.do#!/api_doc?v=madrid&id=r_JSUtil-nil_O

Your condition would look something like this instead:

if(temp1[0].contains("Additional feedback on the enrollment process") && (JSUtil.nil(temp1[1]) ){