setLimit(1); performance issue
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2022 12:24 PM
Hi All
When we are triggering multiple times this table it's not updating the records every time,
For example, if we trigger 5 times this BR simultaneously by 5 people it will work for half of the time only.
Any solution for this kind of performance issue?
gr = new GlideRecord('u_custom_integration');
gr.orderByDesc("sys_created_on");
gr.setLimit(1);
gr.query();
if (gr.next()) {
gs.log("cmtin101");
ritmNumber2 = sc_item.getValue('number');
//below code is not updating every time
gr.u_ritm_number = ritmNumber2;
gr.u_trigger_status = "Processed";
gr.u_email_outputresponse = "email_body_file_sys_id::" + ecsysid + "\n" + "operations_XMAP:true" + "\n" + "sys_id:" + gr4 + "\n" + "urn:" + actionstatus + "\n" + "ritm_num:" + gr6 + "\n" + "email_body_file_table:u_email_client" + "\n" + "table:sc_req_item";
gr.update();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2022 01:01 PM
HI @jobin1,
Your code seems to be broken in to pieces, i guess you tried to share part of a function can you please provide the complete code ?
thanks,
sohail khilji
☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2022 10:45 PM
Below is my full script
(function executeRule(current, previous /*null when async*/ ) {
//Fetching values from custom_integration table.
var userid = current.getDisplayValue('u_userid');
var ritmcmpy = current.getDisplayValue('u_ritm_company');
var taskid = current.getDisplayValue('u_taskid');
var triggercount = current.getDisplayValue('u_trigger_count');
var triggerstatus = current.getDisplayValue('u_trigger_status');
var ritmnumber = current.getDisplayValue('u_ritm_number');
var sessionid = current.getDisplayValue('u_sessionid');
var actionstatus = current.getDisplayValue('u_action_status');
var emailsender = current.getDisplayValue('u_email_sender');
var emailcc = current.getDisplayValue('u_email_cc');
var emailbcc = current.getDisplayValue('u_email_bcc');
var emailinbound = current.getDisplayValue('u_email_inbound');
var emailoutputresponse = current.getDisplayValue('u_email_outputresponse');
var emailbody = current.u_email_body;
var str4 = GlideSPScriptable().stripHTML(emailbody);
var emailsubject = current.getDisplayValue('u_email_subject');
//subject changes for /open/wip/pending tickets/only new email client entry/ starts
if (emailsubject.includes("RITM")) {
var subject = emailsubject;
var indexStart = subject.indexOf('RITM');
var number = subject.slice(indexStart, indexStart + 12);
var grritm = new GlideRecord("sc_req_item");
grritm.addQuery('number', number);
grritm.addEncodedQuery('stateIN1,2,101,102,103,6');
grritm.query();
if (grritm.next()) {
var grritmval = grritm.getValue('sys_id');
grritm.u_mailbox_address = userid;
grritm.comments = str4;
grritm.update();
var gr4 = grritm.getValue('number');
gr4 = grritm.sys_id.toString();
//mailbox address is passing to u_from
var emailId4 = grritm.u_mailbox_address;
var emailadddress3 = new GlideRecord('u_email_reply_address');
emailadddress3.addQuery('u_reply_to', emailId4);
emailadddress3.query();
if (emailadddress3.next())
var ecfrom5 = emailadddress3.sys_id.toString();
//Updated for u_from field in email client end
var gr5 = new GlideRecord('u_email_client');
gr5.initialize();
gr5.u_subject = emailsubject;
gr5.u_to_whom = userid;
gr5.u_cc = emailcc;
gr5.u_bcc = emailbcc;
gr5.u_from_address = emailsender;
gr5.u_from = ecfrom5;
gr5.u_status = "Received";
gr5.u_item = gr4;
if (emailinbound.includes("html_content:false")) {
gr5.u_message = emailbody;
} else {
gr5.u_html_message = emailbody;
}
gr5.u_sender = emailsender;
gr5.u_email_direction = "Received";
gr5.insert();
var ecsysid = gr5.getValue('sys_id');
var gr6 = grritm.getValue('number');
var gr = new GlideRecord('u_custom_integration');
gr.orderByDesc("sys_created_on");
gr.setLimit(1);
gr.query();
if (gr.next()) {
var ritmNumber2 = grritm.getValue('number');
gr.u_ritm_number = ritmNumber2;
gr.u_trigger_status = "Processed";
gr.u_email_outputresponse = "email_body_file_sys_id::" + ecsysid + "\n" + "operations_XMAP:true" + "\n" + "sys_id:" + gr4 + "\n" + "urn:" + actionstatus + "\n" + "ritm_num:" + gr6 + "\n" + "email_body_file_table:u_email_client" + "\n" + "table:sc_req_item";
gr.update();
}
}
}
//subject changes for /open/wip/pending tickets/only new email client entry/ ends
//subject changes for Cancelled and closed ticket starts/RITM and email client entry/starts
//Without Ritm in subject /Normal scenario starts
if (emailsubject.includes("RITM") || (!emailsubject.includes("RITM"))) {
subject = emailsubject;
indexStart = subject.indexOf('RITM');
number = subject.slice(indexStart, indexStart + 12);
var sc_item1 = new GlideRecord("sc_req_item");
sc_item1.addQuery('number', number);
sc_item1.query();
if (sc_item1.next()) {
var val = sc_item1.getValue('sys_id');
var stateval = sc_item1.getDisplayValue('state');
if ((stateval == 'Closed Complete') || (stateval == 'Closed Incomplete') || (stateval == 'Closed Skipped') || (stateval == 'Cancelled')) {
var sc_itemval = sc_item1.getValue('sys_id');
var user_id, country, company, phone, company_sysid;
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item;
//Validation of Catalog Item-Starts here
if (sessionid == "Standard HR Query") {
item = cart.addItem('a9bb0be5379b524021865ca543990ebf');
cart.setVariable(item, 'stan_hr_issue_category', 'HR Query');
} else if (sessionid == "Standard Invoice Query") {
item = cart.addItem('b1deb33a0f989240e65a4b9ce1050e51');
cart.setVariable(item, 'stan_issue_category', 'Invoice Follow-up');
} else if (sessionid == "Standard Query") {
item = cart.addItem('edf0890b37d8fe0040ba70f543990e85');
cart.setVariable(item, 'stan_hr_issue_category', 'General Query');
} else if (sessionid == "Standard Learning Query") {
item = cart.addItem('86fb9e6e37a1e68040ba70f543990e74');
cart.setVariable(item, 'stan_hr_issue_category', 'HR Query');
}
user_id, country, company, phone, company_sysid;
cartId = GlideGuid.generate(null);
cart = new Cart(cartId);
item;
//Validation of Catalog Item-Starts here
if (sessionid == "Standard HR Query") {
item = cart.addItem('a9bb0be5379b524021865ca543990ebf');
cart.setVariable(item, 'stan_hr_issue_category', 'HR Query');
} else if (sessionid == "Standard Invoice Query") {
item = cart.addItem('b1deb33a0f989240e65a4b9ce1050e51');
cart.setVariable(item, 'stan_issue_category', 'Invoice Follow-up');
} else if (sessionid == "Standard Query") {
item = cart.addItem('edf0890b37d8fe0040ba70f543990e85');
cart.setVariable(item, 'stan_hr_issue_category', 'General Query');
} else if (sessionid == "Standard Learning Query") {
item = cart.addItem('86fb9e6e37a1e68040ba70f543990e74');
cart.setVariable(item, 'stan_hr_issue_category', 'HR Query');
}
//Validation of Requested for-Starts here
var domain, domain1;
var com = new GlideRecord("core_company");
com.addQuery('name', ritmcmpy);
com.query();
if (com.next()) {
domain = com.sys_domain;
domain1 = domain.getDisplayValue();
company_sysid = com.sys_id;
}
user_id;
var user = new GlideRecord("sys_user");
user.addQuery('sys_domain', domain);
user.addQuery('email', emailsender);
user.query();
if (user.next()) {
user_id = user.getValue('sys_id');
} else {
var default_user = new GlideRecord("sys_user");
default_user.addQuery('sys_domain', domain);
default_user.addQuery('user_name', 'CONTAINS', 'nobody');
default_user.query();
if (default_user.next()) {
user_id = default_user.getValue('sys_id');
}
}
var cartGR = cart.getCart();
cartGR.requested_for = user_id;
var reqfor = cartGR.requested_for.getDisplayValue();
cartGR.update();
cart.setVariable(item, 'req_for', user_id);
cart.setVariable(item, 'u_country', country);
cart.setVariable(item, 'contact', phone);
cart.setVariable(item, 'company', company);
cart.setVariable(item, 'shrt_des', emailsubject);
cart.setVariable(item, 'des', str4);
var rc = cart.placeOrder();
number = rc.number;
var sysID = rc.sys_id;
var num = number.toString();
var num2 = '';
var sc_item = new GlideRecord('sc_req_item');
sc_item.addQuery('request', sysID);
sc_item.query();
if (sc_item.next()) {
num2 = sc_item.number.toString();
sc_item.u_requestor = sc_item.opened_by;
sc_item.u_mailbox_address = userid;
sc_item.contact_type = 'email';
sc_item.urgency = '3 - Low';
sc_item.short_description = emailsubject;
str4 = GlideSPScriptable().stripHTML(emailbody);
sc_item.description = str4;
sc_item.u_hr_request_type = 'General Query';
var gr2 = new GlideRecord('core_company');
gr2.addQuery('name', ritmcmpy);
gr2.query();
if (gr2.next())
var cmpyal = gr2.sys_id.toString();
sc_item.company = cmpyal;
//assignment group validation with respect to company config.
var grint = new GlideRecord('u_company_config_intg');
grint.addQuery('u_company.name', ritmcmpy);
grint.addQuery('u_active=True');
grint.query();
if (grint.next()) {
var value = grint.getValue('u_language_translation_exclude_list');
var arr = value.split(',');
var emailArr = [];
for (var i in arr) {
var arr1 = arr[i].split(':');
var obj = {};
obj["email"] = arr1[0].toString();
obj["otherValue"] = arr1[1].toString();
emailArr.push(obj);
}
var myFinalValue = '';
var inputvalue = userid;
for (var j = 0; j < emailArr.length; j++) {
if (inputvalue == emailArr[j].email) {
myFinalValue = emailArr[j].otherValue;
}
}
var gr1 = new GlideRecord('sys_user_group');
gr1.addQuery('name', myFinalValue);
gr1.query();
if (gr1.next()) {
var gpsyal = gr1.sys_id.toString();
sc_item.assignment_group = gpsyal;
}
} else {
sc_item.assignment_group = " ";
}
sc_item.update();
}
gr4 = sc_item.getValue('number');
gr4 = sc_item.sys_id.toString();
//mailbox address is passing to u_from
emailId4 = sc_item.u_mailbox_address;
emailadddress3 = new GlideRecord('u_email_reply_address');
emailadddress3.addQuery('u_reply_to', emailId4);
emailadddress3.query();
if (emailadddress3.next())
ecfrom5 = emailadddress3.sys_id.toString();
//Updated for u_from field in email client end
gr5 = new GlideRecord('u_email_client');
gr5.initialize();
gr5.u_subject = emailsubject;
gr5.u_to_whom = userid;
gr5.u_cc = emailcc;
gr5.u_bcc = emailbcc;
gr5.u_from_address = emailsender;
gr5.u_from = ecfrom5;
gr5.u_status = "Received";
gr5.u_item = gr4;
if (emailinbound.includes("html_content:false")) {
gr5.u_message = emailbody;
} else {
gr5.u_html_message = emailbody;
}
gr5.u_sender = emailsender;
gr5.u_email_direction = "Received";
gr5.insert();
ecsysid = gr5.getValue('sys_id');
gr6 = sc_item.getValue('number');
gr = new GlideRecord('u_custom_integration');
gr.orderByDesc("sys_created_on");
gr.setLimit(1);
gr.query();
if (gr.next()) {
ritmNumber2 = sc_item.getValue('number');
gr.u_ritm_number = ritmNumber2;
gr.u_trigger_status = "Processed";
gr.u_email_outputresponse = "email_body_file_sys_id::" + ecsysid + "\n" + "operations_XMAP:true" + "\n" + "sys_id:" + gr4 + "\n" + "urn:" + actionstatus + "\n" + "ritm_num:" + gr6 + "\n" + "email_body_file_table:u_email_client" + "\n" + "table:sc_req_item";
gr.update();
}
}
}
//subject changes for Cancelled and closed ticket /RITM and email client entry/ends
//Without Ritm in subject /Normal scenario ends
else {
//invalid Ritm in subject starts
user_id,
country,
company,
phone,
company_sysid;
cartId = GlideGuid.generate(null);
cart = new Cart(cartId);
item;
//Validation of Catalog Item-Starts here
if (sessionid == "Standard HR Query") {
item = cart.addItem('a9bb0be5379b524021865ca543990ebf');
cart.setVariable(item, 'stan_hr_issue_category', 'HR Query');
} else if (sessionid == "Standard Invoice Query") {
item = cart.addItem('b1deb33a0f989240e65a4b9ce1050e51');
cart.setVariable(item, 'stan_issue_category', 'Invoice Follow-up');
} else if (sessionid == "Standard Query") {
item = cart.addItem('edf0890b37d8fe0040ba70f543990e85');
cart.setVariable(item, 'stan_hr_issue_category', 'General Query');
} else if (sessionid == "Standard Learning Query") {
item = cart.addItem('86fb9e6e37a1e68040ba70f543990e74');
cart.setVariable(item, 'stan_hr_issue_category', 'HR Query');
}
//Validation of Requested for-Starts here
domain,
domain1;
com = new GlideRecord("core_company");
com.addQuery('name', ritmcmpy);
com.query();
if (com.next()) {
domain = com.sys_domain;
domain1 = domain.getDisplayValue();
company_sysid = com.sys_id;
}
user_id;
user = new GlideRecord("sys_user");
user.addQuery('sys_domain', domain);
user.addQuery('email', emailsender);
user.query();
if (user.next()) {
user_id = user.getValue('sys_id');
} else {
default_user = new GlideRecord("sys_user");
default_user.addQuery('sys_domain', domain);
default_user.addQuery('user_name', 'CONTAINS', 'nobody');
default_user.query();
if (default_user.next()) {
user_id = default_user.getValue('sys_id');
}
}
cartGR = cart.getCart();
cartGR.requested_for = user_id;
reqfor = cartGR.requested_for.getDisplayValue();
cartGR.update();
cart.setVariable(item, 'req_for', user_id);
cart.setVariable(item, 'u_country', country);
cart.setVariable(item, 'contact', phone);
cart.setVariable(item, 'company', company);
cart.setVariable(item, 'shrt_des', emailsubject);
cart.setVariable(item, 'des', str4);
rc = cart.placeOrder();
number = rc.number;
sysID = rc.sys_id;
num = number.toString();
num2 = '';
sc_item = new GlideRecord('sc_req_item');
sc_item.addQuery('request', sysID);
sc_item.query();
if (sc_item.next()) {
num2 = sc_item.number.toString();
sc_item.u_requestor = sc_item.opened_by;
sc_item.u_mailbox_address = userid;
sc_item.contact_type = 'email';
sc_item.urgency = '3 - Low';
sc_item.short_description = emailsubject;
str4 = GlideSPScriptable().stripHTML(emailbody);
sc_item.description = str4;
sc_item.u_hr_request_type = 'General Query';
gr2 = new GlideRecord('core_company');
gr2.addQuery('name', ritmcmpy);
gr2.query();
if (gr2.next())
cmpyal = gr2.sys_id.toString();
sc_item.company = cmpyal;
//assignment group validation with respect to company config.
grint = new GlideRecord('u_company_config_intg');
grint.addQuery('u_company.name', ritmcmpy);
grint.addQuery('u_active=True');
grint.query();
if (grint.next()) {
value = grint.getValue('u_language_translation_exclude_list');
arr = value.split(',');
emailArr = [];
for (var i in arr) {
arr1 = arr[i].split(':');
obj = {};
obj["email"] = arr1[0].toString();
obj["otherValue"] = arr1[1].toString();
emailArr.push(obj);
}
myFinalValue = '';
inputvalue = userid;
for (var j = 0; j < emailArr.length; j++) {
if (inputvalue == emailArr[j].email) {
myFinalValue = emailArr[j].otherValue;
}
}
gr1 = new GlideRecord('sys_user_group');
gr1.addQuery('name', myFinalValue);
gr1.query();
if (gr1.next()) {
gpsyal = gr1.sys_id.toString();
sc_item.assignment_group = gpsyal;
}
} else {
sc_item.assignment_group = " ";
}
sc_item.update();
}
gr4 = sc_item.getValue('number');
gr4 = sc_item.sys_id.toString();
//mailbox address is passing to u_from
emailId4 = sc_item.u_mailbox_address;
emailadddress3 = new GlideRecord('u_email_reply_address');
emailadddress3.addQuery('u_reply_to', emailId4);
emailadddress3.query();
if (emailadddress3.next())
ecfrom5 = emailadddress3.sys_id.toString();
//Updated for u_from field in email client end
gr5 = new GlideRecord('u_email_client');
gr5.initialize();
gr5.u_subject = emailsubject;
gr5.u_to_whom = userid;
gr5.u_cc = emailcc;
gr5.u_bcc = emailbcc;
gr5.u_from_address = emailsender;
gr5.u_from = ecfrom5;
gr5.u_status = "Received";
gr5.u_item = gr4;
if (emailinbound.includes("html_content:false")) {
gr5.u_message = emailbody;
} else {
gr5.u_html_message = emailbody;
}
gr5.u_sender = emailsender;
gr5.u_email_direction = "Received";
gr5.insert();
ecsysid = gr5.getValue('sys_id');
gr6 = sc_item.getValue('number');
gs.log("cmtin91"+gr6);
gr = new GlideRecord('u_custom_integration');
gr.orderByDesc("sys_created_on");
gr.setLimit(1);
gr.query();
//gs.sleep(5000);
if (gr.next()) {
gs.log("cmtin101");
ritmNumber2 = sc_item.getValue('number');
gr.u_ritm_number = ritmNumber2;
gr.u_trigger_status = "Processed";
gr.u_email_outputresponse = "email_body_file_sys_id::" + ecsysid + "\n" + "operations_XMAP:true" + "\n" + "sys_id:" + gr4 + "\n" + "urn:" + actionstatus + "\n" + "ritm_num:" + gr6 + "\n" + "email_body_file_table:u_email_client" + "\n" + "table:sc_req_item";
gr.update();
}
}
}
//invalid Ritm in subject ends
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2022 11:25 AM
Hi, providing code is a good step forward but without any clear details as to the codes purpose and the results of your diagnostics the forum is still guessing as to the actual issue.
You appear to be duplicating code rather than configuring functions and passing in parameters to suit the requirements when they are called, which makes the code long and it appears more complicated that I suspect it needs to be. You are also instantiating GlideRecord with variables names that already exists IE GR5 I suspect this happens as line 97, means this function will be called for all records.
if (emailsubject.includes("RITM") || (!emailsubject.includes("RITM"))) {
Also, the 'gs.sleep()' methods (currently commented out) would have caused significant delay if used.
Perhaps you can update this thread with clear details of your diagnosis\explain why you think setLimit(1) is causing performance issues? and details of the functions\gliderecord queries that fail?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2022 01:09 PM
Hi, unfortunately, your post has no clear details of the BR's use\context\trigger conditions etc.
Most ServiceNow functionality is batch processed, and then sequentially within the batch and I think chances of any BR running 5 times simultaneously is extremely remote, even if it did occur java-script should maintain scoped separation unless you are utilizing global variables somewhere.
Perhaps the issue is that the code is not running against the record you are expecting it to run against, but with no context it is not something that the forum can evaluate.
Rather than logging 'cmtin101' try logging unique identifiers for the record involved, sys_id, task number etc.
if you could update this thread with more specific details the forum, would be in a better position to review and advise.