Need help in fetching data

Hi Guys ,

I am able to fetch data using cur_frm.add_fetch but this fetches something in a field say zzz ,now I want to fetch data in some other field from the data already fetched in zzz field . I tried the same cur_frm.add_fetch , this gets triggered only if I manually edit zzz field. It does not automatically the fetch data .

Please help

Thanks

Try using cur_frm.set_value

1 Like

Many thanks for the reply , but what I want is the field zzz is a link field , my script fetches the exact data in zzz then from the zzz should fetch data and put it in ddd , how do I do that ?

cur_frm.add_fetch('item_code','ccc','zzz');

cur_frm.add_fetch('zzz','bbb','ddd');

The first code works perfectly and fetches the data in zzz field , but the second code does not work until I manually edit the data of zzz field . In this case could you please guide me @KanchanChauhan.

Kindly help

Thanks

Hi Muthu,

May be this helps you,

add_fetch(link_fieldname, source_fieldname, target_fieldname)

https://frappe.github.io/erpnext/user/manual/en/customize-erpnext/custom-scripts/custom-script-examples/custom-script-fetch-values-from-master
https://erpnext.com/docs/user/manual/en/customize-erpnext/custom-scripts/fetch-values-from-master

In frappe add fetch not work for second level

for this you need to write js code.

1 Like

@KanchanChauhan suggested use cur_frm.set_value

cur_frm.set_value('ddd','zzz')

2 Likes

Many thanks for the reply @Jitendra_Khatri1

Could you just tell me , how do I do this ?

Thanks

Hi @Nick , I understood

I dont want zzz’s value to appear in ddd field , zzz is the link field which gets filled automatically when I use this code
cur_frm.add_fetch('item_code','ccc','zzz');

now my question is this code should work again with the data already fetched in the zzz field and fetch a data from bbb field in the zzz doc and put it in my ddd field.

To put simply cur_frm.add_fetch is not working for the second level .

Please help

@Muthu

