How to pass IN value of SQL Query using Javascript & Python

After a lot of trial and error I have found the right way to pass value for “IN” using JavaScript and Python in ERPNext.
Example SQL:
SELECT
tabEmployee.name,
tabEmployee.religion
FROM tabEmployee
WHERE tabEmployee.religion IN %(religions)s

Please read this article if you want to know how easy it is:

Hi @farukahmmed,

Simple syntax.

For javascript side:

frappe.db.get_list('Employee', {
  filters: {
    religion: ['Hindu', 'Christian', 'Muslim', .....]
  },
  fields: ['name', 'religion']
}).then(function(result) {
  // Process the result here
  console.log(result);
});

For Python side:

from frappe import db

def get_employee_list():
    religions = ['Hindu', 'Christian', 'Muslim']
    return db.sql("""
        SELECT name, religion
        FROM `tabEmployee`
        WHERE religion IN %(religions)s
    """, {'religions': religions}, as_dict=True)

Please check the syntax and apply your scenario with logic.

Thank You!

2 Likes