Custom Script Help needed - adding value from main DocType in Child table

Hi, taking my first steps into custom scripting and I can’t get my head around this issue. Went over a lot of threads on this forum, but as all solutions seem custom adapted to the requests I can’t figure out how to adapt to given answers. So trying my luck to see if somebody is friendly enough to help this noob out…

What I Have

  • the main DocType Project with:
  • a custom field called target
  • a child table named Project Details containing several custom fields like meas and corr

What I Need to happen in the Child Table:

the field corr should show the difference between the value of target and meas like in -(target)-meas

Note: all values will be minus (eg -23,0)

Example
if target=-23,0 and meas=-22.1 than corr should show -0,9 as -22,1 should be lowered by -0,9 to become -23.

any help would be really appreciated!

I’m trying to fetch the value of target to the child table in each field of the corr column to get me started, and prior to do the actual math.

I have this custom script for the parent DocType Project:

frappe.ui.form.on("Project", "refresh", function(frm) {	
	frappe.ui.form.on("Project Details", {
			"episode": function(frm) {
			frm.add_fetch("episode", "target", "corr");
		}
	});

});

But this ‘simple’ fetch from the parent DocType does not work at all. It shows that I don’t understand how the custom script works at all…

This may help get you started.

In the code below, I made an assumption: that the DocField in 'Project', which points at 'Project Details', is named ‘details’. You may need to change “details” to something else, depending on the actual name of your DocField.

frappe.ui.form.on('Project', {
	refresh(frm) {

        // Loop through every Child Document...    
	    frm.doc.details.forEach(child_doc => {
            // for each found, set corr = target - meas.
	        child_doc.corr = frm.doc.target - child_doc.meas;
	    });
        // ask the web page to show the latest values:
	    refresh_field('details');
	}
});

Note that the way I’m doing it above, the values of corr are updated:

  1. When the page is loaded.
  2. After modifying “target” or “meas”, and then clicking Save.

There are many other ways this could be coded, depending on your precise requirements.

Thank you Brian for your insights…

Project Details' is the name of the Child Table that currently needs to have one column corr populated with the calculated value of 'target' - meas.

I’ve renamed it to ‘project_mastering_details’ to avoid spaces.

As I’m new to ERPNext and the Frappe Syntax I’m currently not able to wrap my head around this and you are really helpful to give me a start where to look… I really appreciate!

So when I look at your suggestion I think I should do something like this:

frappe.ui.form.on('Project', {
	refresh(frm) {

    // Loop through every Child Document...    
	    frm.doc.project_mastering_details.forEach(child_doc => {
        // for each found, set corr = target - meas.
	        child_doc.corr = frm.doc.target - child_doc.meas;
	    });
    // ask the web page to show the latest values:
	    refresh_field('project_mastering_details');
	}
});

But I’m still off apparently, as this does nothing at all. It is not 100% clear to me what the last line states: refresh_field('project_mastering_details'); Should this be the Child Table as in my example or the field that holds the calculation: corr?

I’ve tried both, but the field stays empty.

Nobody?

Hi @pedro_vde,

Can you screenshot the Project DocType, and show me the DocField row that is a 'table' and points at the Project Details child document?

Hi Brian, here you go…

As you can see there is an empty column that needs to receive the calculation.

Can you screenshot the DocType/Customization itself? Example below.

I don’t have a Project Details child document beneath Project. So trying to understand how your schema is structured.

Hi Brian, I’m really sorry that my reply took so long… apparently I’ve missed your update. Your help is really appreciated and thank you for taking your time to look at my issue.

This is what the customization of the Project DocType looks like for that particular Section:

As you can see there is a data field called target_lufs_lkfs and a child table mastering_notes

The child table is something more complicated than what I had in my original post as I need to have two separate columns that need to be auto-calculated, one for the 5.1 measurements and one for the 2.0 measurements. I’ve removed their actual delimiters to get an insight into how that custom script would be built.

I’ll try to keep an eye on this thread as the automatic update mails don’t seem to come true and I don’t want to take to much of your time.

Kind regards from Belgium
pedro

I still cant wrap my head around it. If I use brian_pond’s suggestion console trows errors on the forEach statement.

I was trying to get this working in its simplest form using this tutorial but I’m missing something important as there is nothing showing at all in the child’s table target field.

So to learn how it works, I just need a basis to start from:

  • DocType with Child Table: “Project
  • Child Table: “project_mastering_details
  • Data Field on Main Doc Type “target_lufs_lkfs
  • Child Table Row identifier: “episode
  • Data input field per episode in child table: “meas_6tr
  • Calculated read only field in child table with the result: “corr_6tr

The calculation that should be done is to distract a positive version of “target_lufs_lkfs” from “meas_6tr” (-(target_lufs_lkfs)-meas_6tr)

If somebody can give me the simplest custom script for this I would be thrilled and I hopefully could start to understand how this scripting really works… and I would be able to do all the needed calculations…

thanks in advance!

Thanks to @NCP in another thread I’ve finally figured out what I did wrong.

When referring to a Child Table I was using the name of the Child Table but I should use the name of the field that shows the Child Table in the Parent DocType.

@brian_pond That is the reason why your script at my side was not working… now it does perfectly and as expected!

Thank you both!

2 Likes