Client Script updating values onLoad

jmichael
Kilo Contributor

ClientScript is working but when reloading the form it will set to the original value and overwrite if changed after reloading. For Instance, Location is set by the Users data but if Location is updated and Request is saved, upon opening the Request again the Location will be set back to the User table value. Is there a way to ensure this doesn't happen, maybe like on save or submit then disregard the Client Script?

4 REPLIES 4

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Could you post your client script code? You may want to add and check and not change the field if it's already populated.


function onChange(control, oldValue, newValue, isLoading) {  


var id = g_form.getValue('u_recipient');


var user = new GlideRecord('sys_user');  


          user.addQuery('sys_id',id);  


          user.query();  


if ( user.next() ) {  


  g_form.setValue('location', user.location);  


}  


}



Just very simple, very straightforward.


function onChange(control, oldValue, newValue, isLoading) {


if(!isLoading){


var id = g_form.getValue('u_recipient');


var user = new GlideRecord('sys_user');


          user.addQuery('sys_id',id);


          user.query();


if ( user.next() ) {


  g_form.setValue('location', user.location);


}


}


}



Adding a !isLoading before should prevent from running on load (if thats what you are looking for..).


Steven Young
Tera Guru

that is because you have an onLoad script running.   So everytime it runs it will set the value to the script.


As Brad stated you need to update your script



if(fieldname == "") {


run your script here


}



That way if it's not blank, it wont run.