Where and how to use g_sc_form?

AnubhavS
Tera Expert
 
2 ACCEPTED SOLUTIONS

AnubhavS
Tera Expert

 

In ServiceNow, g_form and g_sc_form are JavaScript objects used for manipulating forms, but they are used in different contexts and have different capabilities.

g_form

  • Context: g_form is used in the context of regular forms, typically for records in any ServiceNow table. It is commonly used in client scripts and UI policies.
  • Usage: g_form provides methods to get and set values in fields, show and hide fields, add messages, and perform other actions on the form.
  • Capabilities: It can interact with any form in ServiceNow, including incident forms, change request forms, and custom table forms.

Example Methods

  • Getting a Value: g_form.getValue('field_name');
  • Setting a Value: g_form.setValue('field_name', 'value');
  • Hiding a Field: g_form.setDisplay('field_name', false);
  • Adding a Message: g_form.addInfoMessage('This is an info message');

Example Usage

 

 

 

 

javascript
Copy code
// Client script on a Change Request form function onLoad() { var changeType = g_form.getValue('change_type'); if (changeType === 'Standard') { g_form.setDisplay('risk', false); } }

 

 

 

g_sc_form

  • Context: g_sc_form is specifically used in the context of Service Catalog item forms. It is designed to handle catalog item variables rather than table fields.
  • Usage: g_sc_form provides methods similar to g_form, but they are specifically tailored for Service Catalog items. It is used in catalog client scripts and UI policies.
  • Capabilities: It interacts with catalog item forms, including order guides, record producers, and catalog items.

Example Methods

  • Getting a Value: g_sc_form.getValue('variable_name');
  • Setting a Value: g_sc_form.setValue('variable_name', 'value');
  • Hiding a Field: g_sc_form.setDisplay('variable_name', false);
  • Adding a Message: g_sc_form.addInfoMessage('This is an info message');

Example Usage

 

 

 

 

 

javascript
Copy code
// Catalog client script on a Service Catalog item function onLoad() { var requestedFor = g_sc_form.getValue('requested_for'); if (requestedFor === 'admin') { g_sc_form.setDisplay('vip', true); } }

 

 

 

Key Differences

  • Scope:

    • g_form is used for regular forms across all ServiceNow applications.
    • g_sc_form is used exclusively for Service Catalog item forms.
  • Object of Manipulation:

    • g_form manipulates fields within a record in any table.
    • g_sc_form manipulates variables within a catalog item.
  • Context of Use:

    • g_form is typically used in Client Scripts and UI Policies on table forms.
    • g_sc_form is typically used in Catalog Client Scripts and UI Policies on catalog items.

Summary

Use g_form when working with standard record forms in ServiceNow tables, such as incident forms or change request forms. Use g_sc_form when working with Service Catalog items to manipulate catalog item variables. Each object provides methods tailored to its specific context, allowing for appropriate form manipulation based on the type of form being handled.

View solution in original post

Amitoj Wadhera
Kilo Sage

Hi @AnubhavS ,

 

All GlideForm methods can be accessed through the global g_form object, which is available in client scripts.

The g_form object does not support variables on task records. Therefore, if you encounter situations where g_form APIs do not work as expected, you can use g_sc_form instead.

 

If you find my response helpful, please consider marking it as the 'Accepted Solution' and giving it a 'Helpful' rating. Your feedback not only supports the community but also encourages me to continue providing valuable assistance.

 

Thanks,

Amitoj Wadhera

View solution in original post

2 REPLIES 2

AnubhavS
Tera Expert

 

In ServiceNow, g_form and g_sc_form are JavaScript objects used for manipulating forms, but they are used in different contexts and have different capabilities.

g_form

  • Context: g_form is used in the context of regular forms, typically for records in any ServiceNow table. It is commonly used in client scripts and UI policies.
  • Usage: g_form provides methods to get and set values in fields, show and hide fields, add messages, and perform other actions on the form.
  • Capabilities: It can interact with any form in ServiceNow, including incident forms, change request forms, and custom table forms.

Example Methods

  • Getting a Value: g_form.getValue('field_name');
  • Setting a Value: g_form.setValue('field_name', 'value');
  • Hiding a Field: g_form.setDisplay('field_name', false);
  • Adding a Message: g_form.addInfoMessage('This is an info message');

Example Usage

 

 

 

 

javascript
Copy code
// Client script on a Change Request form function onLoad() { var changeType = g_form.getValue('change_type'); if (changeType === 'Standard') { g_form.setDisplay('risk', false); } }

 

 

 

g_sc_form

  • Context: g_sc_form is specifically used in the context of Service Catalog item forms. It is designed to handle catalog item variables rather than table fields.
  • Usage: g_sc_form provides methods similar to g_form, but they are specifically tailored for Service Catalog items. It is used in catalog client scripts and UI policies.
  • Capabilities: It interacts with catalog item forms, including order guides, record producers, and catalog items.

Example Methods

  • Getting a Value: g_sc_form.getValue('variable_name');
  • Setting a Value: g_sc_form.setValue('variable_name', 'value');
  • Hiding a Field: g_sc_form.setDisplay('variable_name', false);
  • Adding a Message: g_sc_form.addInfoMessage('This is an info message');

Example Usage

 

 

 

 

 

javascript
Copy code
// Catalog client script on a Service Catalog item function onLoad() { var requestedFor = g_sc_form.getValue('requested_for'); if (requestedFor === 'admin') { g_sc_form.setDisplay('vip', true); } }

 

 

 

Key Differences

  • Scope:

    • g_form is used for regular forms across all ServiceNow applications.
    • g_sc_form is used exclusively for Service Catalog item forms.
  • Object of Manipulation:

    • g_form manipulates fields within a record in any table.
    • g_sc_form manipulates variables within a catalog item.
  • Context of Use:

    • g_form is typically used in Client Scripts and UI Policies on table forms.
    • g_sc_form is typically used in Catalog Client Scripts and UI Policies on catalog items.

Summary

Use g_form when working with standard record forms in ServiceNow tables, such as incident forms or change request forms. Use g_sc_form when working with Service Catalog items to manipulate catalog item variables. Each object provides methods tailored to its specific context, allowing for appropriate form manipulation based on the type of form being handled.

Amitoj Wadhera
Kilo Sage

Hi @AnubhavS ,

 

All GlideForm methods can be accessed through the global g_form object, which is available in client scripts.

The g_form object does not support variables on task records. Therefore, if you encounter situations where g_form APIs do not work as expected, you can use g_sc_form instead.

 

If you find my response helpful, please consider marking it as the 'Accepted Solution' and giving it a 'Helpful' rating. Your feedback not only supports the community but also encourages me to continue providing valuable assistance.

 

Thanks,

Amitoj Wadhera