[Tutorial] Push changes to another document

An example script showing how to push changes from one doc to another.

6 Likes

Hey thanks @cpurbaugh. Script works perfectly!

Had one doubt, How to trigger the function on document save event, instead of calling it on update button?

Update: Got Through, called below function on “validate” event instead of refresh.

    function() {
        frappe.call({
            "method": "frappe.client.set_value",
            "args": {
                //replace "Target DocType" with the actual target doctype
                "doctype": "Target DocType",
                //replace target_doctype_link with a link to the document to be changed
                "name": frm.doc.target_doctype_link,
                "fieldname": {
                    //You can update as many fields as you want.  
                    "target_field_1": frm.doc.source_field_1,
                    "target_field_2": frm.doc.source_field_2,
                    "target_field_3": frm.doc.source_field_3,
                    "target_field_4": frm.doc.source_field_4,
                    "target_field_5": frm.doc.source_field_5  //Make sure that you do not put a comma over the last value
                },
            }
        });
});
3 Likes

Glad it helped!

Heyy @cpurbaugh

I’m trying that, I completed the code you gave us, but I missing the target doctype link, I’m new on this, and a really want to know how should the link be?

it should be a link field in your source. You can use customize form to find link fields or create your own.

@cpurbaugh very helpful code indeed, i was using something like that but i needed it to upon submit go create and autofill a doc then save it without waiting for me to manually save it, maybe it can be done in the background.
p.s. i can only use custom script, cant access any files or folders other than the simple client side editing.

thank you in advance

Edit:
and i also wanted to ask can i use this method to add rows and fill them in a child table?


Best regards

1 Like

Dear @cpurbaugh

I’m tried to using the script to update field value in child table in quotation item from sales order item put not working

frappe.ui.form.on("Sales Order Item", {
	refresh: function(frm) {
		frm.add_custom_button(__("Update"),
			function() {
				frappe.call({
					"method": "frappe.client.set_value",
					"args": {
						"Sales Order Item": "Quotation Item",
						"name": frm.doc.prevdoc_docname,
						"stage": {  
							"stage": frm.doc.stage
						},
					}
				});
		});
}
});

[Newbie alert!] What is target_doctype_link in the above code? Target DocType name?

1 Like

in the source document, there would be a link to the target document. target_doctype_link would be that field’s name.

1 Like

I’m getting the following error, I checked everything.

Traceback (most recent call last):
  File "/home/frappe/frappe-bench/apps/frappe/frappe/app.py", line 61, in application
    response = frappe.handler.handle()
  File "/home/frappe/frappe-bench/apps/frappe/frappe/handler.py", line 21, in handle
    data = execute_cmd(cmd)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/handler.py", line 56, in execute_cmd
    return frappe.call(method, **frappe.form_dict)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 1036, in call
    return fn(*args, **newargs)
TypeError: set_value() takes at least 3 arguments (2 given)

@rk_root
can you share your code? There should be 3 args: doctype, docname, and fieldname (which in the example is several fields)

//Replace "DocType" with the source DocType
frappe.ui.form.on("Sales Contract", {
  //The trigger can be changed, but refresh must be used to use a button
  refresh: function(frm) {
    //The following line creates a button.
    frm.add_custom_button(__("Update"),
      //The function below is triggered when the button is pressed.
      function() {
        frappe.call({
          "method": "frappe.client.set_value",
          "args": {
            //replace "Target DocType" with the actual target doctype
            "doctype": "Purchase Contract",
            //replace target_doctype_link with a link to the document to be changed
            "name": frm.doc.get_purchase_contract,
            "fieldname": {
              //You can update as many fields as you want.  
              "contract_date": frm.doc.contract_date,
              "entity": frm.doc.entity,
              "quantity": frm.doc.quantity,
              "rate": frm.doc.rate,
              "commodity": frm.doc.commodity //Make sure that you do not put a comma over the last value

            },

          }
        });
    });
  }
});

@rk_root- Thank you for sharing this code.

Do you code to when the changes include a child doctype?

No, I just code for main table only.

@rk_root- OK, thank you.

is it possible to copy child tables?

Yes, I’m quite sure it is.

The frappe framework can handle many requirements.

One simply needs to be knowledgeable on the subject

1 Like

That’s true :stuck_out_tongue:
I’m trying…

@cpurbaugh To update child table I tried the following code, It’s partially working but data not pushed. any suggestions.

"cost_sheet": [{
                    "cost_code": frm.doc.cost_code,
                    "cost_amt": frm.doc.cost_amt
                }]

There are examples of scripts that deal with child tables here: Community Developed Custom Scripts · frappe/erpnext Wiki · GitHub

1 Like