Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Pulling data from license details table

annmol1
Kilo Expert

Hi all

Please can you help. I am trying to automate license allocation to users based on user request and available licenses in the system. However GlideRecord and query on license_details table doesn't return me desired results. Please find below the script am using in the workflow. Please can you see why?

// This script needs to set answer to 'yes' or 'no' to indicate the state of the activity.

//

// For example,

//

    answer = ifScript();

//

    function ifScript() {

     

      var usr = new GlideRecord('sys_user_has_role');

      usr.addQuery('user', current.variables.requestedfor);

      usr.query();

      if (usr.next()){

        gs.log("I am already charged");

      return 'yes';

   

      }

      else {

//below is the section with issue

     

  var lic = new GlideRecord('license_details');

     

      lic.addQuery('sys_id', "3701a52437132200ad36616043990e54");

      lic.query();

      var alloc = parseInt(lic.allocated);

      var used = parseInt(lic.count);

      gs.log("Name"+ lic.name +"   "+ lic.allocated+ "Allocated "+ alloc+ " "+ lic.count+ "Used "+ used);

// this gs.log prints "Name   0Allocated 0 Used NaN" in the log. So name not picked, allocated and used not picked as in the system. 1010 and 0 should be the numbers

      while(lic.next()){

     

    if (alloc < used) {

      gs.log("I am newly charged");

              return 'yes';

        }

      gs.log("Sorry no license");

        return 'no';

     

      }

      }    

     

     

     

       

  }

2 REPLIES 2

Mwatkins
ServiceNow Employee
ServiceNow Employee

Until you call lic.next(), the lic object has not been populated with an actual record. Thus, your lic.count and lic.allocated check is checking against a null GlideRecord value and naturally will always evaluate to 0 when cast as an Integer. Put those checks inside the lic.next() while loop.


SanjivMeher
Mega Patron
Mega Patron

Just a small mistake



lic.addQuery('sys_id', "3701a52437132200ad36616043990e54");


      lic.query();


      // this gs.log prints "Name   0Allocated 0 Used NaN" in the log. So name not picked, allocated and used not picked as in the system. 1010 and 0 should be the numbers




      while(lic.next()){


    //should be inside the while loop


var alloc = parseInt(lic.allocated);


      var used = parseInt(lic.count);


      gs.log("Name"+ lic.name +"   "+ lic.allocated+ "Allocated "+ alloc+ " "+ lic.count+ "Used "+ used);



    if (alloc < used) {


      gs.log("I am newly charged");


              return 'yes';


        }


      gs.log("Sorry no license");


        return 'no';


   


      }



Please mark this response as correct or helpful if it assisted you with your question.