Glideform Methods in ServiceNow | g_form object | Practical Demonstration

In this article you will get the understanding of API, glideform methods in servicenow along with video attached below which represent practical implementation of using glideform js and use of g_form object.

glideform js, glideform methods in servicenow, glide form servicenow

What is API ?  

API is basically a tool set (such as class with methods, java script file etc) which programmer can use to create software, to create some functionality (for example displaying google map, triggering google login, payment gateways, window scroll bars, and many more etc). So, Programmer can use API when he needs to perform some task etc.

Actually, it is not possible to build each and everything again and again, so API solved this problem and programmers can use already created API to perform the required task.
In ServiceNow we have 6 API categories, whereas in this article we will learn Client Category API i.e. we will focus on ServiceNow GlideForm API and glideform methods in ServiceNow.

What is Glide Form API and what are glideform methods in ServiceNow?

GlideForm API is a javascript class which provides methods to customize forms. We can only use glide form methods on client side (Client Scripts) to make custom changes. There is no need to initialize glide form api as there is no constructor and we can call or access methods using global object g_form as mentioned in below example:

g_form.addErrorMessage("Please fill date in required format");

There are lots of glideform methods in servicenow. Methods which are widely used while doing coding client script are listed below:

1.addDecoration(string fieldname, string icon, string title)

for example - let's say we have to add star icon in front of the requestor label so that it can be treated as VIP user, then we can use this function as mentioned below:

g_form.addDecoration('requester', 'icon-star', 'VIP User'); 

and If  you want to remove decoration then you can use:

removeDecoration(string fieldname, string icon, string title)

2. addErrorMessage(string ErrorMessageText)

for example - let's say we want to display error message when user fill the date in wrong format and click on submit button, then we can use this function as mentioned below:

g_form.addErrorMessage("Please fill date in required format");

3. addInfoMessage(string InformationMessageText)

for example: Let’s say we want to display the total count of incident record having same description when the save the record then we can use this method as mentioned below:

g_form.addInfoMessage("Total number of incident with same description are :" +variable);

4. addOption(string fieldname, stringchoicevalue,stringchoicelabel)

this method is used to add option in choice list field. Let's say we want to add "Customer Action" label in state field, then we can use this method as mentioned below:

g_form.addOption('state','12','Customer Action');

if we want to add it in specific index number then we can use this method as mentioned below:

g_form.addOption('state','12','Customer Action','4');  // so here 4 is a choice index number

if we want to remove the option from the choice list, then we can use:

g_form.removeOption(string fieldname, string choicevalue) 

for example:

5. clearValue(string fieldname);

Let's say if we want to clear the value displayed in text box. then we can use this method.


6. g_form.clearOptions(string fieldname);

This method is used to remove all the option available in the choice list.

7. g_form.disableAttachment();

We can attach the document in any record through the attachment icon displayed on the right side top. So, if we want that use cannot attach the document then we can use this method.
same as to enable we can use:


8. g_form.clearMessages();

This method is used to remove all the message displayed on the top of the form.

9. g_form.getControl(string fieldname);

This method returns HTML element [object HTMLInputElement]. 

10. g_form.getLabelOf(string fieldname);

This method returns the label name of the field.

11. getBooleanValue(string fieldname)

this  method is used to return the boolean value such as true or false. Let's says we want the value of checkbox, radio button etc that whether they are checked or not, then we can use this method.


12. g_form.getOption(string fieldname,string choicevalue);

This method is used to get the choice label text of choice value given in method. for example:
g_form.getOption('state','2').text // will return "In Progress" text value in incident form.  

13. g_form.getReference(string fieldname,callbackfunctionname)

In this method if we do not use callback function then this method will be executed synchronously and if the use call back method then it will be executed asynchronously.
for example (synchronous call):

g_form.getReference('caller_id').vip;  // basically it refer the sys_user table and return true or false as VIP field is a checkbox.

g_form.getReference('caller_id').email   // return email if of user, basically it refer the sys_user table and return the value of user email.

for example (asynchronous  call):


function isUserVIP(caller)

14. g_form.getTableName()

This method return the table name of the record for this above function is getting executed. Lets say we have written it for incident record then it will return incident table name.

15. g_form.getUniqueValue()

This method return the sysID of the record displayed in the form.

16. g_form.getValue(string fieldname)

Let's say if we want to get the value display in short description text box. then we can get it through this method.


and if you want to set value in the field then:

g_form.setValue(string fieldname,string ValueYouWantToSet)


This method is used when we want to save the form and do not want to navigate to another form or want to stay to same form.

18. g_form.setDisabled(string fieldname, booleanvalue)

This method is used to disable the field and make it unavailable when boolean value in true and enable and avaialalbe when boolean value in false.

But, if we want that field only become read only then we can use:
g_form.setReadOnly(string fieldname, booleanvalue)

19.g_form.setMandatory(string fieldname, booleanvalue)

We can use this method to make the field mandatory, if the boolean value is true then field become mandatory and when false then field become optional.
for example:

g_form.setMandatory(('state', true); //state field become mandatory

20. g_form.setVisible(string fieldname,BooleanValueTrue/false);

We can use this method to make the field hidden or visible in the form.But when we make the field hidden then it left blank space there.

for example:
g_form.getVisible('category',false); // this will make the category field hidden in incident form.

21. g_form.showFieldMsg(string fieldname,string MessageWantToDisplayBelowField,string Info/Warning/Error)

We can use this method to display message just below the field. 
For example:
g_form.showFieldMsg('state','This message text will be displayed below state field','Info');

22. g_form.submit()

this method is used to save the record, but it will take user away from the form. but if we use below:

g_form.submit(string verb) // here verb in action name mentioned in UI action.

then this method with action name parameter will perform the UI action.

Video Tutorial of Using glideform methods in servicenow:

Below video will provide you basic understanding that how you can code and access glide form api methods through g_form object. Please do subscribe the youtube channel if video seems to be helpful.

Above I have only mentioned those glideform methods which are mostly used while doing coding. For more glideform methods in servicenow you can refer ServiceNow docs. If you are looking for servicenow tutorials video you can refer my youtube channel Basico ServiceNow learning.  Please let me know if you need more information on glideform js. Please feel free to ask questions and provide your feedback below in comments box.

No comments:

Thankyou !!!!

Powered by Blogger.