Autofil Variable How to fill in a choice input using a script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2024 10:09 AM
I have an autofil variable where I want to use the Script attribute to populate my input choice field. However, pulling just the value in the script include is not working. Does anyone know what format I am supposed to be returning for an input choice field via the script include?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2024 10:18 AM
The format should be a JSON object with the choices listed as an array of objects where each object has label and value properties.
Script Include:
var GetChoices = Class.create();
GetChoices.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getChoices: function() {
var choices = [];
var choiceGR = new GlideRecord('your_choice_table');
choiceGR.query();
while (choiceGR.next()) {
var choice = {};
choice.label = choiceGR.getValue('label_field');
choice.value = choiceGR.getValue('value_field');
choices.push(choice);
}
return JSON.stringify(choices);
}
});
Client Script:
function onLoad() {
var ga = new GlideAjax('GetChoices');
ga.addParam('sysparm_name', 'getChoices');
ga.getXMLAnswer(function(response) {
var choices = JSON.parse(response);
var choiceField = g_form.getControl('choice_field');
// Clear existing choices
choiceField.options.length = 0;
// Add new choices
choices.forEach(function(choice) {
var opt = new Option(choice.label, choice.value);
choiceField.options.add(opt);
});
});
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2024 11:23 AM
I should've clarified. I am on the mobile agent app trying to autofil the choice input field.
I am unsure what object I am supposed to be returning in order for the autofil to work

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2024 10:20 AM
Is it a dropdown? Can you share the script you are using to add the choices?
Is it g_form.addOption?
Please mark this response as correct or helpful if it assisted you with your question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2024 10:25 AM
@zsquared If you are using a choice field then you need to use
addOption(String fieldName, String choiceValue, String choiceLabel) to fill the choice.
Here is the sample code.
g_form.addOption('priority', '6', '6 - Really Low');