Glideform query

keep_it_simple
Tera Contributor

Can someone please tell me what is wrong with the below code

function onLoad() {
    var gr = new GlideRecord("incident");
    var x = gr.getNumber("number","INC0000031");  # this incident exists 
    if(x.isValidRecord())
    {
    g_form.setValue('impact',"3");
    }
}
5 ACCEPTED SOLUTIONS

Prashanth redd4
Kilo Guru

The GlideRecord method is no longer recommended due to its performance impact. it retrieves all fields in the requested GlideRecord when most cases only require one field. Instead, use g_scratchpad on a display business rule or use GlideAjax methods.

 

 

View solution in original post

Amit Pandey
Kilo Sage

Hi @keep_it_simple 

 

I am not sure of .getNumber. Instead you should use addQuery() or addEncodedQuery(). Please try following code-

 

function onLoad() {
    var gr = new GlideRecord("incident");
    gr.addQuery("number", "INC0000031");
    gr.query();
    if (gr.next()) {
        g_form.setValue('impact', "3");
    }
}

Please mark my answer helpful and correct.

 

Regards,

Amit

View solution in original post

Amit, 

 

Great response. Came here to say exactly this. 

 

Also https://www.youtube.com/watch?v=H_eoB6LXPrs

Here's a video on why variables shouldn't be named gr

View solution in original post

Harish KM
Kilo Patron
Kilo Patron

Hi @keep_it_simple you cannot use glide record query in client side scripts, convert them to script include and use glide ajax to call,

Also you cannot compare with number compare with sysid instead.

Example: create a client callable script include

Script Include:

 checkRecord: function() {
        var recSysId= this.getParameter('sysparm_sysid'); // get sysid of record from client script
        var rec = new GlideRecord('incident');
       rec.get(recSysId);
        rec.query();
// valid record?
        if (rec.isValidRecord()) {
            return true;
        }
        return false;
    },
 
onload client script:
 var recSysID= g_form.getValue('variablename');
    var ga = new GlideAjax('scriptincludename'); // pass your script include name here
    ga.addParam('sysparm_name', 'checkRecord');//function name used in script include
    ga.addParam('sysparm_sysid', recSysID);
    ga.getXMLAnswer(response);

    function response(answer)
    {
    if (answer == true) {
g_form.setValue('impact',"3");
    } else {
        g_form.setValue('impact',"1");
    }
}
Regards
Harish

View solution in original post

Maddysunil
Kilo Sage

@keep_it_simple 

 

function onLoad() {
    var gr = new GlideRecord("incident");
    gr.addQuery("number", "INC0000031"); // Query for the specific incident number
    gr.query(); // Execute the query

    if (gr.next()) {
        // If the record exists, set the 'impact' field value to "3"
        g_form.setValue('impact', "3");
    }
}

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

View solution in original post

5 REPLIES 5

Prashanth redd4
Kilo Guru

The GlideRecord method is no longer recommended due to its performance impact. it retrieves all fields in the requested GlideRecord when most cases only require one field. Instead, use g_scratchpad on a display business rule or use GlideAjax methods.

 

 

Amit Pandey
Kilo Sage

Hi @keep_it_simple 

 

I am not sure of .getNumber. Instead you should use addQuery() or addEncodedQuery(). Please try following code-

 

function onLoad() {
    var gr = new GlideRecord("incident");
    gr.addQuery("number", "INC0000031");
    gr.query();
    if (gr.next()) {
        g_form.setValue('impact', "3");
    }
}

Please mark my answer helpful and correct.

 

Regards,

Amit

Amit, 

 

Great response. Came here to say exactly this. 

 

Also https://www.youtube.com/watch?v=H_eoB6LXPrs

Here's a video on why variables shouldn't be named gr

Harish KM
Kilo Patron
Kilo Patron

Hi @keep_it_simple you cannot use glide record query in client side scripts, convert them to script include and use glide ajax to call,

Also you cannot compare with number compare with sysid instead.

Example: create a client callable script include

Script Include:

 checkRecord: function() {
        var recSysId= this.getParameter('sysparm_sysid'); // get sysid of record from client script
        var rec = new GlideRecord('incident');
       rec.get(recSysId);
        rec.query();
// valid record?
        if (rec.isValidRecord()) {
            return true;
        }
        return false;
    },
 
onload client script:
 var recSysID= g_form.getValue('variablename');
    var ga = new GlideAjax('scriptincludename'); // pass your script include name here
    ga.addParam('sysparm_name', 'checkRecord');//function name used in script include
    ga.addParam('sysparm_sysid', recSysID);
    ga.getXMLAnswer(response);

    function response(answer)
    {
    if (answer == true) {
g_form.setValue('impact',"3");
    } else {
        g_form.setValue('impact',"1");
    }
}
Regards
Harish