Andris_S
Andris_S

Reputation: 1

Asset field problem with onChange client script

I'm working on client script which reads data from alm_hardware table and reads also value from field u_invoice_date and that value is displayed in form field called invoice_date.

This script works fine, but I found one problem: when u_invoice_date field is not filled in asset by asset manager in form for specific devices, the output is blank. Working script bellow:

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

    var reqVarHelper = new GlideAjax('x_swea2_wies_servi.WPCatalogItemClientHelpers');
    reqVarHelper.addParam('sysparm_name', 'getRecord');
    reqVarHelper.addParam("sysparm_table", "alm_hardware");
    reqVarHelper.addParam("sysparm_query", "sys_id=" + g_form.getValue("select_device"));
    reqVarHelper.addParam("sysparm_fields", "name,u_invoice_date");
    reqVarHelper.getXML(function(response) {
        var result = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
        g_form.setValue("invoice_date", result.u_invoice_date);
    });
}

Then I found table fm_expense_line and field date which is always field, therefore there is chance the form will have some value there always.

I started to modify legacy script with replacing table and field in script but it does not work, do you see any issues in bellow script?

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

    var reqVarHelper = new GlideAjax('x_swea2_wies_servi.WPCatalogItemClientHelpers');
    reqVarHelper.addParam('sysparm_name', 'getRecord');
    reqVarHelper.addParam("sysparm_table", "fm_expense_line");
    reqVarHelper.addParam("sysparm_query", "sys_id=" + g_form.getValue("select_device"));
    reqVarHelper.addParam("sysparm_fields", "name,date");
    reqVarHelper.getXML(function(response) {
        var result = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
        g_form.setValue("invoice_date", result.date);
    });
}

Checked in browser console, there where no errors related to this topic. Any ideas, why it does not work in new script?

I have tried to remove JSON part from script and replacing it with console.log but could be I have made mistake there as well, therefore reverted back to version which is added in post.

Upvotes: 0

Views: 768

Answers (1)

kerry
kerry

Reputation: 767

This is a quirk of GlideRecord - when a value is null, it doesn't play nice with any hard-typed field (expecting a non-null value such as string or number). When you give a null to GlideForm.setValue(), the function sees the null and says "whelp, I guess there's no change, so won't do anything!"

There are a couple of ways to handle this by handling the null result from result.date (I'll list them in my order of preference).

  1. Use a logical OR operator to substitute for any non-truthy value (beware, this substitutes for null, undefined, false, and 0
result.date || ''
  1. Use an implicit cast to force a string (in JS, casting is done based on the type of the initial object)
'' + result.date
  1. If these aren't working (which I've seen, but am unable to provide a reliable reason for), try using the .getValue() function in combination with one of the previous:
result..getValue('date') || ''
'' + result.getValue('date')
  1. I only mention this last method because a co-worker uses it EVERY TIME FOR EVERY VALUE EVERY WHERE. I think it's brutish and ugly, but they swear by it. Why it works, I have no idea - it breaks the coalesce rules in JS, so it must be more of the "Now Magic" that drives me so f'n crazy
result.date + ''

There's also my favorite, nullish-coalesce, but I don't believe it's supported in ES6 used by UI/Next so isn't a part of a ServiceNow discussion.

Upvotes: 0

Related Questions