Auto Populate Field from Reference Field

riaz_mansuri
Kilo Guru

Hi,

I would like to Auto Populate a String Field from a Reference Field.

So I have a Client Script on Load which populates the users Group. I would Like to use this Group Field (which is a Reference) to populate a String Field.

Here is my current scrip which does not work:

find_real_file.png

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

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

          return;

    }

var svc= g_form.getValue("u_From");

if (svc == "UK Service Management") {

g_form.setValue("u_To_Desk", "UK");

}

}

The part of the Form that needs to be updated is the To Desk Field

find_real_file.png

So on the Form Loading it already populated the "Sent By" and "From"

Based on the "From" which is a reference it needs to Auto populate some writing in the "To Desk" string field.

Any ideas what I am doing wrong or how I can achieve this?

Thanks,

1 ACCEPTED SOLUTION

Brian Dailey1
Kilo Sage

Hi Riaz,



Two things:


  1. The field names will be all lower case (e.g.):     u_from
  2. The value you get for the reference field 'u_from' will be the sys_id of the selected option ("UK Service Management"), not the displayed label


So, try this instead:


        var svc = g_form.getValue('u_from');


        if(svc == (sys_id of your desired option)){


                  g_form.setValue('u_to_desk', "UK");


        }




If you want to easily see the value for your selected option of u_from, you can add a line for testing to your script after you've declared svc:



        g_form.addInfoMessage(svc);





See if that helps.



-Brian





Edit:   Also, if you are setting this up as an onChange script for the From field ('u_from' ), then you can just use the newValue parameter being passed to your script automatically instead of using g_form.getValue('u_from'):



        var svc = newValue;


View solution in original post

3 REPLIES 3

Brian Dailey1
Kilo Sage

Hi Riaz,



Two things:


  1. The field names will be all lower case (e.g.):     u_from
  2. The value you get for the reference field 'u_from' will be the sys_id of the selected option ("UK Service Management"), not the displayed label


So, try this instead:


        var svc = g_form.getValue('u_from');


        if(svc == (sys_id of your desired option)){


                  g_form.setValue('u_to_desk', "UK");


        }




If you want to easily see the value for your selected option of u_from, you can add a line for testing to your script after you've declared svc:



        g_form.addInfoMessage(svc);





See if that helps.



-Brian





Edit:   Also, if you are setting this up as an onChange script for the From field ('u_from' ), then you can just use the newValue parameter being passed to your script automatically instead of using g_form.getValue('u_from'):



        var svc = newValue;


Hi Brian,



Perfect! thank you very much I am going to Mark this as correct.



I changed to the sys id and works perfectly



Thank you very much



Riaz


Great, glad it was that easy.  




-Brian