Promote Dropdown dinamically

Hello, I'm trying to fill a dropdown by code.
Let me explain, I have a report, which when selected, shows me a screen where the user can select the date range and I would like to put a dropdown with the list of departments of a method (table).
Well, there's no way, I've looked at examples here, in google, and in... chatgpt. But it always fails me when executing the code, it always gives me the error:

Event handler failed with message: TypeError: Cannot read properties of null(reading 'appendChild')[function onload$user$handler(e) {
    try {
        if (onload1(e) === false) return false;
    } catch (exp) {
        aras.AlertError(aras.getResource('', 'ui_methods_ex.event_handler_failed'), aras.getResource('', 'ui_methods_ex.event_handler_failure_msg', exp.description ? exp.description : (exp + ' [' + arguments.callee.toString() + ']')), aras.getResource('', 'common.client_side_err'));
        return false;
    }
}]

This is my code, one of them, I have tried many ways.

// Obtén una referencia al ListBox
var listBox = document.getElementById("Departamentos");

// Crea un nuevo elemento de opción
var option = document.createElement("option");

// Establece el valor y el texto del elemento de opción
option.value = "valor1";
option.textContent = "Opción 1";

// Agrega el elemento de opción al ListBox
listBox.appendChild(option);

As I see, or I think, document.getElementById doesn't take well, I'm failing in something. I pu an a alert(listBox); and return NULL.

This is the structure of where the field is:

                          

All the code is executed when entering the form, in the event "onLoad"

I have seen examples in the forum with the use of "getFieldComponentByName", but here in the company we have Version 11.0 SP9 Build: 6549 installed and it doesn't work. Examples from  and Christoper Gillis.

Too many thanks!!!

Parents
  • Well, the way to work with dropdowns changed around 3 times over the years. There is a difference between 11SP9, 11SP11 and 11SP15 :). 

    General tip: Always add a comment sections on the top of your code that indicates which version it was build for and add some descriptions.

    This way I found this Innovator 11SP9 code that filled a dropdown in Form based on a List item:

    var inn = new Innovator();

    // Get dropdown element from Form
    var div_startpage = getFieldByName("mydropdown").id;
    var control_items = div_startpage.substring(0, div_startpage.indexOf("span"));
    var select = document.getElementById(control_items);

    // Get  list values
    var listValues = [];
    listValues = aras.getListValues("BFDEFE0CEE174B...."); // id of List "xy"
    for (var i = 0; i < listValues.length; i++) {
    var value = aras.getItemProperty(listValues[i], "value");
    var label = aras.getItemProperty(listValues[i], "label");
    var option = document.createElement('option');

    select.options[i] = new Option(label, value); // fill dropdown list
    }

    No Idea if it works :D

Reply
  • Well, the way to work with dropdowns changed around 3 times over the years. There is a difference between 11SP9, 11SP11 and 11SP15 :). 

    General tip: Always add a comment sections on the top of your code that indicates which version it was build for and add some descriptions.

    This way I found this Innovator 11SP9 code that filled a dropdown in Form based on a List item:

    var inn = new Innovator();

    // Get dropdown element from Form
    var div_startpage = getFieldByName("mydropdown").id;
    var control_items = div_startpage.substring(0, div_startpage.indexOf("span"));
    var select = document.getElementById(control_items);

    // Get  list values
    var listValues = [];
    listValues = aras.getListValues("BFDEFE0CEE174B...."); // id of List "xy"
    for (var i = 0; i < listValues.length; i++) {
    var value = aras.getItemProperty(listValues[i], "value");
    var label = aras.getItemProperty(listValues[i], "label");
    var option = document.createElement('option');

    select.options[i] = new Option(label, value); // fill dropdown list
    }

    No Idea if it works :D

Children
  • 0 オフライン in reply to AngelaIp

    Thank you very much Angela. It works perfectly. "Eres un Sol" (Spanish expression, hehe)!!!

    I did not answer you before because in Andalusia, Spain, we have had 2 days of vacation, "Dia de Andalucia".

    The code works perfectly, I only have to change where I would collect the data, since I take it from a table and not from a LIST of ARAS, but I already know how to do it. I did it, it works perfectly.

    It gives me courage, because I have looked everywhere, google, forum, PDF and I did not find anywhere information on how to do it in the version that I have. All the examples that I saw and tried gave me an error and were examples that the forum partners marked as valid.

    What's more, I have tried to search for the code that you have put in the PDFs and still nothing appears. I don't know.

    Thank you so much. Eternally grateful!!!

  • Glad to hear it worked! You never have to justify if it needs a bit more time to answer. Especially when you have good reasons :D. Happy "Dia de Andalucia" from the "Free State of Bavaria" !

    There is another source of code you may haven´t found yet: Github! --> https://github.com/ArasLabs

    In your case you probably often have to look at previous releases of the projects, cause you´re still using I11.
    But lot´s of the projects are based on I11.

    I actually used a dropdown in one own projects -> https://github.com/AngelaIp/cui-select-starting-page . 
    Sometimes just adding "github" to an aras specific question in google helps.

    But when you have a subscription, you can always ask Aras support for these kind of stuff!