After upgrading to ERPnext 11 I had trouble setting child dynamic documents directly. I solved it by creating an object for the dynamic links and adding it to the newly created record. I then discovered that frappe.db.insert now returns a promised, which has greatly simplified coding. I have included my function for creating a new customer. customerInfo is returned from a dialog box, and fieldID is the field to update on the current form with the name of the customer record.
async function CreateCustomer(customerInfo,fieldID) {
var customer_name=“”;
var customer_group=“”;
var customer_type=“”;
var send_email=0;
var statement_email=“”;
if(!customerInfo.email) {customerInfo.email="";}
customerInfo.email=customerInfo.email.trim();
if(customerInfo.email) {send_email=1;}
if (customerInfo.company) {
customer_name=customerInfo.company.trim();
customer_group="Commercial";
customer_type="Company";
statement_email=customerInfo.email;
}
if (!customerInfo.last_name) {customerInfo.last_name="";}
//If the customer name is blank then get First name, Lastname
if (!customer_name) {
if (customerInfo.first_name) {
customer_name=customerInfo.first_name.trim()
}
if (customerInfo.last_name) {
customer_name+=" "+customerInfo.last_name.trim();
}
customer_group="Individual";
customer_type="Individual";
}
//Updated code to use promise so can get the name of the created customer
//which is used to link address
let customer = await frappe.db.insert({
doctype: 'Customer',
customer_name: customer_name,
customer_group: customer_group,
customer_type: customer_type,
send_invoice_by_email:send_email,
send_statement_by_email:send_email,
email_for_statements:statement_email
});
//Create a link object and remember to assign it to an array using [mylink]
var mylink={
link_doctype:"Customer",
doctype:"Dynamic Link",
link_name:customer.name
};
if (customerInfo.city.trim()=="") {
customerInfo.city="Motueka";
}
if(!customerInfo.address_line2) {customerInfo.address_line2="";}
if(!customerInfo.post_code) {customerInfo.post_code="";}
//Insert the address
let address = await frappe.db.insert({
doctype: 'Address',
address_type: 'Billing',
address_title:customerInfo.address_line1.trim(),
address_line1:customerInfo.address_line1.trim(),
address_line2:customerInfo.address_line2.trim(),
city:customerInfo.city.trim(),
country:customerInfo.country.trim(),
pincode:customerInfo.post_code.trim(),
links:[mylink]
});
//insert the contact
if(!customerInfo.fax) {customerInfo.fax="";}
if(!customerInfo.mobile) {customerInfo.mobile="";}
let contact = await frappe.db.insert({
name:customer_name+"-"+customerInfo.first_name.trim()+" "+customerInfo.last_name.trim(),
doctype: 'Contact',
first_name: customerInfo.first_name.trim(),
last_name:customerInfo.last_name.trim(),
phone:customerInfo.phone.trim(),
mobile_no:customerInfo.mobile.trim(),
fax:customerInfo.fax.trim(),
email_id:customerInfo.email.trim(),
links:[mylink]
});
cur_frm.set_value(fieldID,customer.name);
}