(GlideRecord) [Q:NOCB] Query must be called with a callback function

Mr world wide
Mega Guru

I have a catalog client script to make attachment mandatory based on conditions:

function onSubmit() {
	
	//Get the field
	var field= g_form.getValue("hr_process");
	
	//Make attachment mandatory only based on conditions.
	if(field == 'it_infra'){
		
		var cat_id = g_form.getValue('sysparm_item_guid');
		var gr = new GlideRecord("sys_attachment");
		gr.addQuery("table_name", "sn_hr_core_case"); //Update your table
		gr.addQuery("table_sys_id", cat_id);
		gr.query();
		if (!gr.next()) {
			g_form.addErrorMessage('You must add an attachment before submitting this request.');
			return false;
		}
	}
	
	
}


When I click on submit it always throwing the below warning message:
 (GlideRecord) [Q:NOCB] Query must be called with a callback function.
The form is not submitting on the valid case also. How to fix this issue.

 

 

1 ACCEPTED SOLUTION

Andrew Barnes -
ServiceNow Employee
ServiceNow Employee

Greetings Abdul,

 The error relates to your gr.query() line - in that all gliderecord calls from the client side require a responseFunction per - https://developer.servicenow.com/dev.do#!/reference/api/orlando/client/c_GlideRecordClientSideAPI#r_...

But, before you go and adjust that - you should instead, create a script include and use the GlideAjax API, or use the REST APIs

-AJB

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Using GlideRecord is not recommended in client script

try this

function onSubmit() {
	
	//Get the field
	var field= g_form.getValue("hr_process");
	
	//Make attachment mandatory only based on conditions.
	if(field == 'it_infra'){
		
		var cat_id = g_form.getValue('sysparm_item_guid');
		var gr = new GlideRecord("sys_attachment");
		gr.addQuery("table_name", "sn_hr_core_case"); //Update your table
		gr.addQuery("table_sys_id", cat_id);
		gr.query(checkRecord);
		
		function checkRecord(gr){
		if(!gr.next()) {
			g_form.addErrorMessage('You must add an attachment before submitting this request.');
			return false;
		}
		}	
	}
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

As you can see in Ankur's example, the query method requires a response function as a parameter. Per https://developer.servicenow.com/dev.do#!/reference/api/orlando/client/c_GlideRecordClientSideAPI#r_...

@Abdul Waheed 

Try this without using GlideRecord as using GlideRecord is not recommended.

function onSubmit() {
	
	//Get the field
	var field = g_form.getValue("hr_process");
	var count = getCurrentAttachmentNumber();
	
	//Make attachment mandatory only based on conditions.
	if(field == 'it_infra' && count == ''){
			g_form.addErrorMessage('You must add an attachment before submitting this request.');
			return false;
	}
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Thanks for the comment  @Ankur Bawiskar