Add entries to a doctype table with Javascript?

Hi @Mohammed_ElKadee,

I have managed to do this client side using frappe.run_serially
I feels like a bit of a hack since it’s used mainly for writing code tests but it does work. It is not “invisible” like doing the same thing in Python that can run in the background, but I personally prefer to stay client side if I can.

I wrote this to force the user to create a project AFTER the Sales Order is saved in draft (so it has a number) but BEFORE it’s submitted and can’t be modified. This way a new project can be created linked to the Sales Order number and the SO can be linked to the project number so they can be easily referenced from the dashboard.

  • The first part of the code changes the “Project” field on the SO to required as soon as the SO is saved as a draft

  • This code is set to run when the “Create a new project” link is clicked while in the SO doctype

  • frappe.route_options is a list of field data that is sent from the current form (SO) to the new form (Project)

  • then the new_doc is created and the field data gets copied in

  • the new_doc becomes the current form so cur_frm.save() saves the new project form

  • frappe.set_route then follows the newly set SO link on the project form and takes me back to the original SO form

  • lastly, I use cur_frm.set_value to add the newly saved project name to the SO, SO can then be submitted.

      frappe.ui.form.on("Sales Order", "refresh", function(frm, cdt, cdn){
      	if (!frm.doc.__islocal) {
      		(frm.toggle_reqd("project", true));
      	}
      	else {
      		(frm.toggle_reqd("project", false));
      	}
      });
      frappe.ui.form.on('Sales Order', {
      	refresh: function(frm) {
      	frm.fields_dict.project.new_doc = function(){
      		frappe.run_serially([
      		() => frappe.route_options = {
      			"project_name": frm.doc.project_name,
      			"project_type": "External",
      			"sales_order": frm.doc.name,
      			"customer": frm.doc.customer,
      			},
      		() => frappe.new_doc("Project"),
      		() => cur_frm.save(),
      		() => frappe.set_route('Form', 'Sales Order', frm.doc.name),
      		() => cur_frm.set_value("project", frm.doc.project_name),
      		])
      		};
      	}
      });
    

I realize you’re probably not trying to do the same thing I was, but hopefully explaining this example will help you adapt it to your needs.

3 Likes