Setting value of survey question via client script

Ahmad6
Giga Expert

Hi All,

I have a public survey which I am creating and I want the location of the user (being passed in the URL) to populate a location question in the survey. There are various articles online that mention setting the default value of the question to javascript:RP.getParameterValue("url-key")   but in both Helsinki and Istanbul that has never worked for me (I am currently on Istanbul, upgrading to Jakarta soon). I've even tried setting the default value to a string, which doesn't show up either.

I have made changes to the assessment_take2 UI page and am able to retrieve the value from the URL in the client script block, however I have no idea how to set the value of the field in the client script. I'm trying to use the question's name in a g_form.setvalue() but it's not working, does anyone know how to set that value?

This is the code that i've added to the addLoadEvent function:

var url = document.URL.parseQuery();

if (url['sysparm_assessable_type'] == 'e89064854f4943006a7266fe0310c760') { //public Survey sys_id

        if (url['location'] != '') {

                  alert('changing location from ' + g_form.getValue('Location') + " to " + url['location']);

                  g_form.setValue('Location', url['location']);

        } else {

                  g_form.setValue('Location', 'Unknown');

        }

}

The question is below, as you can see I've set the default value yet when i start a new audit the field is still blank. A fix to either the default value or the client script would be much appreciated.

find_real_file.png

6 REPLIES 6

Manjeet Singh
ServiceNow Employee
ServiceNow Employee

The possible solution on these thread might help:



Auto-Fill a field.


Service Now Auto populate fields Script?


Hi Manjeet,



The two examples you've mentioned both talk about things I've tried and didn't work. Setting default values on survey questions aren't working for me (I tried to set it to just a string instead of fancy javascript functions and even that didn't work), and I am unable to use g_form.setValue because I do not know what the field name is. I've called the field "location" but when I used it in the client script I could not find a field with that name.


khadija3
Tera Guru

Hello Ahmad,



Did you find any solution for this I'm having the issue ?



Thanks


Did you ever get this to work?  We have the same use case.