Try ERPNext Try Frappe Cloud Buy Support Partners Foundation

How to change options in a select field automatically

Hi,

I have two custom fields as supplier_quoted_currency which is a float data type and priced which is a select data type in Quotation doctype. After submitting the document if anyone changes the supplier_quoted_currency field i want priced field to be changed from Yes to No.

I have used below custom script but not getting expected output,

frappe.ui.form.on(‘Quotation’, {
supplier_quoted_currency : function(frm) {
if(frm.doc.priced == “No”){
frm.set_df_property(“priced”, “options”, “Yes”);
}frm.refresh_field(“priced”);}
});

First the data is like below,

Once its changed, it is showing yes but we have to select it, is there any way to show it automatically without selecting,

Thanks in advance!

@Jenisha Try this code:

frappe.ui.form.on(‘Quotation’, {
supplier_quoted_currency : function(frm) {
if(frm.doc.priced == “No”){
   frm.set_df_property(“priced”, “options”, “Yes”);
   frm.set_value("priced","Yes");
}
frm.refresh_field(“priced”);}
});

Thank you so much, its working! :slightly_smiling_face:
I need to add this code to child table, can you guide me for the same.

I have tried the below code for setting property in child table but its not working,

refresh: function (frm, cdt, cdn) {
frm.fields_dict.items.grid.get_field(‘supplier_quoted_currency’).grid.set_df_property = function (doc, cdt, cdn) {
if(frm.doc.priced == “Yes”){
frm.set_df_property(“priced”, “options”, “No”);
frm.set_value(“priced”,“No”);
}
};
}

I have tried the below script for child table but its not working,

refresh: function (frm, cdt, cdn) {
frm.fields_dict.items.grid.get_field(‘supplier_quoted_currency’).grid.get_query = function (doc, cdt, cdn) {
if(frm.doc.priced == “Yes”){
frm.set_df_property(“priced”, “options”, “No”);
frm.set_value(“priced”,“No”);
}
frm.refresh_field(“priced”);
};
}

@Jenisha could you explain more of what you are trying to do to help you.

please take a screenshot of child table and triggered field

I have the same fields in Quotation Item which is a child table. I need the same condition for child table, i am not sure how to give frm.set_df_property in child table,

You have to use :

frappe.meta.get_docfield('Quotation Items', 'priced', frm.doc.name).options = ["No"];
frm.refresh_field('items');

Thanks a lot for the info, i tried with below code, the priced field is not changing :sweat:

frappe.ui.form.on(‘Quotation’, {
supplier_quoted_currency: function (frm, cdt, cdn) {
frappe.meta.get_docfield(‘Quotation Item’, ‘priced’, frm.doc.name).options = [“No”];
frm.refresh_field(‘items’);
}
});

because you don’t set the value for a child field element

    frappe.ui.form.on(‘Quotation’, {
    supplier_quoted_currency: function (frm, cdt, cdn) {
    frappe.meta.get_docfield(‘Quotation Item’, ‘priced’, frm.doc.name).options = [“No”];
    frappe.model.set_value(cdt, cdn, "priced", "No");
    frm.refresh_field(‘items’);
    }
    });

Its working thanks a lot for the guidance and help! :slightly_smiling_face:

frappe.ui.form.on(‘Quotation Item’, {
supplier_quoted_currency: function (frm, cdt, cdn) {
frappe.meta.get_docfield(‘Quotation Item’, ‘priced’, frm.doc.name).options = [“No”];
frappe.model.set_value(cdt, cdn, “priced”, “No”);
frm.refresh_field(‘items’);
}
});

For future reference:
being stuck in the same problem, the upper function was updating meta in memory but wasn’t rendering it to dom on refresh_field event, after reading the updated code I got the solution

upper solution worked fine in version 12, but in version 13 it worked like this:

frappe.meta.get_docfield(“doctype”, “field_name”).options = ["", “option”]
frm.refresh_field(“child_table_field_name”);

for now, I don’t know why this function prototype has a serious effect on refresh function(?), but it may save someone’s time

1 Like

Hey

I have a child table (Opportunity Item) with name ‘items’ inside Opportunity

I am using a script to load prices inside child table for (fieldname=‘prices’, type=‘select’)

let load_prices = function(frm, cdt, cdn) {
var child = locals[cdt][cdn];

frappe.call({
	method: 'frappe.client.get_list',
		args: {
			'doctype': 'Item Price',
			'filters': {'item_code': child.item_code},
			'fields': ['currency', 'price_list_rate'],
	},
	callback: function(data) {
		var prices = [];
		var money = new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'RUB' });
		
		(data.message || []).forEach(function(row) {
			prices.push({
				'value': row.price_list_rate, 
				//'label': row.price_list_rate + " " + row.currency
				'label': money.format(row.price_list_rate)
			});
		});
		
		frappe.meta.get_docfield('Opportunity Item', 'prices', cur_frm.doc.name).options = prices;
		frm.refresh_field('items');
	}
});

};

Prices load in meta, but DOM not refresh
frappe.meta.get_docfield(‘Opportunity Item’, ‘prices’, cur_frm.doc.name).options = prices;
frm.refresh_field(‘items’);

Opportunity Item - doctype child table
prices - select field
items - name child table

this worked in version 12
the solution seems to be the same as yours
but doesn’t work on
ERPNext: v13.0.1 (version-13)
Frappe Framework: v13.0.2 (version-13)