Slow GlideRecord update() in Widget

Shark0os17
Tera Contributor

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);
		
	}

 

 

 

 

 

1 ACCEPTED SOLUTION

Community Alums
Not applicable

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

View solution in original post

2 REPLIES 2

Subhashis Ratna
Tera Guru

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

SubhashisRatna_0-1712834240419.png


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

Community Alums
Not applicable

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