Setting value of survey question via client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2017 04:55 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2020 01:40 AM
Hi All,
Could you please provide if any solution found for this? Thanks in advance
Kind regards,
Pavan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2020 02:01 AM
Forgive my poor formatting but I'm replying from mobile, I believe this worked however you will need to make small modifications depending on the structure of your survey. We didn't end up using this by the way, we just used another survey site and fed feedback into ServiceNow via api
var url = document.URL.parseQuery();
if (url['sysparm_assessable_type'] == 'e89064854f4943006a7255fe0310c760') { //QR code Survey
if (url['location'] != '') {
document.getElementsByClassName("form-control")[0].value = url['location'];
}
}
The index 0 above is because that was the first question of our survey, you'll need to change that if your question is further down. I'll check once I'm at a computer to see if that is the right code but if you want to try and let me know if it works for you I'll mark it as correct for anyone else in the future.