How to query the table fields exists or not from the array in background script

rahul16
Giga Guru

var gr = new GlideRecord("interaction");
var myStringArray = ["sys_created_by", "assignment_group"];
var arrayLength = myStringArray.length;
gr.query();
while (gr.next()) {
    for (var i = 0; i < arrayLength; i++) {
        var data = myStringArray[i];
        gs.print(data + ' --> ' + gr.data);
    }
}

I want to know if field values exist in a table or not.

As I have many filed values I was taught to use arrays and looping through all values.

If a field does not exist then  I get not defined but it is not working can anyone help me find if field exist in the table using the background script.

I have a lot of fields to find and some have multiple references like (assignment_group.name....) like that  

3 REPLIES 3

Marcin20
Mega Guru

Hi Rahul,

 

Please use gr.isValidField()

i.e.

 

var gr = new GlideRecord("interaction");
var myStringArray = ["sys_created_by", "assignment_group"];
var arrayLength = myStringArray.length;
gr.query();
while (gr.next()) {
    for (var i = 0; i < arrayLength; i++) {
        var data = myStringArray[i];
        gs.print(data + ' --> ' + gr.data);

       if (gs.isValidField(data)) {

              gs.print (' the field exists ')

             }
    }
}

https://developer.servicenow.com/dev.do#!/reference/api/sandiego/server_legacy/c_GlideRecordAPI#r_Gl...

 

Best Regards,

Marcin

 

 

If my answer helped you in any way, please mark this answer as helpful and correct.

It does not work for dot notations like opened_for.user_name it returns false even if the field exists.

There is a typo in the code. It should read

gr.isValidField(data)

If this answer was helpful, I would appreciate if you marked it as such - thanks!

Best
Daniel