script include usable ways?

srinivas_366
Tera Contributor

When we use mainly script include? 

4 REPLIES 4

piyushsain
Tera Guru
Tera Guru

Script Includes are reusable server-side script logic that define a function or class. They can be reused multiple times by calling them from Client side and other server scripts

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,
Piyush Sain

Sandeep Rajput
Tera Patron
Tera Patron

@srinivas_366 Following are some usage of script include.

1. Script includes are reusable server side scripts which can contain core business logic for an application and can be used in many places eg. business rules, client script, workflow scripts, flow scripts etc.

2. Script includes can be called via client script using GlideAjax API to execute server side code and get result on the client side.

Nivedita Patil
Mega Sage
Mega Sage

Hi @srinivas_366 ,

 

Please refer the below link.

https://developer.servicenow.com/dev.do#!/learn/learning-plans/utah/new_to_servicenow/app_store_lear...

 

Mark my answer as accepted solution and helpful if helps you.

 

Thanks,

Nivedita Patil.

Danish Bhairag2
Tera Sage
Tera Sage

Hi @srinivas_366 ,

 

In ServiceNow, Script Includes are used to store JavaScript functions that can be reused in various scripts and components throughout the application. They are a way to encapsulate server-side logic, making the code modular, maintainable, and easier to reuse. Script Includes are particularly useful in the following scenarios:

 

1. **Business Logic Reusability:**

   - When you have complex business logic that needs to be shared across multiple scripts, such as Business Rules, Scripted REST APIs, or Scheduled Jobs, you can encapsulate this logic in a Script Include. This way, the logic remains consistent and can be easily updated in one central location.

 

2. **Integration Scripts:**

   - When integrating ServiceNow with external systems using scripts, Script Includes can store functions related to data transformation, API calls, error handling, and other integration-specific logic. These functions can be reused in multiple integration scripts.

 

3. **Scripted Web Services:**

   - When creating Scripted REST APIs or Scripted SOAP Web Services, Script Includes can store the processing logic for incoming requests. This separation of concerns improves code organization and readability.

 

4. **Scheduled Jobs:**

   - When you need to perform scheduled tasks, such as data cleanup, data synchronization, or sending notifications, you can encapsulate the job logic in a Script Include. Scheduled Jobs can then call functions from the Script Include to execute the specific tasks.

 

5. **Data Transformation and Validation:**

   - When dealing with data transformation or validation logic, such as formatting dates, manipulating JSON objects, or validating user inputs, Script Includes can store these helper functions. These functions can be reused across various scripts and modules in the application.

 

6. **Client Scripts and UI Policies:**

   - While most Script Includes are used on the server-side, they can also contain client-side functions used in Client Scripts and UI Policies. This ensures consistency in client-side behavior and reduces redundancy.

 

By utilizing Script Includes in these scenarios, developers can adhere to the DRY (Don't Repeat Yourself) principle, promoting efficient, maintainable, and reusable code within the ServiceNow platform.

 

Thanks,

Danish