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

I am trying to get and put last four field value to my new created record from the latest created in my custom doctype order by moni_date field, create a new custom doctype via client side scripting, but I am struggling to find same scenario of mine.

cur_frm.doc.creation see this in the forum to retrieve a created records

Here is the flow:

  1. Get the value of four fields from latest created doctype. - fields: field1, field2, field3, field4, moni_date
  2. Create New Record and put the lates value to the following fields: fieldnew1, fieldnew2, fieldnew3, fieldnew4, moni_last_date

Doctype Name: Monitoring

Last Rec. ------- New Rec.
field1 —> fieldnew1
field2 —> fieldnew2
field3 —> fieldnew3
field4 —> fieldnew4
moni_date—> moni_last_date

Thanks

Hi experts,

Is it possible in erpnext or is there any suggestion on how to accomplish this?

Thanks

Hi there,

I’m not totally follow you here, but if you want to just fetch the last four created documents of a doctype, you can use the get_list method, sorted by creation:

frappe.db.get_list("CUSTOM DOCTYPE", 
    {fields: ['*'], order_by: "creation desc", limit: 1})

Is that what you’re looking for?

Thank you for the reply,

how automatically fetch that value and put in newly created doctype?

this will need in Custom Script

Yes, if you want to automate this process, you could use a client script. Just pick the event you want to have as a trigger, fetch the latest doc, and set the values that you need.

I don’t totally understand your use case here, but this might be better done with a server script. If the data is not critical, a client script may be fine.

Here is form look like

how to can I put this value automatically in my field in newly created document?

something missing of this script after retrieving the value of fieldnew1…fieldnew4 need to put in field1…field4

frappe.db.get_list("CUSTOM DOCTYPE", 
    {fields: ['*'], order_by: "creation desc", limit: 1})

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