Issue with OnChange script to update select box variable base on another variable selection

Stephen Marks
Tera Expert

Hello Community hope you can help with this

 

I recently had to create a record producer form. On the record producer form I had two variables:

Reference variable

Lookup Select box variable 

 

Both variables are on the same table and the lookup select variable appears as a field in the variable that the reference variable points to.

 

The script I was trying to write was so when a user selects an option for reference variable it sets the value of the lookup select field automatically to be the same as what is in stored in that field on the product.

 

Ultimately I was able to do this by using the auto-populate tab on the lookup variable (setting the dependent question to what the reference value points to, setting the table to the one both variables reference/lookup from, and setting the dot walk path to value the lookup value field) 

 

I had tried to do this though with a script and I don't know what is wrong with it and why it doesn't work,

 

Attached/below is my onchange script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    
    //Type appropriate comment here, and begin script below
    var lookup = new GlideRecord('cmdb_type_for_reference_variable');
    lookup.addQuery('sys_id', g_form.getValue('reference_variable_name'));
    lookup.query();

    var temp;
        if (lookup.next()) {

        temp = lookup.selectbox_variable_name.sys_id;
        
        if (null != temp) {
            g_form.setValue('selectbox_variable_name', temp);
        } else {
            g_form.setValue('selectbox_variable_name'"");
        }
    } else {
        g_form.setValue('selectbox_variable_name'"");
    }
    
}

 

 

0 REPLIES 0