How to filter options of a link field in a web form

I have a web form, it has a link field named branch. so when users clicks on the field 9 branches are being shown as option. I want to show only 7 branches. how can i do it?
getting error ‘frappe.web_form.on()’ is not a function. for below code

frappe.web_form.on('branch', (field, value) => {
    console.log("Setting query for branch 2");

    const allowedBranches = [
        "branch1",
        "branch2",
        "branch3"
    ];
    
    var branchField = frappe.web_form.get_field('branch');
    if (branchField) {
        console.log("branch field")
        // Prepare options in the format { label: '...', value: '...' }
        var options = allowedBranches.map(branch => ({
            label: branch,
            value: branch
        }));
        console.log("adding branch field filtered options")
        // Assign the options to the field and refresh
        branchField._data = options;
        branchField.refresh();

        console.log("Branch options filtered:", options);
    } else {
        console.error("Branch field not found!");
    }
});

also tried this code

frappe.web_form.after_load = ()=>{
    console.log("Web Form Loaded 2");

    // Allowed branches
    const allowedBranches = [
        "branch1",
        "branch2",
        "branch3"
    ];

    // Get branch field object
    var branchField = frappe.web_form.field_group.get_field('branch');

    if (branchField) {
        // Prepare options in the format { label: '...', value: '...' }
        var options = allowedBranches.map(branch => ({
            label: branch,
            value: branch
        }));
        console.log("adding branch field filtered options")
        // Assign the options to the field and refresh
        branchField._data = options;
        branchField.refresh();

        console.log("Branch options filtered:", options);
    } else {
        console.error("Branch field not found!");
    }
};

one doubt i have is that all the code should be inside frappe.ready() or not?

frappe.ready(function() {}

Is this your final code?
If branch is Link field.
why there is hard coded branches.

if it is a Link field.
you can add filter to that field to show whatever data you want.

I just want to filter based on branch name and i want to show only those branches which are in the allowed Branches

frappe.web_form.fields_dict.{link_field_name}.set_data(["Option1","Option2"])

you need to options then

2 Likes

above code should be insidefrappe.ready(function() {}), right?

1 Like

yess. it should be inside ready

ok. thanks @Sudhanshu