onSubmit catalog client script using gr.addEncodedQuery

MayrelCerero
Tera Expert

Hello, I am a new to ServiceNow and I need some assistance with creating an onSubmit catalog client script. The requirement is that the catalog item should prevent users from requesting the same item more than ten times within ten business days. However, I have encountered some errors where "gr.addEncodedQuery" is not allowed in my script. I would appreciate any help in resolving these issues. Thank you!

function onSubmit() {
    //Prevents a user from being the requested for on this catalog item more than 10 times within 10 business days.
    var requestFor = g_form.getValue('who_is_this_request_for');
    var catalogItemSysID = "785339fa872842d0477964a90cbb35b7";
 
    // Query to count requests by current user for the current catalog item in the last 10 business days
    var gr = new GlideRecord('sc_req_item');
    gr.addQuery('requested_for', requestFor);
    gr.addQuery('sc_cat_item', catalogItemSysID);
    gr.addEncodedQuery('opened_byBETWEENjavascript:gs.beginningOfLast10BusinessDays()@javascript:gs.endOfLast10BusinessDays()');
    gr.query();

    var requestCount = 0;
    while (gr.next()) {
        requestCount++;
    }

    // If request count exceeds 10, abort submission and display error message
    if (requestCount >= 10) {
        g_form.addErrorMessage('You cannot request this item more than 10 times within the last 10 business days.');
        return false; // Abort submission
    }

    return true;
}

 

5 REPLIES 5

James Chun
Kilo Patron

Hey @MayrelCerero,

 

You are using client-side GlideRecord API and it should be 'setEncodedQuery' instead. The 'addEncodedQuery' is server-side GlideRecord API (yup, I know it's weird). Refer to the documentation for more details - https://docs.servicenow.com/bundle/washingtondc-api-reference/page/app-store/dev_portal/API_referenc...

 

Give this a go, cheers.

Harish KM
Kilo Patron
Kilo Patron

Hi @MayrelCerero as per best practice, it is not recommended to use gliderecord query in client script, you can move your validations to client callable script include and return yes/no to onSubmit client script to abort the submission. This improves performance.

Regards
Harish

Aman Kumar S
Kilo Patron

Hi @MayrelCerero 

You must not use GlideRecord query in client script, although its supported but not recommended due to performance implications.

Instead you can use GlideAjax method in fetching data from server side. Now the challenge comes for onsubmit that sync ajax calls are not supported in catalog client scripts, so you need to do some adjustments, refer to code below:

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0783579

 

Best Regards
Aman Kumar

Amit Verma
Kilo Patron
Kilo Patron

Hi @MayrelCerero 

 

It's not recommended to run GlideRecord query on client side scripts. You should move your server-side logic to a Client Callable Script Include and call your Script Include from your On-Submit Client Script. Refer below post for reference :

https://www.servicenow.com/community/developer-forum/calling-a-script-include-from-onsubmit-client-s...

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.