Issue with the script

Hi All ,

The below script works , but the issue is , it searches for the oldest doc created (in other words doc created first ) and sets the value in the required field .

{
frappe.ui.form.on("VAVE", "prodorder", function(frm, cdt, cdn){
frappe.call({
       "method": "frappe.client.get_value",
        args: {
		doctype: "ht",
		fieldname: "htbatch",
		filters: {
		p1:["=", frm.doc.prodorder]
			 }           			
	      },								
	      callback: function (data) {									
	      console.log(data);
              cur_frm.set_value("ht", data.message.htbatch);
				        }					
	     })

});
}

If there are docs with same p1 as that of prodorder , it sets HT00001 instead of HT00002 or HT00003 . But I want to set the value of the latest doc created that is HT00003 .

Could someone guide me , please .

Thanks

Hi, you can try something like querying the table and get the last or first one depending on the order :slight_smile: but I think there’s an easier way of doing it, what I said was just the first thing that came out on my mind.

1 Like

Hi @johnskywalker . [quote=“johnskywalker, post:2, topic:16814”]
Hi, you can try something like querying the table and get the last or first one depending on the order :slight_smile: but I think there’s an easier way of doing it, what I said was just the first thing that came out on my mind.
[/quote]

Thanks for swift response , could you tell me how to do that ?

@Muthu you have to create a python file for that, which will return the first/last of the frappe.db.sql and in your .js you to do something like

frappe.ui.form.on("Doctype", "field", function(frm) {

	return  frappe.call({
		method: "path.to.your.method",
		args: {
				"var": var_arg
		},
		callback: function(r, rt) {
			if(r.message) {
                                 //some code
                       }
               }
}

But I suggest, if there’s a way that you don’t have to do python it would be better :slight_smile:

2 Likes

Thanks a ton for swift response @johnskywalker . As I am not a naturally gifted programmer , the above code looks bit confusing , could you please guide me , if possible .

Thanks