Need help in fetching data

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

Hi , @Nick , many thanks for the reply , yes of-course I did .

Please help ,

Thanks

@Nick . Sorry my mistake , instead of reloading and clearing the cache , I reloaded the page , now the page loads but the the script is not working , I had a look at the console no error but the script is not working .

Please guide me

Thanks

@Muthu
Please share script

1 Like

@Nick Thanks a lot for swift reply and for continued support , here is the script

frappe.ui.form.on("TC", "tcno", function(frm, cdt, cdn){
frappe.call({
       "method": "frappe.client.get_value",
        args: {
		doctype: "Item",
		fieldname: "cmill",
		filters: {
		name:["=", frm.doc.item_name]
			 }           			
	      },								
	      callback: function (data) {									
	      console.log(data);
          cur_frm.set_value("cmill", data.cmill);
				        }					
	     })

});

Please help
Thanks

1 Like

@Muthu
Script says when tcno field from TC form changes cmill of the Item record of the item (i.e. item_name field from tc form) is fetched. And output will be displayed in console.

Is this you want to achieve?

1 Like

Jquery is not able to reference to frm check if you have passed the frm to your function or use cur_frm instead.

Thanks, Makarand

1 Like