Is it advisable to query 3 tables in the same script includes?

Lon Landry4
Mega Sage

Howdy Community,

Before I try writing the code, I figured it was a good idea to see if querying three tables in one script includes falls within best practices...

 

Is it advisable to query 3 tables in the same script includes?

 

Notes for script:

All is within global scope & we are in Washington DC release.

1st table - custom table -fetch record using employee_number

2nd table is Hardware table [alm_hardware} - looking for assets assigned to user
(if user does not meet filter condition then user removed from the query)

3rd table is User [sys_user} - if user still in query- match against filter condition for this table

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Yes, querying three tables within the same Script Include is generally acceptable in ServiceNow, provided you manage performance efficiently and avoid excessive data fetching. However, there are a few best practices and considerations to ensure the script remains efficient and scalable, especially since you're working in the global scope and are on the Washington DC release.

 

Best Practices for Querying Multiple Tables in a Script Include

  1. Use Efficient Queries:

    • Ensure each query is optimized, using specific conditions and indexed fields to minimize the load on the system.
    • Avoid querying for unnecessary fields by limiting the fields returned using the addQuery() method.
    • Only query for the data that is absolutely necessary. For example, if you're fetching records using employee_number, make sure the field is indexed to improve performance.

     

  2. Break the Script into Separate Functions (If Possible):
  3. If possible, limit the number of records returned by setting an appropriate condition on each query. This can be achieved by filtering based on relevant business logic.

View solution in original post

2 REPLIES 2

Community Alums
Not applicable

Yes, querying three tables within the same Script Include is generally acceptable in ServiceNow, provided you manage performance efficiently and avoid excessive data fetching. However, there are a few best practices and considerations to ensure the script remains efficient and scalable, especially since you're working in the global scope and are on the Washington DC release.

 

Best Practices for Querying Multiple Tables in a Script Include

  1. Use Efficient Queries:

    • Ensure each query is optimized, using specific conditions and indexed fields to minimize the load on the system.
    • Avoid querying for unnecessary fields by limiting the fields returned using the addQuery() method.
    • Only query for the data that is absolutely necessary. For example, if you're fetching records using employee_number, make sure the field is indexed to improve performance.

     

  2. Break the Script into Separate Functions (If Possible):
  3. If possible, limit the number of records returned by setting an appropriate condition on each query. This can be achieved by filtering based on relevant business logic.

Isaac Vicentini
Mega Sage
Mega Sage

Hi @Lon Landry4,

 

Yes, if there are 3 queries related to the same functionality, it is good practice to make them in a single Script Include.

You can define this Script Include as a layer that only performs CRUD.

 

If you need to process the output of this data and this processing has many requirements, then you can create a Script Include layer just for processing the information.

 

The most important thing is that your Script Include is not too long and has a well-defined purpose.

 


If my answer helped you in any way, please mark it as correct/helpful 🙂

Regards,

Isaac Vicentini.

 




Best regards,

Isaac Vicentini
MVP 2025


If my answer was helpful, mark it as Helpful or Accept as Solution.