Reference Qualifiers not working for Table API

lss123
Tera Contributor

I'm using the REST Table API to update tickets, and I'm able to put invalid values into reference fields.   It appears the reference qualifiers for the reference fields aren't being evaluated.   I understand that reference qualifiers are evaluated if I use the Import Set API, but I don't want to force our data consumers to only use the Import Set API to insert/update records.   Plus, once we give the consumers REST access, they're free to use the Table API anyway and update with invalid values.

I'm considering writing my own evaluation script, but the prospect of mimicking what ServiceNow does with advanced and dynamic reference qualifiers looks daunting.   Are there ways to ensure reference qualifiers will be applied when using the Table API?

1 REPLY 1

lss123
Tera Contributor

For those interested, I went ahead and wrote my own evaluation script.   The only mystery I couldn't solve was how to deal with fields of type Reference who also have a dependent field - I just couldn't seem to understand how that was validated.   So I'll deal with that on a case-by-case basis.



But, if you'd like the code for manually validating what a reference qualifier does automatically, here it is (with a handful of debug statements):



var RefQualValidation = Class.create();


RefQualValidation.prototype = {


initialize: function() {


},



isRefValid: function(inTable, inField, inValue){


 


  /* check to see if the field is in-fact a reference field */


  var gr = new GlideRecord("sys_dictionary");


  gr.addQuery("name", inTable);


  gr.addQuery("element", inField);


  gr.addQuery("internal_type", "reference");


  gr.query();


  if (!gr.next()) {


   


    /* check for a parent table */


    var dbObj = new GlideRecord("sys_db_object");


    dbObj.addQuery("name", inTable);


    dbObj.query();


    dbObj.next();


   


    if(dbObj.super_class.nil()){


      /* shouldn't ever get here, but don't let that stop


        * the validation - just let it through */


      gs.log("Couldn't validate reference qualifier (no parent): " + inTable + ", " + inField + ", " + inValue);


      return true;


    }


    else{


      gr = new GlideRecord("sys_dictionary");


      gr.addQuery("name", dbObj.super_class.name);


      gr.addQuery("element", inField);


      gr.addQuery("internal_type", "reference");


      gr.query();


      if (!gr.next()) {


        /* shouldn't ever get here, but don't let that stop


          * the validation - just let it through */


        gs.log("Couldn't validate reference qualifier (no field in parent): " + dbObj.super_class.name + ", " + inField + ", " + inValue);


        return true;


      }


     


      /* if you're here, we've found the field in the parent table */


      gs.log("record found in parent: " + gr.column_label);


    }


  }


  gs.log("records found: " + gr.column_label);


 


  /* Now check to see the kind of reference qualifier and


    * take action. */


  switch(gr.use_reference_qualifier.toString()){


    case "advanced":


      gs.log("found advanced");


      return this.evalAdvanced(gr.reference, gr.reference_qual, inValue);


      break;


    case "dynamic":


      gs.log("found dynamic");


      return this.evalDynamic(gr.reference, gr.dynamic_ref_qual, inValue);


      break


    case "simple":


    default:


      gs.log("found simple");


      gs.log("reference_qual_condition: " + gr.reference_qual_condition);


      return this.evalSimple(gr.reference, gr.reference_qual_condition, inValue);


    break;


  }


  return true;


},



/* evaluates the simple reference qualifier */


evalSimple: function(refTable, refCondition, fieldValue){


  var gr = new GlideRecord(refTable);


  gr.addQuery("sys_id", fieldValue);


  gr.addEncodedQuery(refCondition);


  gr.query();


  if (!gr.next()) {


    gs.log("Invalid simple reference: " + refTable + "-" + refCondition + "-" + fieldValue);


    return false;


  }


 


  return true;


   


},



/* evaluates the advanced reference qualifier */


evalAdvanced: function(refTable, refQual, fieldValue){


  /* if advanced qualifier is javascript, get value here */


  var rqQuery;


  if(refQual.startsWith("javascript:")){


    var fct = refQual.substring(11);


    rqQuery = eval(fct);


  }


  else{


    rqQuery = refQual;


  }


 


  var gr = new GlideRecord(refTable);


  gr.addQuery("sys_id", fieldValue);


  gr.addEncodedQuery(rqQuery);


  gr.query();


  if (!gr.next()) {


    gs.log("Invalid advanced reference: " + refTable + "-" + refQual + "-" + fieldValue);


    return false;


  }


   


  gs.log("advanced reference qualifier validated!");


  return true;


},



/* evaluates the dynamic reference qualifier */


evalDynamic: function(refTable, refQual, fieldValue){


  /* get dynamic query */


  var dq = new GlideRecord("sys_filter_option_dynamic");


  dq.addQuery("sys_id", refQual);


  dq.query();


  if (!dq.next()) {


        gs.log("Invalid dynamic reference (not found): " + refTable + "-" + refQual + "-" + fieldValue);


  }


   


  var rqQuery = eval(dq.script);


  gs.log("rqQuery: " + rqQuery);


  var gr = new GlideRecord(refTable);


  gr.addQuery("sys_id", fieldValue);


  gr.addEncodedQuery(rqQuery);


  gr.query();


  if (!gr.next()) {


    gs.log("Invalid dynamic reference: " + refTable + "-" + refQual + "-" + fieldValue);


    return false;


  }


   


  gs.log("dynamic reference qualifier validated!");


  return true;


},



type: 'RefQualValidation'


};