Fetch Value of Link based on query filter with specfic condition

  1. Drop Down 3 values
  2. Based on value selected in above drop down list , i need to populate other field(Link Type) based on if the value is not allocated in DB.

Hi there, not sure if this will help, but this is one sample script I’m using to fetch values from my DB using AJAX calls, you’ll need to adjust yours accordingly:

Client Script:

frappe.ui.form.on('Project', {

    activity_id: function(frm){
        let activity_id = frm.doc.activity_id;
        if(activity_id.length === 10){
                method: "erpnext.calls.get_activity",
                args: {activity_id: activity_id}
            }).done((r) => {
                // Activity Details
                frm.doc.start_date = r.message[0].activity_start_date
                frm.doc.end_date = r.message[0].activity_end_date

                // Refresh all fields
                Object.keys(frm.doc).forEach(function (item) {


On the server, under apps/erpnext I created calls.py and the function get_activity(arg) which will query the DB according to the docs here: Database API

In my case above I’m passing the activity_id as arg for the py function and processing it (r.message[0] is the response).

Just adding on to the backend file:


import frappe

def get_activity(activity_id):
    return frappe.db.sql(f"SELECT * FROM <your_table> WHERE <condition>;", as_dict=True)
1 Like