Hard coded sys_id

ravi2k
Kilo Contributor

Hi Frds, I don't want to use Hard coded sys_id in my below script, Instead of this I want to Create a system property to store the name of the record (not the sys_id) for easier manageability. The script can use gs.getProperty() to retrieve the record and use the sys_id. If the named record was not found, an error can be displayed appropriately. Pls help how to modify the script. var DB_Get_ServiceDeskArea = Class.create(); DB_Get_ServiceDeskArea.prototype = Object.extendsObject(AbstractAjaxProcessor, { getServiceDeskArea: function () { var id = this.getParameter('sysparm_assignment_group'); var encodedquery = "sys_id="+id.toString()+"^typeLIKE887a30d60f623100ec12c9cce1050e6f"; var gr_group = new GlideRecord('sys_user_group'); gr_group.addEncodedQuery(encodedquery); // type of group is Helpdesk gr_group.query(); if (gr_group.next()) { return gr_group.u_servicedesk_subarea.toString(); } return 'null'; }, type: 'DB_Get_ServiceDeskArea' });

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

Hi Ravish,



I've build a full wiki page with vides around this exact subject.



System Properties Best Practices - ServiceNow Wiki



In your situation, create a property something like this:



my.default.user_type, with a value "YOUR GROUP TYPE HERE".



Then your script might look something like this (I made a couple changes to fix some issues you were about to run in to, like returning nothing instead 'null'.)



var DB_Get_ServiceDeskArea = Class.create();


DB_Get_ServiceDeskArea.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  getServiceDeskArea: function () {


  var defaultType = gs.getProperty('my.default.user_type');


  if (defaultType = '')


      return;



  var id = this.getParameter('sysparm_assignment_group');


  var encodedquery = "sys_id="+id.toString()+"^type.name=" + defaultType;


  var gr_group = new GlideRecord('sys_user_group'); gr_group.addEncodedQuery(encodedquery);


  // type of group is Helpdesk gr_group.query();


  if (gr_group.next()) {


      return gr_group.u_servicedesk_subarea.toString();


  }


  return;


  },



  type: 'DB_Get_ServiceDeskArea'


});


View solution in original post

10 REPLIES 10

larstange
Mega Sage

Hi,



You could build a generic function, where you can store a pointer to   a particular record in a system property by storing the table and search criteria.



Eg. create a system property and put some JSON in it


{"form":"sys_user_group","query":"name=Helpdesk"}



Then build your AJAX script include to be generic - you just need to pass the name of the system property, and it will return the record that it is pointing to:



var propName = this.getParameter('sysparm_propertyName');


var JSON = gs.getProperty(propName);


var parser = new JSONParser();


var parsed = parser.parse(JSON);




var gr = new GlideRecord(parsed.form);


gr.addEncodedQuery(parsed.query);




if (gr.next())


  return gr.sys_id;


else


  return "not_found";


Kalaiarasan Pus
Giga Sage

First, to make it easier for others to help/understand/respond to question, do paste your script in a user friendly format.


Chuck Tomasi
Tera Patron

Hi Ravish,



I've build a full wiki page with vides around this exact subject.



System Properties Best Practices - ServiceNow Wiki



In your situation, create a property something like this:



my.default.user_type, with a value "YOUR GROUP TYPE HERE".



Then your script might look something like this (I made a couple changes to fix some issues you were about to run in to, like returning nothing instead 'null'.)



var DB_Get_ServiceDeskArea = Class.create();


DB_Get_ServiceDeskArea.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  getServiceDeskArea: function () {


  var defaultType = gs.getProperty('my.default.user_type');


  if (defaultType = '')


      return;



  var id = this.getParameter('sysparm_assignment_group');


  var encodedquery = "sys_id="+id.toString()+"^type.name=" + defaultType;


  var gr_group = new GlideRecord('sys_user_group'); gr_group.addEncodedQuery(encodedquery);


  // type of group is Helpdesk gr_group.query();


  if (gr_group.next()) {


      return gr_group.u_servicedesk_subarea.toString();


  }


  return;


  },



  type: 'DB_Get_ServiceDeskArea'


});


Thank you very much Chuck for a great help:-)