Try ERPNext Buy Support Partners Foundation

Create a document and copy child items from sales order

Hi
Now we added a custom button in sales order which will create anew 'job order" and copy some fields from sales order into our new ‘job order’
we wrote this custom script to copy fields but couldn’t copy items from sales order into ‘job order item’ how to work around that?

frappe.ui.form.on('Sales Order', {
refresh: function(frm) {
    if (frm.doc.docstatus==1) {

      if(cur_frm.has_perm("submit")) {
          frm.add_custom_button(__('Job Order'), function() {
               frappe.route_options = {
            customer: cur_frm.doc.customer,
            sales_order: cur_frm.doc.name,
            'customer_address':frm.doc.customer_address,
          'contact_mobile':frm.doc.contact_mobile,
          'contact_person':frm.doc.contact_person,
          job_order_item:frm.doc.items
          
          };
          frappe.set_route('Form', 'Job Order', 'New Job Order');
        }, __('Create'));
   // }
		}}}})

refering to this code

we can create adocument from sales order into job order and copy items as follows:

frappe.ui.form.on('Sales Order', {
	    refresh: function(frm) {
  	        
   
        if (frm.doc.docstatus==1) {

    if(cur_frm.has_perm("submit")) {
    frm.add_custom_button(__('Job Order'), function() {
			frappe.model.with_doctype('Job Order', function() {
				var jobOrder = frappe.model.get_new_doc('Job Order');
				jobOrder.customer=frm.doc.customer;
				jobOrder.posting_date=frm.doc.transaction_date;
				jobOrder.customer_address=frm.doc.customer_address
				jobOrder.shipping_address_name=frm.doc.shipping_address_name
				jobOrder.contact_person=frm.doc.contact_person;
				jobOrder.company_address=frm.doc.company_address;
				var items = frm.get_field('items').grid.get_selected_children();
				if(!items.length) {
					items = frm.doc.items;
				}
				items.forEach(function(item) {
					var jobOrder_item = frappe.model.add_child(jobOrder, 'job_order_item');
					jobOrder_item.item_code = item.item_code;
					jobOrder_item.item_name = item.item_name;
					jobOrder_item.uom = item.uom;
					jobOrder_item.conversion_factor = item.conversion_factor;
					jobOrder_item.item_group = item.item_group;
					jobOrder_item.description = item.description;
					jobOrder_item.image = item.image;
					jobOrder_item.qty = item.qty;
					jobOrder_item.warehouse = item.s_warehouse;
					jobOrder_item.required_date = frappe.datetime.nowdate();
				});
				frappe.set_route('Form', 'Job Order', jobOrder.name);
			});
		}, __('Create'));