Whats wrong in this SQL query call from python?

from future import unicode_literals
import frappe
from frappe.model.document import Document

query= frappe.db.sql(“”“select state, count() from Lead where state is not null group by state"“”)
frappe.get_all(“Lead”, fields = [“state”, "count(
)”]) for row in frappe.get_all(“Lead”, fields = [“state”, “count()"]):
print row[“state”]
print row["count(
)”]

Its not working. Can anyone tell what’s wrong in that ??

try → count(column name) e.g. count(name)

also use tab befor table name → tabLead

1 Like

from future import unicode_literals
import frappe
from frappe.model.document import Document
import json

@frappe.whitelist()

def state_wise(state):
query= frappe.db.sql(“”“select state, count(lead_name) from tabLead where state is not null group by state”“”)
frappe.get_all(“Lead”, fields = [“state”, “count(lead_name)”])
print row[“state”]
print row[“count(*)”]

Not working. Still not getting any values

Why are you using frappe.db.sql and frappe.get_all() at the same time ?

I am trying to understand your query? is it the case that sql call is not giving result as get_all ?

Regards,

Parth

yes, when i was using only frappe.db.sql still it wan’t giving values

query= frappe.db.sql(“”“select state, count(lead_name) from tabLead where state is not null group by state”“”)
for row in query():
statelist=list(row)
state=statelist[0]
lead=statelist[1]
print “query = {}”.format(frappe.as_json(query))
return (state,lead)

Ok @nikzz,

Did you try the query in $bench mysql is the query giving you results there ?

Regards,

Parth

yup, its giving values there

I am also trying to do something like that… but did’t get any solution. Please help.

query= frappe.db.sql(“”“select state, count(lead_name) from tabLead where state is not null group by state”“”)
for row in query:
state=row[0]
lead=row[1]
return (state,lead)

tried that too, no success

What error you are getting?

Nothin…just blank

show me response of api in network tab and Javascript code

.js here

frappe.ui.form.on(‘graphtesting’, {
onload: function(frm) {

$.getScript(“https://unpkg.com/frappe-charts@1.1.0/dist/frappe-charts.min.iife.js”,function(){

var state=new Array();
var lead=new Array();

frappe.call({
query: “erpnext.erpnext.selling.doctype.graphtesting.graphtesting.state_wise”,
args: {‘state’:frm.doc.state,
‘lead’:frm.doc.lead},
async: false,
callback: function(r) {
if(r.message) {

state = r.message.state;
lead = r.message.lead;

},
},

});

let chart = new frappe.Chart( “#chart”, {
data: {
labels:[state],

  datasets: [
    {
      name: "Total Lead", chartType: 'bar',
      values:[lead],
      
    },
    
  ],
  


},

title: "State-Wise Lead Count",
type: 'bar',
height: 300,
colors: ['blue'],

tooltipOptions: {
  formatTooltipX: d => (d + '').toUpperCase(),
 formatTooltipY: d => d + ' pts',

}

});

});

}

});

this one is not response of that API.

sorry

and the previous one I was using :

via online checking with api tester both are passed (checked from apitester.com)

It should be like this. This one is API call.

I have no idea how to get api of erpnext . Any help ?

I think you are passing query instead of method in your frappe.call

Also, refer following code for clarity

from future import unicode_literals
import frappe
from frappe.model.document import Document
import json

@frappe.whitelist()
def state_wise(state):
state_wise_date = frappe.db.sql(""“select state, count(lead_name) as total_lead from `tabLead` where state is not null group by state”"")
return state_wise_data

On the client-side do

frappe.call({
    method: 'erpnext.erpnext.selling.doctype.graphtesting.graphtesting.state_wise',
    callback: function(response) {
       console.log(response.message);
    }
  });

@surajshetty using the method of yours is giving error that "The resource you are looking for is not available " .

I am not getting why a simple query isn’t giving output in console even

Method should be " method: erpnext.selling.doctype.graphtesting.graphtesting.state_wise"

But on this its giving error that TypeError: state_wise() takes exactly 1 argument (0 given)

But in .py file argument is there