REST API calls for AWA not fully completing even though response is sent?

Community Alums
Not applicable

I have a scripted REST API that makes 3 internal API calls to the AWA Agent API, AWA Assign API and the AWA Inbox Actions API, one after the other in the script. I am getting inconsistent results with this and have discovered that the REST calls are not being fully actioned before they continue to the next API call, even though I get a 200 response.

 

I am using sn_ws.RESTMessageV2() to call each of the APIs and am using execute() as opposed to executeAsync(). I assumed that because of this that they would be synchronously processed and each call would fully complete before the response message is returned for each of them. I have added a loop in my code for the AWA Assign API call and do a GlideRecord call to keep checking the value of the assigned_to field on the work item record. This is after the response 200 has been received. I have output this to the logs and can see that it that the assigned to value is not immediately updated and in the example below it took 31 GlideRecord queries/loops until it had the value populated. This varies with each test I do, presumably due to whatever is happening on the instance at the same time.

 

Has anyone else experienced this before? Is this a ServiceNow bug? What is the best way to handle this?

 

Any thoughts or ideas appreciated!

 

Steve_J_0-1731170192344.png

 

Steve_J_1-1731170201394.png

 

 

 

var urlAssign = url_instance + "api/now/awa/workitems/" + work_itemSysId + "/assignments";
log.logDebug(int_num + " " + user_email + ": AWA_Assign urlAssign = " + urlAssign);
var AWA_Assign = new sn_ws.RESTMessageV2();
AWA_Assign.setAuthenticationProfile('xxxxx', 'xxxxxxxxxxxx');
AWA_Assign.setEndpoint(urlAssign);
AWA_Assign.setHttpMethod('POST');
var requestBodyJSON = JSON.stringify({
	"agent_sys_id": user_sys_id,
	"enable_auto_assign": "true"
});
AWA_Assign.setRequestBody(requestBodyJSON);
AWA_Assign.setRequestHeader('Accept', 'application/json');
AWA_Assign.setRequestHeader('Content-Type', 'application/json');
var responseAssign = AWA_Assign.execute();

var httpStatusAssign = responseAssign.getStatusCode();
var responseBodyAssign = responseAssign.getBody(); //get the message body as a string

if (httpStatusAssign == 200) {
	log.logInfo(int_num + " " + user_email + ": AWA Assign API REST call SUCCESSFUL: " + httpStatusAssign + " " + responseBodyAssign);

	//double check that it has now been assigned
	var maxRetries = 100;
	var retries = 0;
	var fieldUpdated = false;

	while (retries < maxRetries && !fieldUpdated) {
		//gs.sleep(200); // wait for 200 ms
		
		var gr = new GlideRecord('awa_work_item');
		if (gr.get(work_itemSysId) && gr.assigned_to == user_sys_id) { 
			fieldUpdated = true;
		}
		log.logInfo(int_num + " " + user_email + ": AWA Assign API REST call checking loop " + retries + " : gr.assigned_to = " + gr.assigned_to);
		retries++;
	}
	if (!fieldUpdated) {
		gs.error("Field was not updated within expected time frame");
		log.logError(int_num + " " + user_email + ": AWA Assign API REST call FAILED: the value was not updated even though a 200 OK was received");
	} else {
		log.logInfo(int_num + " " + user_email + ": AWA Assign API REST call SUCCESSFUL: " + httpStatusAssign + " " + responseBodyAssign);
	}

 

 

 

 

2 REPLIES 2

Abhay Kumar1
Giga Sage

@Community Alums Have you tried with gs.sleep , if not then pls try with 500 value, ifworks for you.

 

And also reduce max retries entery if possible because 100 is huge number.

Community Alums
Not applicable

Thanks @Abhay Kumar1 .

 

I added the loop to try and understand what was happening with the rest calls and why the subsequent one was failing. So this is more for diagnostic purposes and not something I would necessarily want to implement.

 

This is in a scoped app so I can't use gs.sleep but potentially I could add some code that mimics this. However, I am concerned about what impact on performance this may have. Do you or anyone else have any thoughts on this?