Fetch value of parent doctype to custom doctype in Table format

Hello everyone,

->I have created a custom DocType “Lead Contact” under module CRM.
->“Lead Contact” is Table under the Lead DocType.
->I’m trying to fetch the “company_name” from Lead and add it “company_name” in Lead Contact.

I’ve tried

cur_frm.add_fetch("Lead", "company_name", "company_name");

and

frappe.ui.form.on('Lead Contact', 'lead', function(frm, cdt, cdn){
    frappe.call({
        'method': 'frappe.client.get_value',
        'args': {
            'doctype': 'Lead',
            'filters': [
                ['Lead', 'name', '=', locals[cdt][cdn].lead]
            ],
           'fieldname':'company_name'
        },
        'callback': function(res){
            frappe.model.set_value(cdt, cdn, 'company_name', res.message.company_name);
        }
    });
});

Neither worked. Any help would be greatly appreciated.

Thank You!

@Tanuj

try

frappe.ui.form.on('Lead Contact', 'form_render', function(frm, cdt, cdn){
    locals[cdt][cdn].company = frm.doc.company;
});

@max_morais_dmm

Thanks for replying Max!

Unfortunately, that didn’t work either! I tried the custom script for both: Lead, as well as Lead Contact.

@Tanuj sounds confuse!
Imagine that I’m a baby and tell me what do you are trying to do!

@max_morais_dmm

So my end goal is to make my Leads Organization-centric.

To do this, I have created a new DocType: Lead Contact, which would allow my Sales Reps to add multiple contacts for each lead, and not have to make a new lead for a person in an existing organization.

If you remember from my previous post: How to remove Contact HTML from Customer and replace with Table , I was having a problem converting my Lead to a Customer, because the Lead Contacts were not getting mapped to the Contact HTML for Customer.

For this reason, I’m trying to link my Lead Contact to Lead with Organization Name in both. (field name: company_name), so i can fetch the values of the contacts with the Organization Name into contacts via the Customer.py file.

For Example:

An organization Frappe (Organization Name) is saved as a lead, and the Contact List(Lead Contact Doctype) has the names of the people at Frappe, i.e., Rushabh, Anand, etc.


I want the Organisation under Contact List(Lead Contact Doctype) to be automatically updated with the name of the Organization added in Lead.

That is: Each person like Anand would have Organization Name as Frappe. (The field should be “read-only”, but i’ll do that once i get it to work.)

Thanks so much for all your help!!

@Tanuj, are you doing it only because the Contacts are not mapped from Lead to Customer?

@max_morais_dmm
Yes.

@Tanuj A nice approach for this is

add a Python Hook event to the Customer based on “before_insert”, and the function will be this.

def before_insert_customer(doc, method=None): 
    if doc.lead_name:
        for contact in frappe.get_all("Contact", {"lead": doc.lead_name}): 
            frappe.db.set_value("Contact", contact["name"], "lead", self.lead_name)

Every time that you convert a lead to a customer it will track the contacts.

Do you can replace the event to “on_update” for every time too.

2 Likes

@max_morais_dmm

That’s so much easier. Thanks!

Is there any documentation that you referred to for this? or just your experience? :smile:

@Tanuj, knowledge that you can get seeing the videos for developers, just do a search here!

This topic was automatically closed after 36 hours. New replies are no longer allowed.