Display data from another doctype

Hi,

I need to display details from another linked doctype and i dont want to save them in this doctypes table.
what might be the best logic for this?

eg: I am making a library member doctype linked to user and when the library member is selected from list then need to show data from user doctype (email,sex etc) along with library members fields.

See how the onload event works (contact info is loaded while customer loads): erpnext/customer.py at develop · frappe/erpnext · GitHub

Hi,

I have made a button in the onload javascript event and made the details appear using msgprint.

thanks @rmehta
Frankly that example was a little bit hard for me to digest :smile: since I am a beginner.

frappe.ui.form.on("Student", "user",function(frm) 
	{
        frappe.call({
            "method": "frappe.client.get",
            args: {
                doctype: "User",
                name: frm.doc.user
            },
            callback: function (data) {
                frappe.model.set_value(frm.doctype,frm.docname, "full_name",
                    data.message.first_name + (data.message.middle_name ?
                        (" " + data.message.middle_name) : "")+ (data.message.last_name ?
                        (" " + data.message.last_name) : "") );
            frm.add_custom_button(__('User Details'), function(){
				var msg = '<img src="'+data.message.user_image+'"/><br/>';
				msg = msg + 'Email : ' + data.message.email+'<br/>';
				msg = msg + 'Gender : ' + data.message.gender+'<br/>';
				msg = msg + 'Birthdate : ' + data.message.birth_date + '<br/>';
				msgprint(__(msg));
				});
            }
        })
    });
frappe.ui.form.on("Student", "onload", function(frm) 
{ 
	if(frm.doc.user!=undefined)
	{
        frappe.call({
            "method": "frappe.client.get",
            args: {
                doctype: "User",
                name: frm.doc.user
            },
            callback: function (data) {
            frm.add_custom_button(__('User Details'), function(){
				var msg = '<img src="'+data.message.user_image+'"/><br/>';
				msg = msg + 'Email : ' + data.message.email+'<br/>';
				msg = msg + 'Gender : ' + data.message.gender+'<br/>';
				msg = msg + 'Birthdate : ' + data.message.birth_date + '<br/>';
				msgprint(__(msg));
				});
            }
        })
     }
});

Please suggest a better way of doing this.

How can I make the details append to the bottom of the page. Do i need to add any custom fields for that? Please help me with that.
And why is that button on the top right corner? can i make it to the bottom of the page?

@ninjas005 If you want to set student_full_name on Student form on selection of student_id(User).
Then you have to add custom field on student form like ‘Student Name’ and use add_fetch method for fetching details from User form.
Example : cur_frm.add_fetch(‘user’,‘first_name’,‘student_name’);

Hi @priya_s,
I can also do it by making “Student Name” read only and setting option to user.first name.
But it makes a new column in the Student table. That is not what I wanted.

The problem with that is when ever I change the value of some fields in user doctype, the student doctype wont get updated. It will have the older value . plus we don’t want to save the same data in different tables.

I think you didn’t get it.
I have a Student doctype having field user that links to user doctype.
I need to display the details in the user doctype like email,gender etc in the students form.
thats all.

@ninjas005 Html field would be best if you don’t want to save values in form.

I think you are asking for code to show value in html field.
You can refer this code, here my_html is custom field added to customer doctype.
I am assigning “Student info” to my_html field.
You can write any html code and show it into form.

frappe.ui.form.on("Customer", "onload", function(frm,doctype,name) {
   $(cur_frm.fields_dict.my_html.wrapper).html("Student Info");
});
2 Likes

Hi @kolate_sambhaji ,

That worked!!!

Thanks :blush:

1 Like

Hi,
One more thing,
The onload event works the first time only,
when I press the back button and comes back, it loads the cached page. onload even is not triggered.

Is there a methode for onresume ?
Or is there an other way to run some code when it resumes?

you can also use refresh
This will call every time any value is changed or page is refreshed

1 Like

I attempted the same feat as @ninjas005

I placed this code in Custom script for doctype
My particular case:

Source DocType: Customer
Source field is a custom field with following options:
Label: TIN
Type: Data
Name: tax_id
Mandatory: Checked

This field contains data, and can also be seen as column header in Report for each “Customer”, so one can select it as a column.

Target DocType: Sales Invoice
Label: TIN
Type: Data
Name: tax_id
Mandatory: Unchecked

Desired behavior: When NEW Sales invoice is created, upon selection of Customer, a custom field in Sales Invoice labelled: TIN should be updated automatically with the data for the corresponding selected customer. The field can be read only. It should update automatically, each time a new customer is selected.
For a Sales Invoice created from a Sales Order or similar, when the NEW Sales Invoice is created, the same behavior is desired. And for each time a saved “Draft” Invoice is opened, the TIN should also load automatically.

SOLUTION FOUND: I managed to use the solution as described in the Custom Script Help when entering Setup>Custom Script>New. I used this version:
// fetch local_tax_no on selection of customer // cur_frm.add_fetch(link_field, source_fieldname, target_fieldname); cur_frm.add_fetch("customer", "tax_id", "tax_id");

Pending Problem: If created after several sales invoices have been created, it will only work with the NEW invoices, or by editing each one, reselecting Customer, and then the field will fill itself on its own. Once you save the invoice, the data is now saved with it. It would be great for it to be retroactive to previously created Sales Invoices (prior to new custom script)

1 Like

Hi,

I want to ask one question. I just create a new doctype name “Vehicle Info” which i add as a child table in the “Customer” doctype.

Also i added link field in Sales Order/Invoice. What i need is when i select a customer in sales order/invoice I just want to get the vehicles info which is related to the customer in the created link field.

Any help ? Please