Aidan Lovegrov1
Tera Contributor

Hi everyone, 

Document ID fields can be buggy to update dynamically when using client scripts. The normal way of updating values client-side using the functions

g_form.setValue(field, value)
g_form.setValue(field, value, displayValue)

don't act as expected when using Document ID fields - the display value and preview are not updated. ServiceNow HI advised that this is expected behaviour (??? doesn't sound right to me compared to reference fields).

I have a work-around solution. Manually updating the display value and preview does involve minor HTML element work so be advised it should be your last option if no OOTB solution exists as HI will not provide support on this topic. I have extracted the different variables for easy reading.

var currentTable = "u_project_task";
var field = "u_validator";
var refTable = {name:"sys_user", label:"User"};
var refValue = {sys:"a712c282dbfdbfc0c80f8c150596197b", label:"Aidan Lovegrove"};

//Set the value behind the field (and is what is saved when submitting the record)
g_form.setValue(field, refValue.sys);
//Display value
g_form.getElement('sys_display.'+currentTable+'.'+field).value = refTable.label+": "+refValue.label;
//Preview button
var prev = document.getElementById('view.'+currentTable+'.'+field);
prev.setAttribute('data-sysid',refValue.sys);
prev.setAttribute('data-form',refTable.name+'.do');
prev.setAttribute('data-table',refTable.name);

As client scripts are isolated by default, you need to also set the Isolate Script field to false in order for the HTML manipulation to run (you may also have to add the field to the form if its not already there) - this community post explains the concept of isolated scripts should you be interested.

Comments
Sarath Chandra1
Giga Contributor

Thanks for the article @Aidan Lovegrove. Just made slight modifications to meet our requirement and this worked. 

 

This is really helpful! 

Version history
Last update:
‎04-28-2020 03:34 AM
Updated by: