get Field Type in Client Script

rachanar
Tera Contributor

Dear All,

Can someone provide an insight of how we can get the field Type for the fields in a Client Script?

something similar to the below code we use in Server side scripting.

var glideElement = current.getElement(fieldName);

  var descriptor = glideElement.getED();

  var internalType = descriptor.getInternalType();

Any pointers would be very helpful.

Many Thanks,

Rachana

3 REPLIES 3

ccajohnson
Kilo Sage

Since you already have the code server-side, the best thing would be to have a GlideAjax call and feed it the appropriate parameters to get back what you need. If you are unfamiliar with GlideAjax, here is a wiki article:


GlideAjax - ServiceNow Wiki



Since you get the element from current, you probably need to get the current record object. There are two methods that you can use client side to get both the table name g_form.getTableName() and the sys_id of the record g_form.getUniqueValue. For information on these methods, see the following wiki article:


GlideForm (g form) - ServiceNow Wiki



How I am envisioning this happening is that on your client script you get the table name and the sys_id of the record. Then make an Ajax call and pass the table name, the field name and the sys_id. Within the script include you take the table and the sys_id and get the record in question.



      getFieldType: function() {


              var tName = this.getParameter('sysparm_tbl');


              var fName = this.getParameter('sysparm_fld');


              var recID = this.getParameter('sysparm_rid');


              var recObj = new GlideRecord(tName);


              recObj.get(recID);


              var glideElement = recObj.getElement(fName);


              var descriptor = glideElement.getED();


              var result = descriptor.getInternalType();


              return result;


      },



Let us know if this is helpful.


Anurag Tripathi
Mega Patron
Mega Patron

You can use something like this



alert(gel('label.incident.caller_id').outerHTML);



This will give you a handle on the object and from here you can extract the type.


-Anurag

Jay Gamblin
Mega Sage

g_form.getGlideUIElement('description').type