The CreatorCon Call for Content is officially open! Get started here.

Give me couple of examples for each type of client script

Subhashini C
Tera Contributor
 
6 REPLIES 6

Viraj Hudlikar
Tera Sage
Tera Sage

Hello @Subhashini C 

Do check this videos:
https://youtu.be/AqUY98OToGY?si=07P-yPh3Azogi5b0

https://youtu.be/7QF0C6D9Cr0?si=Fy3cDRKh3y-OUuMj

https://youtu.be/lla0rWXuInA?si=DBLArt1JKaKbwjpA

https://youtu.be/iAmrt-75sBA?si=_-XuNx4oQ-4oimPM

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.

Hello Guys, Welcome to SAASWITHSERVICENOW. This is short series of learning Client Scripts in ServiceNow in details. In this series you will learn about different types of client scripts, their major scenarios and practical examples. What is Client Script in ServiceNow A Client Script is a ...
Hello Guys, Welcome to SAASWITHSERVICENOW. This is short series of learning Client Scripts in ServiceNow in details. In this series you will learn about different types of client scripts, their major scenarios and practical examples. What is Client Script in ServiceNow A Client Script is a ...
Hello Guys, Welcome to SAASWITHSERVICENOW. This is short series of learning Client Scripts in ServiceNow in details. In this series you will learn about different types of client scripts, their major scenarios and practical examples. What is Client Script in ServiceNow A Client Script is a ...
Hello Guys, Welcome to SAASWITHSERVICENOW. This is short series of learning Client Scripts in ServiceNow in details. In this series you will learn about different types of client scripts, their major scenarios and practical examples. What is Client Script in ServiceNow A Client Script is a ...

Sandeep Rajput
Tera Patron
Tera Patron

@Subhashini C 

 

There are usually 4 types of client scripts in ServiceNow.

1.onLoad Client Scripts: These scripts run when a form loads.

function onLoad() {
  // Get the GlideForm object
  var gf = g_form;

  // Make the 'comments' field read-only
  gf.setReadOnly('comments', true);

  // Make the 'short_description' field read-only ONLY on new records
  if (g_form.isNewRecord()) {
    gf.setReadOnly('short_description', true);
  }
}

 

2. onChange Client Scripts: These scripts run when a specific field's value changes.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
  if (isLoading || newValue === '') {
    return;  // Don't run on initial load or if the field is cleared
  }

  if (newValue == 'problem') {
    g_form.setVisible('problem_details', true); // Show the 'problem_details' field
  } else {
    g_form.setVisible('problem_details', false); // Hide the 'problem_details' field
  }
}

3. onSubmit Client Scripts: These scripts run when a form is submitted. They are crucial for data validation.

function onSubmit() {
  if (g_form.getValue('short_description') == '') {
    alert('Short description is required!');
    return false; // Prevent submission
  }
}

4. onCellEdit Client Scripts: These scripts run when a cell in a list is edited (inline editing).

function onCellEdit(sys_id, table, old_value, new_value, is_loading) {
  if (is_loading) {
    return;
  }

  if (isNaN(new_value)) {
    alert('Please enter a number.');
    g_form.setCellDisplayValue(table, sys_id, 'quantity', old_value); // Revert to the old value
    return false; // Prevent the change
  }
}

 

Hope this helps.

Ashish Parab
Mega Sage
Mega Sage

Hello @Subhashini C ,

 

The below thread may help you to understand more about client scripts.

https://www.servicenow.com/community/developer-articles/client-script-in-servicenow-with-some-use-ca...

 

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.

 

Thanks,

Ashish

servicenow #skfacts #servicenowdeveloper #ServiceNow #ITSM #ITIL #ServiceManagement #ServiceDesk #IncidentManagement #ChangeManagement #CMDB #Automation #DigitalTransformation #ServiceNowAdmin #ServiceNowDevelopment #ServiceNowCommunity #ServiceNowTraining #ServiceNowIntegration students can log ...

Ankur Bawiskar
Tera Patron
Tera Patron

@Subhashini C 

I will suggest you to go through docs, youtube videos.

I will also suggest to take platform fundamentals course from now learning so that you get hands-on experience.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader