- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2024 03:02 AM
Hi,
I've developed a widget that groups records from sc_task, allowing users to close them collectively on the portal.
However, I'm facing a performance issues. When attempting to update 40 records to "close complete," the process takes 55 seconds to complete.
Can anyone review my code and suggest a better solution?
BTW, I need the flow to run, so adding setWorkFlow(false) is not a solution here.
if(input && input.action == "submit_approvals"){
// loop and approve or reject materials
var grST = new GlideRecord('sc_task');
grST.addEncodedQuery("request_itemSTARTSWITH"+data.ritm_number+"^stateNOT IN3,4");
grST.orderBy('number');
grST.query();
while(grST.next()){
input.approvals.forEach(function(material) {
if (material.task_number === grST.getValue("number") && material.approvalStatus == "1") {
grST.setValue('state',3);
grST.setValue('close_notes', "Materials approved");
}else if(material.task_number === grST.getValue("number") && material.approvalStatus == "0"){
grST.setValue('state',4);
grST.setValue('close_notes', "Materials rejected");
}
grST.update();
});
}
}
//submit approval
c.submitApprovals = function(){
c.data.disable= true;
c.server.get({
action:"submit_approvals",
approvals: c.data.list
});
spUtil.addInfoMessage("Approvals submitted");
setTimeout(function() {
var redirect = 'esc'
top.window.location = redirect;
}, 1500);
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2024 04:32 AM - edited 04-11-2024 04:32 AM
Hi @Shark0os17 ,
Can you please try to place grST.update(); outside your " forEach" Loop, may be this will reduce some time.
Please mark correct and helpful if it works for you
Thanks and Regards
Sarthak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2024 04:19 AM
Hi @Shark0os17
Your revised logic is well-optimized for better performance. Utilizing a traditional for loop allows for more control over loop execution, potentially improving efficiency. Additionally, reducing the number of database operations and avoiding nested loops are effective strategies for enhancing performance
if (input && input.action == "submit_approvals") {
var taskApprovals = {};
for (var i = 0; i < input.approvals.length; i++) {
var material = input.approvals[i];
taskApprovals[material.task_number] = material.approvalStatus;
}
var grST = new GlideRecord('sc_task');
grST.addEncodedQuery("request_itemSTARTSWITH" + data.ritm_number + "^stateNOT IN3,4");
grST.orderBy('number');
grST.query();
while (grST.next()) {
var taskNumber = grST.getValue("number");
var approvalStatus = taskApprovals[taskNumber];
if (approvalStatus == "1") {
grST.setValue('state', 3);
grST.setValue('close_notes', "Materials approved");
} else if (approvalStatus == "0") {
grST.setValue('state', 4);
grST.setValue('close_notes', "Materials rejected");
}
grST.update();
}
}
If this solution resolves your query, kindly mark it as the accepted solution and give it a thumbs up.
Thanks,
Subhashis Ratna
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2024 04:32 AM - edited 04-11-2024 04:32 AM
Hi @Shark0os17 ,
Can you please try to place grST.update(); outside your " forEach" Loop, may be this will reduce some time.
Please mark correct and helpful if it works for you
Thanks and Regards
Sarthak