onChange catalog client script to populate a field from a reference field ?

yshah
Giga Contributor

As shown in the figure of a catalog item, I have a reference field named 'Disk Name' and when a value is selected the field 'Current Disk Size' should be populated

find_real_file.png

I have the written the following script, but the pop up box is not closing once I select the value of the reference field and is giving me error 'getReference for disk_name not allowed: missing callback function as parameter'.   I am not sure how to make a callback function and pass it as a parameter. If that is needed then in what part of the script I should add it.

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

  if (isLoading || newValue == '') {

          return;

    }

    //Type appropriate comment here, and begin script below            

                              var disk_name = g_form.getReference('disk_name');

                                                              var disk_size;

                                                              var disk_rec = new GlideRecord('x_aona_gcp_gcp_persistent_disks');

                                                              disk_rec.addQuery('sys_id',disk_name);

                                                              disk_rec.query();

                                                              while(disk_rec.next()){

                                                                                              //gs.info('inside while');

                                                                                              disk_size=disk_rec.disk_space;

                                                              }

                                                              g_form.setValue('c_disk_size',disk_size);

}

1 ACCEPTED SOLUTION

yshah
Giga Contributor

Yes, it is a scopped app. But the issue was that we are querying on a record, which is wrong. The getReference method return us a complete record, so we just had to point to the right attribute.



The code here is as shown below.



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


  if (isLoading || newValue == '') {


  return;


  }


  var disk_name = g_form.getReference('disk_name',fillRec);


}


function fillRec(response){


  var size = response.disk_space;


  g_form.setValue('c_disk_size',size);


}


And this will solve the issue.


View solution in original post

15 REPLIES 15

DrewW
Mega Sage

If memory serves its simply



var disk_name = g_form.getReference('disk_name', myFunction);


function myFunction(response){


//Do your work here.


}



Even better here is the docs


https://developer.servicenow.com/app.do#!/api_doc?v=helsinki&id=r_GlideFormGetReference_String_Funct...


yshah
Giga Contributor

Hi,


I tried to implement your solution, but I am not able to query in the callback function.



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


  if (isLoading || newValue == '') {


  return;


  }


  var disk_name = g_form.getReference('disk_name',fillRec);


}


function fillRec(response){


  var id = response.sys_id;


  var disk_size;


  var disk_rec = new GlideRecord('x_aona_gcp_gcp_persistent_disks');


  disk_rec.addQuery('sys_id',id);


  disk_rec.query();


  if(disk_rec.next()){


  //gs.info('inside while');


  disk_size=disk_rec.disk_space;


  }


  alert(disk_size);


  g_form.setValue('c_disk_size',disk_size);


}



I am able to get the id, but the GlideRecord and the g_form.setValue() is not working. Can you provide me where I am going wrong or any necessary changes I need to make.



Thanks


So the alert of "disk_size" has no value or the "g_form.setValue('c_disk_size',disk_size);" is not setting the value on the form?



If its the latter try


g_form.setValue('variables.c_disk_size', disk_size);




yshah
Giga Contributor

It does not reach to that point. It does not fetch the record using the GlideRecord.


We were passing alerts to check till where the code is running, and it turns out that just before GlideRecord it was working and after that no alert was popping.


Not sure if that helps, but this is what is happening.



Thanks,


Yash