How to restrict supplier field on Purchase Order screen to certain values based on filter

Hi There,

We are trying to restrict values in Supplier Link field on Purchase Order page to certain values based on a filter using below custom script but it doesn’t seems to work. Please help.

frappe.ui.form.on(“Purchase Order”, “onload”, function(frm) {
cur_frm.set_query(“supplier”, function() {
return {
“filters”: {
“currency_pay_in”: “USD”
}
};
});
});

We have defined a custom field for supplier called company. Once above script is working, we want to replace "currency_pay_in with company field and change “USD” to “cur_frm.doc.company”.

Please note we also have following onload custom script in addition to above for purchase Order doctype.

frappe.ui.form.on(“Purchase Order”, “onload”, function(frm) {
cur_frm.set_query(“expense_account”,“items”, function() {
return {
“filters”: {
“company”: cur_frm.doc.company
}
};
});
});

frappe.ui.form.on(“Purchase Order”, “onload”, function(frm) {
cur_frm.set_query(“cost_center”,“items”, function() {
return {
“filters”: {
“company”: cur_frm.doc.company
}
};
});
});

I did format the code before posting but for some reason it lots its format. Sorry.

@mayur_hotmail you can always edit your post

For some reason saving after editing the original post gives error.
Hi There,

We are trying to restrict values in Supplier Link field on Purchase Order page to certain values based on a filter using below custom script but it doesn’t seems to work. Please help.

frappe.ui.form.on("Purchase Order", "onload", function(frm) {
    cur_frm.set_query("supplier", function() {
        return {
            "filters": {
                "currency_pay_in": "USD"
            }
        };
    });
});

We have defined a custom field for supplier called company. Once above script is working, we want to replace "currency_pay_in with company field and change “USD” to “cur_frm.doc.company”.

Please note we also have following onload custom script in addition to above for purchase Order doctype.

frappe.ui.form.on("Purchase Order", "onload", function(frm) {
    cur_frm.set_query("expense_account","items", function() {
        return {
            "filters": {
                "company": cur_frm.doc.company
            }
        };
    });
});



frappe.ui.form.on("Purchase Order", "onload", function(frm) {
    cur_frm.set_query("cost_center","items", function() {
        return {
            "filters": {
                "company": cur_frm.doc.company
            }
        };
    });
});

@rmehta
I have managed to get it to work by changing onload to refresh. What is the difference between the two?

frappe.ui.form.on("Purchase Order", "refresh", function(frm) {
    cur_frm.set_query("supplier", function() {
        return {
            "filters": {
                "currency_pay_in": "USD"
            }
        };
    });
});
3 Likes

onload is called only the first time the document is loaded on the screen, subsequent times, it is refresh