Dear Community,
In Issue(Ticket) doctype
i had fields as
1.Customer as customer
2.Raised by Contact Person(link) as custom_raised_by_email_id
Contact Doctype:
i had one table name as Links (Table) as links Option as Dynamic Link
i has fields as
1.Document Type (link) as link_doctype option as DocType
2.Link Name(Dynamic link) as link_name option as link_doctype
Now in issue doctype if it has customer so the link table we manually add Document Type as Customer and Link Name as Customer Name based on customer.
what I need is if the customer already had link table, if i add new contact in Raised by Contact Person the Document Type and link Name auto fetch based on parent customer it only applicable for this doctype
if i add new contact in Raised by contact person.
Link type as “Customer” and Link name as “Parent Customer Name” should auto fetch
for eg:
customer name is ABC Fruits and already it has contact as Bany
so we manually add Document Type as “Customer” and Link Name as “ABC Fruits”
if i create new contact in Raised by Contact Person as Andy it auto fetch in link field as Document Type as “Customer” and Link Name as “ABC Friuts”.
I tried the code but it not works.
frappe.ui.form.on('Issue', {
refresh: function(frm) {
set_custom_email_filter(frm);
},
customer: function(frm) {
set_custom_email_filter(frm);
},
custom_raised_by_email_id: function(frm) {
if (!frm.doc.custom_raised_by_email_id) {
// If no contact is selected, prompt to create a new contact
frappe.confirm(
__('No contact selected. Do you want to create a new contact for the customer?'),
function() {
// Create new contact and populate Links table
create_new_contact(frm);
}
);
} else {
// Fetch the contact details when a contact is selected
frappe.db.get_doc('Contact', frm.doc.custom_raised_by_email_id)
.then(contact => {
if (contact) {
// Get email and phone details
let email = contact.email_ids && contact.email_ids.length > 0 ? contact.email_ids[0].email_id : '';
let phone = contact.phone_nos && contact.phone_nos.length > 0 ? contact.phone_nos[0].phone : '';
// Set values in custom fields in the Issue doctype
frm.set_value('custom_email_id', email);
frm.set_value('custom_phone_number', phone);
// Check if the contact already has a link to the current customer
let customer_link_exists = contact.links.some(function(link) {
return link.link_doctype === 'Customer' && link.link_name === frm.doc.customer;
});
// If no link to the customer exists, add it
if (!customer_link_exists) {
frappe.call({
method: 'frappe.client.save',
args: {
doc: {
doctype: 'Contact',
name: contact.name,
links: [...contact.links, {
link_doctype: 'Customer',
link_name: frm.doc.customer
}]
}
},
callback: function(res) {
if (!res.exc) {
frappe.msgprint(__('Customer link added to the contact successfully.'));
}
}
});
}
}
});
}
}
});
// Function to set filter for the custom_raised_by_email_id field
function set_custom_email_filter(frm) {
frm.set_query('custom_raised_by_email_id', function() {
if (frm.doc.customer) {
return {
filters: [
['Dynamic Link', 'link_doctype', '=', 'Customer'],
['Dynamic Link', 'link_name', '=', frm.doc.customer]
]
};
}
});
}
// Function to create a new contact and pre-fill Links table
function create_new_contact(frm) {
frappe.new_doc('Contact', {
first_name: frm.doc.customer_name || frm.doc.customer
}).then(new_contact => {
// Add a row to the Links child table
let row = frappe.model.add_child(new_contact, 'links');
// Populate link_doctype, link_name, and link_title fields
row.link_doctype = 'Customer';
row.link_name = frm.doc.customer;
row.link_title = frm.doc.customer_name || frm.doc.customer;
// Open the new Contact form with the pre-filled Links table
frappe.set_route('Form', 'Contact', new_contact.name);
});
}
Please help to solve this issue.
Thanks in Advance