If Statement in filters of frappe.call

I am trying to pass if condition as a filter in frappe.call as i want to get data from that field whose checkbox is selected.

fields: ['mobile'],
	filters: { 
if(product_1==1)
{'product': 'Product 1'}
else if(product_2==1)
{'product':'Product 2'}
}
else
{'product':'Product 3'}
	    },

But i’m getting error. Is it right to filter data via normal js ??
If No, then any idea on how to get the data corresponding to that checkbox.

Thank You

Put the if condition outside of frappe call.
Use a variable to store value based on your if condition and pass it as value in filters

1 Like
frappe.ui.form.on("Group SMS", "onload", function(frm) {

if('product_1'==1)
{
var zz='Product 1';
}
else if('product_2'==1)
{
var zz = 'Product 2';
}
else
{
var zz = 'Product 3';
}

frappe.call({
    method:"frappe.client.get_list",
    args: {
        doctype:"Dummy User",
	fields: ['mobile'],
	filters: {'product':zz}
	    },
#more code

But its giving error. Guess that my variable is not able to get through frappe.call.

Also tried filters: {'product': 'zz'} and filters: {'product': 'zz'},
But still the same result

Initialize zz outside if condition like var zz = “”;
Not sure if that’s the error though

Please share your error.


The send sms button also stops showing.

Statically it was like

Done, still same issue

remove filters and try.

It should be like -

if(frm.doc.product_1)

1 Like

@nikzz sorry to interrupt you but i am wondering that how are you using ERPnext directly through localhost? without entering port 8080?
Can you please share the trick?

setup in production mode check this

https://frappe.io/docs/user/en/bench/guides/setup-production.html

Tried this. Although console.log is giving value inside frappe.call for var zz but on passing this varibale inside filter as :

filters: {'product' : zz } 

is still giving error.

sorry what is the error?
Can post your full code for frappe.call

frappe.ui.form.on("Group SMS", "onload", function(frm) {

var zz = "";
if(frm.doc.product_1==1)
{
var zz='Product 1';
}
else if(frm.doc.product_2==1)
{
var zz = 'Product 2';
}
else
{
var zz = 'Product 3';
}
frappe.call({
    method:"frappe.client.get_list",
    args: {
        doctype:'Dummy User',
	//filters: {'product': zz},
	fields: ['mobile']
		    },
    callback: function(r) {
var a=r.message;

var qq =JSON.stringify(a);
//console.log(a[0]['mobile']);
var dat= JSON.parse(qq);

var mobile_num= dat.map(x =>x.mobile);
 console.log(mobile_num);


frm.add_custom_button(__("Send SMS"), function(click) {
var event_type=cur_frm.doc.before_save;

$.ajax({
            url: "xxxxxxxxxxxxxxxxxxxxxxxxxxxx........", # my api
            data: {
                 "event_type": event_type,
             }
       });


});
}
   
});

});

And error is same, in console: Unable to handle success response

I got something, actually to do so, I first had to save it. But I want it to be dynamic, ie, without clicking on save i want the variable to be selected.

Any help ?

After saving and refreshing, i get the value in filter but I want that to be happening on realtime without clicking on save and refreshing the page.

I think your error is in handling response. You may be getting the checkbox value after it’s checked. See network tab in developer tools what args are passed

Yeah thats what I’m saying. After selecting checkbox and clicking on save , I get the value. But what I want is on clicking checkbox, i get the value instantaneously without clicking save.

ok can you show your oncheck event function for the checkbox?

Like below

frappe.ui.form.on("Group SMS", "product", function(frm){

//you code here

});

On doing so, the “send sms” button disappears.

My code is already posted earlier. See upwards

Place the Send SMS button in refresh method and try again