Difference in passing "current.field_name" and "g_form.getValue('field_name')"

Max Nowak
Kilo Sage

Hi,

So, I wanted to write a script include that could be used as reference qualifier for a field, as well as being used in client scripts to populate other fields based on the field's value. Essentially, I have the two fields "Contract" and "Service Offering", and I want the form to only show contract records during lookup that are connected to the service offering (via the service_offering.contract field) and vice versa. I ended up with something like this in the backend:

Note: The variables "account" and "service offering" are passed to the script via the reference qualifier, which reads something like this:

javascript: "sys_idIN" + new script_include.getContracts(current.account, current.service_offering); 
getContracts: function(account,service_offering) {
    var acc = account || this.getParameter('sysparm_account'),
        svo = service_offering || this.getParameter('sysparm_service_offering'),

    var cntGr = new GlideRecord('ast_contract'),

    if(!gs.nil(svo)) {
        gs.info('Service Offering is: ' + svo);
        gs.info('Service Offering name is: ' + svo.name);
        cntGr.addQuery('sys_id', svo.contract.sys_id);
    }
[...]

The script then continues with more query conditions and ultimately returning an array of sys_ids, but the main point here is that I have a problem with dot-walking when the script is called from the client-side.

 

When I wrote this, I initially thought it wouldn't work, since I assumed that "current.service_offering" would only pass a sys_id value, which would mean that dot-walking trying to print "service_offering.name" would fail. Interestingly, it doesn't, which really surprised me - especially since the script debugger just shows the sys_id as value for the "svo" object.

So, the problem arises if I'm calling this script from a client script via GlideAjax, and passing "sysparm_service_offering" with "g_form.getValue(service_offering)" as value, because THIS, it seems, is indeed only passing a sys_id.

Can anyone explain to me why "current.service_offering" allows dot-walking, while passing "g_form.getValue(service_offering)" doesn't? Also, is there a quick fix to this issue, or do I have to use a gliderecord query every time I want to dot-walk in a script include which gets called (and passed variables) via GlideAjax?

Thanks in advance,

Max

1 ACCEPTED SOLUTION

Dan Ellis
Kilo Sage

When you access a field by dot notation (current.field_name or g_form.field_name) you are getting a reference to the value which for reference fields means it is returning you the object (record) that it is referencing.

When using getValue you are getting the string value of the field which for a reference field is the sys_id of the referenced record.

In your Script Include you could perform a GlideRecord query using the sys_id to get the record you need and then you have access to any value on that record that you need.

Hope this helps!

Thanks,
Dan

View solution in original post

2 REPLIES 2

Dan Ellis
Kilo Sage

When you access a field by dot notation (current.field_name or g_form.field_name) you are getting a reference to the value which for reference fields means it is returning you the object (record) that it is referencing.

When using getValue you are getting the string value of the field which for a reference field is the sys_id of the referenced record.

In your Script Include you could perform a GlideRecord query using the sys_id to get the record you need and then you have access to any value on that record that you need.

Hope this helps!

Thanks,
Dan

I guess there won't be a way around querying a GlideRecord then. Thanks for your help!