logo

NJP

ServiceNow Introduction to GlideForm API = ServiceNow JavaScript Challenge Day 6-9

Import · Nov 23, 2023 · article

Hi There,

I hope you're doing great.

With the participation in the ServiceNow Javascripting Challenge led by @Gagan Jolly, I m learning a lot. I thought I should share my learning on the Now community also

Day 6-9: ServiceNow Java scripting Challange

Statement of Work :

Write a code using the following methods

  1. ShowFieldMessage()
  2. hideAllFieldMsgs()
  3. hideRelatedList(String listTableName)
  4. isMandatory(String fieldName)
  5. isNewRecord()
  6. isSectionVisible(String sectionName)
  7. removeOption(String fieldName, String choiceValue)
  8. save()
  9. setLabelOf(String fieldName, String label)
  10. setMandatory(String fieldName, Boolean mandatory)
  11. setReadOnly(String fieldName, Boolean readOnly)
  12. showRelatedLists()
  13. submit()

Solution :

Let's first try to understand what ServiceNow GlideForm API means

GlideForm :

  • The GlideForm API provides methods to customize forms.
  • Works on the client side only

Lets look into diffrent GlideForm Methods :

  1. showFieldMsg()
    • Displays either an informational or error message under the specified form field
    • Method signature : showFieldMsg(String field, String message, String type)
    • Input parameters :
      • field =⇒ String =⇒ Name of the field or control.
      • message =⇒ String =⇒ Message to display.
      • type =⇒ String ==>" error", "info", or "warning".
    • Return: void
  2. hideAllFieldMsgs()
    • Hides all field messages.
    • Method signature: hideAllFieldMsgs()
    • Input parameters: void
    • Return: void
  3. isNewRecord()
    • Returns true if the record has never been saved.
    • Method signature: isNewRecord()
    • Input parameters: void
    • Return: Boolean =⇒ Returns true if the record has not been saved; otherwise false.
  4. isSectionVisible()
    • Returns true if the section is visible.
    • Method signature : isSectionVisible(String sectionName)
    • Input parameters : String =⇒ Section name
    • Return: Boolean =⇒ Returns true when the section is visible; otherwise, false is returned.
  5. removeOption()
    • Removes the specified option from the choice list.
    • Method signature : removeOption(String fieldName, String choiceValue)
    • Input parameters :
      • fieldName =⇒ String =⇒ Name of the field.
      • choiceValue =⇒ String =⇒ The value stored in the database. This is not the label.
    • Return: void
  6. save()
    • Saves the record without navigating away (update and stay).
    • Method signature: save()
    • Input parameters: void
    • Return: void
  7. setLabelOf()
    • Sets the plain text value of the field label.
    • Method signature : setLabelOf(String fieldName, String label)
    • Input parameters :
      • fieldName =⇒ String =⇒ The field name.
      • label =⇒ String =⇒ The field text label.
    • Return: void
  8. setMandatory()
    • Makes the specified field mandatory.
    • Method signature : setMandatory(String fieldName, Boolean mandatory)
    • Input parameters :
      • fieldName =⇒ String =⇒ The field name.
      • mandatory =⇒ Boolean ==>When true makes the field mandatory.
    • Return: void
  9. setReadOnly()
    • Makes the specified field read-only or editable.
    • Method signature : setReadOnly(String fieldName, Boolean readOnly)
    • Returns
      • Boolean =⇒ True if the field is required, false otherwise.
    • Input parameters :
      • fieldName =⇒ String =⇒ The field name.
      • readOnly =⇒ Boolean ==>When true makes the field mandatory.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    // Show Field Message
    g_form.showFieldMsg('description','Please provide valid description','info');
    g_form.showFieldMsg('caller_id','Requestor only,'error');
    // Hidde All field messages
    g_form  .hideAllFieldMsgs('description');
    // Hide related list
    g_form.hideRelatedList('task_cmdb_ci_service');
    // Show related list
    g_form.showRelatedList('task_cmdb_ci_service');
    // Check if the field is mandatory
    alert ('Is caller id mandatoryy : '+g_form.isMandatory('caller_id'));
    /// Check  if the record is a New record
    alert ('is new record : '+g_form.isNewRecord());
    // Is the section visible
    alert('is notes section visisble : '+g_form.isSectionVisible('notes'));
    // Set mandatory
    g_form.setMandatory('description',true);
    //set read-only
    g_form.setReadOnly('description',true);
    // Set label of
    g_form.setLabelOf('description','summary');
    // Set the value of the field
    g_form.setValue('description','category got changes so we saved ');
    // Save the form record
    g_form.save();
    // Submit the form record
    g_form.submit();
}

Please be sure to bookmark this article and mark it as Helpful if you thought it helpful.

Regards,

Amit Gujarathi

Technomonk Youtube

Amit Gujarathi Linkedin

TheTechnomonk.com

ServiceNow Community Amit Gujarathi

View original source

https://www.servicenow.com/community/developer-articles/servicenow-introduction-to-glideform-api-servicenow-javascript/ta-p/2388700