(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

@Abdul Waheed 

Did you accidentally mark other response as correct?

Would you mind marking my response as correct and helpful If I was able to help you?

Regards
Ankur

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

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

Thanks for the reference @Andrew Barnes - AJB 

Mr world wide
Mega Guru

Hi team, I can able to resolve my issue with the following logic:

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 = top.window.$("#sc_cat_item").scope().$parent.attachments;
		var length = cat_id.length;
		if(length <= 0) {
			g_form.addErrorMessage('You must add an attachment before submitting this request.');
			return false;
		}
	}
	
	
}