Want to retain inactive choice values for older records in list and form view - Shows empty

Imran1
Giga Guru

Hi All,

I want to retain inactive choice values both in form & list views for all historical records. I see them empty/null once they are inactive. Earlier atleast in list view I could see them in Blue not sure post Washington release there could be some changes.

 

I am employing two scripts to achieve this objective.

Dependent Field for the Client shows in alert but doesn't set in the actual field (automation) and it is also becoming empty. 

I am able to set the value of the client field when a form view is open or loaded of old records but dependent field shows empty ( it is not being set) another observation that I see it Automation name is set first and then gets removed and client name is set.

 

and even in the list view I was not seeing any values now I see it not sure how this behavior is changing 

 

Business Rule:

(function executeRule(current, previous /*null when async*/ ) {

    var selectedInactiveChoice = null;
    var dependentFieldValue = current.u_automation_name;
    var dependentValue = null;

    var gr = new GlideRecord('sys_choice');
    gr.addQuery('inactive', true);
    gr.addQuery('name', current.getTableName());
    gr.addQuery('element', 'u_client');
    gr.addQuery('dependent_value', current.u_contract);
    gr.addQuery('value', current.u_client); // Only fetch the selected inactive choice
    gr.query();
    if (gr.next()) {
        selectedInactiveChoice = {
            value: gr.value.toString(),
            label: gr.label.toString()
        };
    }
 // This I added recently because of Dependent field is not setting the value though it shows in alert
    var grD = new GlideRecord('sys_choice');
    grD.addQuery('name', current.getTableName());
    grD.addQuery('element', 'u_automation_name');
    grD.addQuery('dependent_value', current.u_client);
    grD.addQuery('value', dependentFieldValue);
    grD.query();
    if (grD.next()) {
        dependentValue = grD.label;
    }


    g_scratchpad.selectedInactiveChoice = selectedInactiveChoice;
    g_scratchpad.dependentFieldValue = dependentValue;


})(current, previous);

 

Client Script:

function onLoad() {

    if (g_scratchpad.selectedInactiveChoice) {
        var choice = g_scratchpad.selectedInactiveChoice;
        if (choice) {
            g_form.addOption('u_client', choice.value, choice.label);
            g_form.setValue('u_client', choice.value);
        }
    }
    if (g_scratchpad.dependentFieldValue) {
		alert(g_scratchpad.dependentFieldValue);
        g_form.setValue('u_automation_name', g_scratchpad.dependentFieldValue);
    }

}

Appreciate your assistance.

 

Thank You in advance

Regards,

Imran

https://www.servicenow.com/community/developer-forum/want-to-retain-inactive-choice-values-for-older...

 

 

 

 

 

 

1 ACCEPTED SOLUTION

Imran1
Giga Guru

I tried to achieve the dependent field value for the automation by deploying the below onChange script for the field type automation and it worked for me

Thank You Akash

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (newValue === oldValue) {
        return;
    }
    if (!g_form.isNewRecord() && g_scratchpad.selectedInactiveChoice) {
        g_form.setValue('u_automation_name', oldValue);
    }
}

View solution in original post

1 REPLY 1

Imran1
Giga Guru

I tried to achieve the dependent field value for the automation by deploying the below onChange script for the field type automation and it worked for me

Thank You Akash

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (newValue === oldValue) {
        return;
    }
    if (!g_form.isNewRecord() && g_scratchpad.selectedInactiveChoice) {
        g_form.setValue('u_automation_name', oldValue);
    }
}