Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Autofil Variable How to fill in a choice input using a script?

zsquared
Tera Contributor

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?

5 REPLIES 5

Not applicable

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);
});
});
}

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

SanjivMeher
Mega Patron

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.

Sandeep Rajput
Tera Patron

@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');