Pull data from cmdb table to variable on order form based on requested for

Petra Zelenak
Tera Contributor

I have an item where I want to pull information from a custom cmdb table based on the requested for field. It can be anyone not just the logged in user. So my attempts with Get User failed miserably.

 

Table: u_cmdb_ci_phone_number

Number is stored in the "name" field and is "assigned_to" to an employee who would be the same as "requested_for".

 

PetraZelenak_0-1666877654781.png

We also have an added filters on the Variable that the "Number State" has to be either Reserved or Assigned, and "Type" is Mobile.

This is a prerequisite check and call for later Order Guide options and I can't make it work to fill in this information. Can someone help me out? Thanks.

 

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

That's a good start, but based on your description, it sounds like this may be closer.

function onChange(control, oldValue, newValue, isLoading) {
    if (newValue == "") {
         g_form.clearValue("phone_number_reserved");
    }

    var utilsLandlineAJAX = new GlideAjax("INV_ITSM_MobilePhoneCheckAjax");
    utilsLandlineAJAX.addParam("sysparm_name", "mobileDefaultPhoneNumber");
    utilsLandlineAJAX.addParam("sysparm_employee", newValue);
    utilsLandlineAJAX.getXMLAnswer(function(answer) {
        alert(answer)
        if (answer) {
            g_form.setValue("phone_number_reserved", answer);
        } else {
            g_form.clearValue("phone_number_reserved");
        }
    });
}

 

You can add alert lines in the Client Script, and gs.addInfoMessage lines in the Script Include temporarily to see how far the script is getting, the record retrieved by the GlideRecord, etc so you can see where it's going wrong.  On the Script Include, make sure the Client callable box is checked, then it sounds like the script should be more like this:

var INV_ITSM_MobilePhoneCheckAjax = Class.create();
INV_ITSM_MobilePhoneCheckAjax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    mobileDefaultPhoneNumber: function() {
        var answer = '';        
        var usr = this.getParameter("sysparm_employee");
        gs.addInfoMessage('usr = ' + usr)
        var gr = new GlideRecord('u_cmdb_ci_phone_number');
        gr.addQuery('assigned_to', usr);
        gr.addQuery('number_state', 'IN', 'reserved, assigned');
        gr.addQuery('type', 'mobile');
        gr.query();
        if (gr.next()) {
            gs.addInfoMessage('record found ' + gr.name)
            answer = gr.name;
        }
        return answer;
    },
    type: 'INV_ITSM_MobilePhoneCheckAjax'
});

 - depending on your field names and values in your custom table...

 

View solution in original post

5 REPLIES 5

Brad Bowman
Kilo Patron
Kilo Patron

When the Requested for variable changes, trigger an onChange Catalog Client Script that passes the newValue to a Script Include via GlideAjax.  The Script Include would query the custom table where assigned_to = the newValue passed in from the client, and your other criteria.  The name/number can then be returned to the client to set the value of the reserved phone number variable.   

I am not that good with Scripts but does this look okay? Or am I really going the wrong direction:

 

Client Script

function onChange(control, oldValue, newValue, isLoading) {

 

    if (newValue == "") {

        g_form.setValue("phone_number_reserved", "");

    }

 

    var utilsLandlineAJAX = new GlideAjax("INV_ITSM_MobilePhoneCheckAjax");

    utilsLandlineAJAX.addParam("sysparm_name", "mobileDefaultPhoneNumber");

    utilsLandlineAJAX.addParam("sysparm_employee", newValue);

    utilsLandlineAJAX.addParam("sysparm_type", "mobile");

    utilsLandlineAJAX.addParam("sysparm_number_state", "assigned" "reserved");

    utilsLandlineAJAX.getXMLAnswer(function(answer) {

        if (answer) {

            g_form.setValue("phone_number_reserved", answer);

        } else {

            g_form.setValue("phone_number_reserved", "");

        }

    });

}

 

Script Include:

var INV_ITSM_MobilePhoneCheckAjax = Class.create();

INV_ITSM_MobilePhoneCheckAjax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

 

    MobileNumberName: function() {

        var mobilenumber = this.getParameter("mobileDefaultPhoneNumber ");

        var grUser = new GlideRecord('sys_user');

        grUser.get("user_name", userName);

 

        var result = {   

           "phone_number_reserved": grUser.getDisplayValue('mobile_phone_number')

        };

        return JSON.stringify(result);

    },

    type: 'INV_ITSM_MobilePhoneCheckAjax'

});

 

Thanks!

Brad Bowman
Kilo Patron
Kilo Patron

That's a good start, but based on your description, it sounds like this may be closer.

function onChange(control, oldValue, newValue, isLoading) {
    if (newValue == "") {
         g_form.clearValue("phone_number_reserved");
    }

    var utilsLandlineAJAX = new GlideAjax("INV_ITSM_MobilePhoneCheckAjax");
    utilsLandlineAJAX.addParam("sysparm_name", "mobileDefaultPhoneNumber");
    utilsLandlineAJAX.addParam("sysparm_employee", newValue);
    utilsLandlineAJAX.getXMLAnswer(function(answer) {
        alert(answer)
        if (answer) {
            g_form.setValue("phone_number_reserved", answer);
        } else {
            g_form.clearValue("phone_number_reserved");
        }
    });
}

 

You can add alert lines in the Client Script, and gs.addInfoMessage lines in the Script Include temporarily to see how far the script is getting, the record retrieved by the GlideRecord, etc so you can see where it's going wrong.  On the Script Include, make sure the Client callable box is checked, then it sounds like the script should be more like this:

var INV_ITSM_MobilePhoneCheckAjax = Class.create();
INV_ITSM_MobilePhoneCheckAjax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    mobileDefaultPhoneNumber: function() {
        var answer = '';        
        var usr = this.getParameter("sysparm_employee");
        gs.addInfoMessage('usr = ' + usr)
        var gr = new GlideRecord('u_cmdb_ci_phone_number');
        gr.addQuery('assigned_to', usr);
        gr.addQuery('number_state', 'IN', 'reserved, assigned');
        gr.addQuery('type', 'mobile');
        gr.query();
        if (gr.next()) {
            gs.addInfoMessage('record found ' + gr.name)
            answer = gr.name;
        }
        return answer;
    },
    type: 'INV_ITSM_MobilePhoneCheckAjax'
});

 - depending on your field names and values in your custom table...

 

Thank you! Took me a bit more a while to figure some lines out but it is working now. Thank you for your help.