Expense Claim Details Custom Script

Hi,

I want to make a field mandatory on the basis of a criteria like when a user selects an expense type as Conveyance then the vehicle_type field should become mandatory, below is my code but it is not working at all, can anyone help me in this regard?

frappe.ui.form.on(“Expense Claim Detail”,“expense_type”, function(frm, cdt, cdn) {
var d =locals[cdt][cdn]
if(d.expense_type == “Conveyance”){
frappe.model.set_df_property(“vehicle_type”,“reqd”,true);
}
});

I also tried this:

frappe.ui.form.on(“Expense Claim Detail”,“expense_type”, function(frm, cdt, cdn) {
var d =locals[cdt][cdn]
if(d.expense_type == “Conveyance”){
cur_frm.set_df_property(“vehicle_type”,“reqd”,true);
}
});

and this:

frappe.ui.form.on(“Expense Claim Detail”,“expense_type”, function(frm, cdt, cdn) {
var d =locals[cdt][cdn]
if(d.expense_type == “Conveyance”){
cur_frm.set_df_property(“vehicle_type”,d.name,“reqd”,true);
}
});

But nothing works, I appreciate if anyone can help me.

Thanks & Regards
Ruchin Sharma

@ruchin78 please format your code. It is not readable

https://meta.discourse.org/t/syntax-highlighting-of-code-blocks/7242

1 Like

@rmehta now can you look at this code and help me.

@ruchin78 you need not paste an image just wrap your quote in three ` characters

I assume this is in child table.

// if the table field name is "expenses"
frm.get_docfield("vehicle_type", "expenses").reqd = true;
refresh_field("expenses")

I tried this on Expense Claim Doctype

frappe.ui.form.on("Expense Claim","expense_type", function(frm, cdt, cdn) {
var d =locals[cdt][cdn]
if(d.expense_type=="Conveyance")
{
frm.get_docfield("vehicle_type","expenses").reqd = true;
refresh_field("expenses")
}
});

and this on Expense Claim Doctype

frappe.ui.form.on(```"Expense Claim Detail"```,```"expense_type"```, function(frm, cdt, cdn) {
var d =locals[cdt][cdn]
if(d.expense_type==```"Conveyance"```)
{
frm.get_docfield(```"vehicle_type"```,```"expenses"```).reqd = true;
refresh_field(```"expenses"```)
}
});

but didn’t work and no error

@ruchin78 you need to format the whole block. See my edit.

This time I tried it with more sophisticated manner. I admit that I didn’t do it well will try my best next time

I tried this on Expense Claim Doctype

frappe.ui.form.on("Expense Claim","expense_type", function(frm, cdt, cdn) {
var d =locals[cdt][cdn]
if(d.expense_type=="Conveyance")
{
frm.get_docfield("vehicle_type","expenses").reqd = true;
refresh_field("expenses")
}
});

and this on Expense Claim Doctype

frappe.ui.form.on("Expense Claim Detail","expense_type", function(frm, cdt, cdn) {
var d =locals[cdt][cdn]
if(d.expense_type=="Conveyance")
{
frm.get_docfield(```"vehicle_type","expenses").reqd = true;
refresh_field("expenses")
}
});

I think it is fine now.
:stuck_out_tongue_closed_eyes:

sorry it should be parent_fieldname, fieldname

frm.get_docfield("expenses", "vehicle_type")

@rmehta
Yes, it is done now, thanks lot.

Hi @rmehta
It worked but it made the field mandatory for entire table not for that particular row.