// on js side
frappe.ui.form.on("**your Doctype**", "onload/whatever", function(frm) {
  
   frappe.call({
        method:"yourDoctype.method_name",
        args:{"zzz":cur_frm.doc.zzz},
        callback: function(r) {
          if (r.message){
              cur_frm.set_value("ddd_field",r.message[0].zzz)
          }
        }
    });
  }

// on py side

@frappe.whitelist()
def method_name(zzz):
  //set this Query as per your requirement
  data = frappe.db.sql("select name from `tabDoctype` where name = %s",zzz, as_dict=1)
  return data
1 Like

you can use this method

frappe.client.get_value

1 Like

Many thanks for the help @shivkumar , but the above code is not working . I want to fetch bbb field and put that in ddd , where bbb is linked with zzz . Could you please guide me ?

Thanks

Hi @Jitendra_Khatri1 , thanks for the reply , could you please show me an example ?

Thanks

Hi @Jitendra_Khatri1 @shivkumar @KanchanChauhan @Nick , what I want is a javascript handler that allows me to call add_fetch() after my tcno field loads ( which is linked with the item module ) , both item doc and Tc doc have the same field cmill , I tried to accomplish with the help of @shivkumar with this code , but this is not working , please someone help me ,

frappe.ui.form.on("TC", "onload", function(frm, cdt, cdn){
  if (cur_frm.doc.__islocal && cur_frm.doc.item){
    frappe.call({
      "method": "frappe.client.get_value",
      "args": {
         "doctype": "Item",
         "filters": cur_frm.doc.item,
         "fieldname": "cmill"
       },
      "callback": function(res){
        if (!res.exc){
          cur_frm.set_value("cmill", res.message.cmill);
        }
      }
    });
  }
});

check this…using console? res.message[0] or res.message

In your code:

“callback”: function(res){
if (!res.exc){
console.log(res.message[0].cmill);
cur_frm.set_value(“cmill”, res.message[0].cmill);
}
}

1 Like

Many thanks for the reply @shivkumar , I am not getting anything with this code , please help , it does not show anything

frappe.ui.form.on("TC", "onload", function(frm, cdt, cdn){
  if (cur_frm.doc.__islocal && cur_frm.doc.item){
    frappe.call({
      "method": "frappe.client.get_value",
      "args": {
         "doctype": "Item",
         "filters": cur_frm.doc.item,
         "fieldname": "cmill"
       },
      "callback": function(res){
if (!res.exc){
//// console.log(res.message[0].cmill)
cur_frm.set_value("cmill", res.message[0].cmill);
}
}
});}});

Please help
Thanks

@Muthu Try this:

frappe.call({
       "method": "frappe.client.get_value",
        args: {
								doctype: "item,
								fieldname: "cmill",
								filters: {
									name:["=", frm.doc.item_name]
									}           			
								},								
								callback: function (data) {									
								console.log(data);
							}					
					})
1 Like

Hi @Nick , Thanks for the reply ,

The above code gives this error in the console

VM193:1300 Uncaught SyntaxError: Invalid or unexpected tokensetup @ form.min.js:2573_f.Frm.setup @ form.min.js:213_f.Frm.refresh @ form.min.js:535load @ form.min.js:97(anonymous function) @ form.min.js:82callback @ desk.min.js:5815callback @ desk.min.js:1201200 @ desk.min.js:1226(anonymous function) @ desk.min.js:1316i @ jquery.min.js:2fireWith @ jquery.min.js:2z @ jquery.min.js:4(anonymous function) @ jquery.min.js:4
form.min.js:4777 Uncaught TypeError: Cannot read property ‘refresh’ of undefinedfrappe.ui.form.set_viewers @ form.min.js:4777(anonymous function) @ form.min.js:54Emitter.emit @ libs.min.js:2284Socket.onevent @ libs.min.js:1828Socket.onpacket @ libs.min.js:1786(anonymous function) @ libs.min.js:2146Emitter.emit @ libs.min.js:2284Manager.ondecoded @ libs.min.js:1344(anonymous function) @ libs.min.js:2146Emitter.emit @ libs.min.js:2284Decoder.add @ libs.min.js:6895Manager.ondata @ libs.min.js:1334(anonymous function) @ libs.min.js:2146Emitter.emit @ libs.min.js:2284Socket.onPacket @ libs.min.js:2902(anonymous function) @ libs.min.js:2720Emitter.emit @ libs.min.js:2284Transport.onPacket @ libs.min.js:3327Transport.onData @ libs.min.js:3319ws.onmessage @ libs.min.js:4382

Please help

it should be doctype:"Item",

1 Like

@Muthu

Sorry there is typo.
doctype: “Item”

1 Like

Many thanks for the reply @makarand_b and @Nick , even the below code gives this error .

frappe.call({
       "method": "frappe.client.get_value",
        args: {
								doctype: "Item",
								fieldname: "cmill",
								filters: {
									name:["=", frm.doc.item_name]
									}           			
								},								
								callback: function (data) {									
								console.log(data);
							}					
					})

Uncaught ReferenceError: frm is not defined(anonymous function) @ VM292:1303setup @ form.min.js:2573_f.Frm.setup @ form.min.js:213_f.Frm.refresh @ form.min.js:535load @ form.min.js:97(anonymous function) @ form.min.js:82callback @ desk.min.js:5815callback @ desk.min.js:1201200 @ desk.min.js:1226(anonymous function) @ desk.min.js:1316i @ jquery.min.js:2fireWith @ jquery.min.js:2z @ jquery.min.js:4(anonymous function) @ jquery.min.js:4
form.min.js:4777 Uncaught TypeError: Cannot read property ‘refresh’ of undefinedfrappe.ui.form.set_viewers @ form.min.js:4777(anonymous function) @ form.min.js:54Emitter.emit @ libs.min.js:2284Socket.onevent @ libs.min.js:1828Socket.onpacket @ libs.min.js:1786(anonymous function) @ libs.min.js:2146Emitter.emit @ libs.min.js:2284Manager.ondecoded @ libs.min.js:1344(anonymous function) @ libs.min.js:2146Emitter.emit @ libs.min.js:2284Decoder.add @ libs.min.js:6895Manager.ondata @ libs.min.js:1334(anonymous function) @ libs.min.js:2146Emitter.emit @ libs.min.js:2284Socket.onPacket @ libs.min.js:2902(anonymous function) @ libs.min.js:2720Emitter.emit @ libs.min.js:2284Transport.onPacket @ libs.min.js:3327Transport.onData @ libs.min.js:3319ws.onmessage @ libs.min.js:4382

Please help

@Muthu
Did you enclose it in the below?

frappe.ui.form.on("TC", "tcno", function(frm, cdt, cdn){ });

1 Like