Fetch the latest entry created in a doctype order by the latest date field

Yes, you will need to put the code into a event handler and then set the values you need.

frm.set_value(“newfield1”, my_value1)
frm.set_value(“newfield2”, my_value2)
frm.set_value(“newfield3”, my_value3)
frm.set_value(“newfield4”, my_value4)
frm.save()

https://frappeframework.com/docs/v13/user/en/api/form#standard-form-scripts

Im newbie can you please give me a sample client script even 1 field automatically auto fill.

Really appreciate your time and effort

Hi All,

Anyone out there to post a sample Client Script to fetch the field value of the last record from custom doctype and automatically fill to the new field during new creation? only to get the idea

Thanks @peterg for the right direction

frappe.db.get_list("CUSTOM DOCTYPE", 
    {fields: ['*'], order_by: "creation desc", limit: 1})
frm.set_value(“newfield1”, my_value1)
frm.set_value(“newfield2”, my_value2)
frm.set_value(“newfield3”, my_value3)
frm.set_value(“newfield4”, my_value4)

anyone can figure out this scenario please. thanks

Hello guys,

Find this script, can i put the targetdoctype and sourcedoctype with the same docctype and what is [TRIGGER]

frappe.ui.form.on("[TARGETDOCTYPE]", {
    "[TRIGGER]": function(frm) {
        frappe.model.with_doc("[SOURCEDOCTYPE]", frm.doc.[TRIGGER], function() {
            var tabletransfer= frappe.model.get_doc("[SOURCEDOCTYPE]", frm.doc.[TRIGGER])
            $.each(tabletransfer.[SOURCECHILDTABLE], function(index, row){
                var d = frm.add_child("[TARGETCHILDTABLE]");
                d.[TARGETFIELD1] = row.[SOURCEFIELD1];
                d.[TARGETFIELD2] = row.[SOURCEFIELD2];
                frm.refresh_field("[TARGETCHILDTABLE]");
            });
        });
    }
});

Still hoping for some assistance or suggestions here…

Thanks

Please don’t keep bumping help requests. We’re all just volunteers here.

You want something like this:

frappe.ui.form.on("Monitoring", {
    "refresh": function(frm) {
        frappe.db.get_list("Monitoring", {fields: ['*'], order_by: "creation desc", limit: 1}).then((result) => {
            frm.set_value({"fieldnew1": result.field1, "fieldnew2": result.field2 })
            frm.save();
        }
    }
});

This is all just typed from memory, and there are almost certainly bugs. You might run into a problem where the new document actually fetches itself rather than the previous document created, in which case you’ll need some logic to check for that.

In any case, doing this with a client script is probably not the right approach anyway, as server scripts (python) are more reliable for important business logic. This should, however, get you started. Please make sure you’ve gone through the link I posted in my last post for more details.

Hi:
As you’ve said before, “I am trying to get and put last four field value to my new created record”

A trigger defines which event starts the function. When you exactly want to fetch and assign data? Maybe when you save the new record. Right? The trigger should be “before_save” (without quotation marks).

Create a client script linked to target doctype.
Anyway … are you working with child doctypes?

@avc @peterg Thank you so much, really really appreciate your time

  1. When you exactly want to fetch and assign data?

Before saving

Maybe when you save the new record. Right?

Yes

are you working with child doctypes?

I only have Monitoring doctype

FYI: programmatically, why i need to fetch the last four fields in my Monitoring doctype record? to calculate the difference between yesterday and today to put the value other field.

Like for example:

Yesterday Record:
field1 = 1000
field2 = 500
field3 = 600
field4 = 200

fieldnew1 = 900
fieldnew2 = 600
fieldnew3 = 1000
fieldnew4 = 100

New Created Record
Fetch this Yesterday Date:
field1 = 900
field2 = 600
field3 = 1000
field4 = 100

Today:
fieldnew1 = 1200
fieldnew2 = 700
fieldnew3 = 900
fieldnew4 = 200

Field with calculation
FieldLow1 = field1 - fieldnew1

So that in the current day we know if the value high or the value going down

There are really much better ways to do this. Duplicating yesterday’s data in today’s document isn’t generally a good practice. For what you’re trying to do, you’re probably better off using a report of some type.

1 Like

Are you saying can I manage this in report, without any client script?

Yes, indeed. All client scripts do is automate data entry. They’re not really meant to do analysis. Comparing and aggregating values in different documents is precisely what reports are designed to do.

1 Like

Is there any link or documentations to do the report something like what need to be done, I don’t have any idea scripting in report, I can write client script for calculation if the required fields value is there. that’s why I would refer to do it in the form side.

Thank you for this @peterg will take a look at this link after.
in the meantime I will test this first

frappe.ui.form.on("Monitoring", {
    "refresh": function(frm) {
        frappe.db.get_list("Monitoring", {fields: ['*'], order_by: "creation desc", limit: 1}).then((result) => {
            frm.set_value({"fieldnew1": result.field1, "fieldnew2": result.field2 })
            frm.save();
        }
    }
});

Agree with @peterg . Duplicate data in each new record may result unconsistent … The report just should show data of each record and add values of previous record. Probably that’s the best way.

Anyway, “refresh” event occurs every time you load a form. Note that this behavior will overwrite your previous data. Try “setup” event instead, this only occurs just on record creation.

Create doctyle Monitoring and copy and paste the script

@peterg I got Error message:

Error in Client Script
SyntaxError: missing ) after argument list
at init.setup (http://192.168.0.100/assets/js/form.min.js?ver=1652140582.0:17294:19)
at FrappeForm.setup (http://192.168.0.100/assets/js/form.min.js?ver=1652140582.0:19925:24)
at FrappeForm.refresh (http://192.168.0.100/assets/js/form.min.js?ver=1652140582.0:20163:11)
at FormFactory.render (http://192.168.0.100/assets/js/form.min.js?ver=1652140582.0:14520:46)
at http://192.168.0.100/assets/js/form.min.js?ver=1652140582.0:14504:12
at Object.callback (http://192.168.0.100/assets/js/desk.min.js?ver=1652140582.0:9858:20)
at Object.callback [as success_callback] (http://192.168.0.100/assets/js/desk.min.js?ver=1652140582.0:3765:17)
at 200 (http://192.168.0.100/assets/js/desk.min.js?ver=1652140582.0:3809:35)
at Object. (http://192.168.0.100/assets/js/desk.min.js?ver=1652140582.0:3953:7)
at i (http://192.168.0.100/assets/frappe/js/lib/jquery/jquery.min.js:2:27151)

Which I expect to auto fill the 2 fields: fieldnew1 and fieldnew2

Sorry James, I can’t help you any further. There’s a syntax error in the code I posted, as I said there probably would be. It’s missing a ) somewhere. If you’re not able to track down the problem and fix it yourself, I’d encourage you to back up a step and spend some time learning javascript. This forum is not really well suited to the kind of support you are needing.

Ok thanks really appreciate the help and your time