Amit Gujarathi
Giga Sage
Options
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 11-22-2023 09:14 PM
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
- ShowFieldMessage()
- hideAllFieldMsgs()
- hideRelatedList(String listTableName)
- isMandatory(String fieldName)
- isNewRecord()
- isSectionVisible(String sectionName)
- removeOption(String fieldName, String choiceValue)
- save()
- setLabelOf(String fieldName, String label)
- setMandatory(String fieldName, Boolean mandatory)
- setReadOnly(String fieldName, Boolean readOnly)
- showRelatedLists()
- 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 :
- 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
- hideAllFieldMsgs()
- Hides all field messages.
- Method signature: hideAllFieldMsgs()
- Input parameters: void
- Return: void
- 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.
- 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.
- 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
- save()
- Saves the record without navigating away (update and stay).
- Method signature: save()
- Input parameters: void
- Return: void
- 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
- 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
- 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
Labels: