Auto-populating Record Producer Variables

Christine30
Tera Guru

Hello:

I have a record producer with two variables:

var_number: Reference to alm_hardware table.

var_state: String

When the user selects an alm_hardware record in the var_number field, I'd like 'state' from the same record to be displayed on the form, in the var_state field. I've tried setting the default value of the var_state variable, for example: g_form.setValue('var_number'), with no success. Thanks in advance for any help.

Christine

1 ACCEPTED SOLUTION

Make sure to delete that client script you made on the Record Producer table. You want to create a Catalog Client Script on your record producer.

View solution in original post

6 REPLIES 6

bammar
Kilo Sage
Kilo Sage

Setting this involves - g_form.setValue('var_number', "value" or variable your trying to set)

First you need to define a variable that fetches the Reference to the alm_hardware record.

Then you need to dot walk to the state field of that reference and define that- then you need to put that value the way I showed - if you get a variable you dont need "" in G-form setValue

Tiffany Royer
Giga Guru

You would need to do either a GlideAjax call or a getReference on an onchange script;

Here is an example of getReference 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        g_form.setValue('var_state', "");
        return;
    }

    var hardware = g_form.getReference('var_number', setFields);
}

function setFields(hardware) {
    g_form.setValue('var_state', hardware.state);



}

Christine30
Tera Guru

Thank you. What table would I add the onchange script to? I've tried sc_cat_item_producer, but that doesn't seem to be working, and I'm unsure what to select for the Field Name variable when I create the script.  Thank you.

 

find_real_file.png

For your situation, use a GlideAjax call instead. A getReference will get the state value not the state Display Value.

Here is what you need to do

1)Create a Script Include

2)Create a Catalog OnChange Script - on the record producer you created

Script Include

find_real_file.png

 

var getHardware = Class.create();
getHardware.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    gethardwareState: function() {
        var hardwareSyS = this.getParameter('sysparm_hardware');

        var hardware = new GlideRecord('alm_hardware');

        hardware.get(hardwareSyS);
		
        return hardware.getDisplayValue('install_status');
    },

    type: 'getHardware'
});

 

 

On Change Script find_real_file.png

 

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

    if (isLoading || newValue == '') {
        g_form.setValue("state", "");
        return;
    }


    var ga = new GlideAjax('global.getHardware');
    ga.addParam('sysparm_name', 'gethardwareState');
    ga.addParam('sysparm_hardware', newValue);
    ga.getXML(setFields);

}


function setFields(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    g_form.setValue("state", answer);


}

 

Example of the Record Producer and Variables

find_real_file.png

find_real_file.png