Based on the script you provided, here are a few optimization recommendations:
1. Use GlideAggregate instead of GlideRecord: Since you're only interested in a single field (sys_id) from the cmn_cost_center table, you can use GlideAggregate instead of GlideRecord to get the result. GlideAggregate is much faster when dealing with large tables.
2. Use a Join Query: Instead of running two separate queries (one for sys_user and one for cmn_cost_center), you can use a join query to get the data you need in a single query. This can significantly reduce the query execution time.
Here's an updated script include that optimizes the query:
getMyCostCenter: function() {
var answer;
var user = new GlideRecord('sys_user');
user.addQuery('sys_id', gs.getUserID());
user.query();
if (user.next()) {
var cc = new GlideAggregate('cmn_cost_center');
cc.addQuery('sys_id', user.cost_center);
cc.addAggregate('COUNT');
cc.query();
if (cc.next()) {
answer = cc.getValue('sys_id');
}
}
return answer;
}
This script uses a join query and GlideAggregate to get the result. It first queries the sys_user table to get the user's cost center, then joins it with the cmn_cost_center table to get the associated cost center sys_id. Finally, it returns the result as an answer.
I hope this helps! Let me know if you have any further questions.