Dynamic Selection Box

Hi guys,
Some help on how to populate a selection box dynamically? Is there a similar case already so I can view the code?

1 Like

@vivek I appreciate if you can explain the use case.

@ruchin78
I need to pull “rates” received on an item from multiple supplier quotations and populate in a form that is used to calculate selling price.

So the user would open this form and open up the “Cost” Selection Box to see it populated with different rates received for the specific item. He would simply select the best price he wants to work with and continue with constructing the selling price.

@vivek

frappe.ui.form.on('Sales Order', 'onload', function(frm){
    frappe.call({
         method: 'frappe.client.get_values'
         args: {
             'doctype': 'NFe Parameters', // DocType that is the source of the options
             'fields': ['key', 'value'], // Fields that will provide the options (one for value, another for label, or use a single one for both)
             'filters': [['NFe Parameters', 'group', '=', 'IPI CST']] // filter conditions while fetching a list of values
         },
         callback: function(res){  // receive the response from the server
             var options = [];
             (res.message || []).forEach(function(row){   // start a loop over all options in the response, or in a empty list; 
               options.push({'value': row.key, 'label': row.value}) // makes a option entry 'value' and 'label'
             });
             frm.fields_dict.ipi_cst.options = options; // define de options for the field in this case (ipi_cst)
         }
    });
});

@ruchin78

3 Likes

@max_morais_dmm will try it and get back thanks a ton

Hi Max,

I appreciate if you can put some comments on the code shared by you so that it will be easy to understand by a layman too like
key,value
ipi_cst
NFe Parameters
etc.

@max_morais_dmm
This is awesome!
Thanks a lot.

@vivek would love to see you answer some questions on the forum too :smile:

2 Likes

@rmehta for sure. I am a bit reluctant cause i still feel I need to understand more. But whatever I know for sure, will help around.

1 Like

@max_morais_dmm thanks for this!

How would you update selection box options in a child table? Assuming the following (trying to fetch list of options for alternative/equivalent items). The list returns properly from the custom function I created but I can’t figure out how to get it to write to options.

I can make a new thread if needed but figured this could be somewhat helpful to the initial use case…

frappe.ui.form.on("Sales Order Item", "item_code", function(frm, cdt, cdn){
    var child = locals[cdt][cdn];
    frappe.call({
        method: "erpnext.selling.custom_methods.get_alternative_item_options",
        args: { "item_code": child.item_code,
     },
        callback: function(res){  
            var options = [];
                (res.message || []).forEach(function(row){    
                       options.push({"item_code": row.item_code}) // just one entry for now...
                     })
              // not sure what to put here 
        }
    })
});
1 Like

@alec_ruizramon1

The unique difference in this context is, you can get the meta-info of the field so you need put

frappe.meta.get_docfield('{{DocType}}', '{{fieldname}}').options = options;

eg:

frappe.meta.get_docfield('Sales Order Item', 'category').options = options;
1 Like

@vivek its just a matter of putting 10-15 minutes a day. I am sure you maybe be able to help at least a couple of users. Maybe learn a few more things too!

@rmehta will do :smile: