What did I do wrong in this script?

Jessica28
Tera Guru

Hello,

The following script is working fine in Scripts Background.  However, when I put it in onChange, it returns "0" record.

Please let me know what I did wrong?  Thank you

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var requestor = g_form.getValue('who_is_this_request_for');

    var gr = new GlideRecord('alm_asset');
    gr.addQuery('assigned_to', requestor);
    gr.addQuery('asset_tag''!=''');
    gr.query();
    var Count = 0;
    while (gr.next()) {
        Count++;
    }
    alert("Count is " + Count);
}

 

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi @Jessica28 ,

 

Example:

 

Script include:

var Test = Class.create();
Test.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    test: function() {

        var callerId = this.getParameter('callerId');
        var Count = 0;
        var gr = new GlideRecord('alm_asset');
        gr.addQuery('assigned_to', callerId);
        gr.addEncodedQuery('asset_tagISNOTEMPTY');
        gr.query();
        if(gr.next()) {
            Count++;
        }
        return Count;
    },
    type: 'Test'
});
 
Client Script:
 var requestor = g_form.getValue('caller_id');
    var ga = new GlideAjax("Test");
    ga.addParam('sysparm_name', 'test'); //Method
    ga.addParam('callerId', requestor); //Parameters
    ga.getXMLAnswer(getResponse);

    function getResponse(response)
    {
        g_form.setValue('short_description', response);
    }
 
Thanks,
Vikas

View solution in original post

9 REPLIES 9

Sharan Ellendul
Tera Contributor

Hi,

 

As a developer, we should avoid using “GlideRecord” in the client script. Try using server side scripting for gliding the records.

 

Thanks

Mohith Devatte
Tera Sage
Tera Sage

Hello @Jessica28 ,PLEASE DONT USE GLIDE RECORD API WHICH IS A SERVER SIDE API IN CLIENT SCRIPTS 

i would suggest you to call a script include and pass the value of who_is_this_request_for value as a parameter to the function and perform the glide record over there and then return the count which should work easily.

 

Hope this helps 

Mark the answer correct if this helps you 

Thanks

Harish Bainsla
Kilo Patron
Kilo Patron

Its Not good practice to use glide record to client side instead of that you can use script include and to call it client side use glide ajjax

Chetan Mahajan
Kilo Sage
Kilo Sage

Hello @Jessica28 ,

                                      Its not recommended to server side script in client side. you can use glide ajax instead. Refer this thread to understand how to utilize Server side script at client side.  GlideAjax Example Cheat Sheet Video Servicenow GlideAjax Script Include Example 


Kindly mark correct and helpful if applicable