getDisplayValue returning value from wrong field

___miked___
Mega Contributor

I have multiple fields on a form.   I'm trying to get the value of a choice-list field called 'patient_category'.   But, the following code always returns the value of another field on the form -- the patient_name field.   regardless of what field I specify as the getDisplayValue parameter, the system always returns the value of the patient_name field. I'm totally new to scripting in ServiceNow so am wondering what I am missing.    

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

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

          return;

    }  

alert(g_form.getDisplayValue('patient_category'));

    }

Thanks in advance for your help.

1 ACCEPTED SOLUTION

Rahul RJ
Giga Sage
Giga Sage

Hi Mike,



Can you try this code


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


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


          return;


    }


        alert(g_form.getDisplayBox('patient_category').value);



    }



P.S: Hit Like or Correct depending on the impact of response


View solution in original post

16 REPLIES 16

Thanks for the reply.   Unfortunately, the suggestions didn't solve the problem.


You can try this client script by changing <choicefield> to the name of your choice field.


This client script returns get all choice list values. Please modify accordingly.





  1. var values = [];  
  2. var sel = g_form.getElement('<choicefield>');  
  3. for (var i=0, n=sel.options.length;i<n;i++) {  
  4.   if (sel.options[i].value) values.push(sel.options[i].value);  
  5. }  
  6. alert(values.join(","));  

shloke04
Kilo Patron

As rightly mentioned by explorenow getDisplayValue() is a server side function and cannot be used in Client side. If you need to fetch the value of choice labels of a field better way would be to use a server side script and call the same on a client side.



For example : 1) In your scenario, write a Display   Business rule on your target table with a scratchpad variable as mentioned below:



g_scratchpad.val = current.patient_category.getDisplayValue();



2) After step 1, write an on Load/On Change Client script and call the above scratchpad variable as mentioned below:



function onLoad() {


//Type appropriate comment here, and begin script below



alert(g_scratchpad.val);


}



This should work for you.



Cheers,


Shloke


Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Thank you for the reply.   Really appreciate the additional information.


amlesh
Kilo Sage

Try using this.



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


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


          return;


    }


alert(g_form.getOption('u_patient_category', g_form.getValue('u_patient_category')).text);


    }