How to add new Item using custom scripts?

Is there a way i could create a clone of current item being added, update few fields and add a new item through custom script?

there is a method called, frappe.new_doc is this to create a new doctype? or can i use it to add an entry to my existing doctype “item”.

Update
i am using the below code to acheive the above
cur_frm.cscript.on_submit = function(frm) {
var newdoc = frappe.new_doc(“Item”);

newdoc.item_code = “testing”;
newdoc.item_group = “Mobiles”;
newdoc.flag.ignore_mandatory = True;
newdoc.insert();
}
but i am getting the following error in the console :
TypeError: newdoc is undefined

Update
I just tried printing newdoc using
newdoc=frappe.new_doc(“Item”);
msgprint(newdoc);
I see the message undefined. y is it undefined?
Any resolutions?
Thanks

Any suggestions??

Yes it is used to create new document. But if you have to add any child table entry, then you can try,

frm.add_child(table_fieldname)

e.g.

var d = frm.add_child("items");
d.item_code = "testing"
d.item_group = "Mobiles"
frm.refresh_fields("items")

OR

frappe.model.add_child(cur_frm.doc, "table_doctype", "table_fieldname");

1 Like

Hi @Sangram
I used the following code
cur_frm.cscript.on_submit = function(frm) {
var d = frm.add_child(“Items”)
d.item_code = “testing”
d.item_group = “Mobiles”
frm.refresh_fields(“Items”)
}
But i got this error “TypeError: frm.add_child is not a function”

Thanks

check field name. It should be items not Items

Hi @Sangram Still getting the same error.

P.S :Items is my doctype. Into which i want to add a new item using custom script

It’s confusing.
Items is doctype or table?

if it is a doctype then use frappe.new_doc()

Items is a doctype.

@Sangram Yes i did that before. and as mentioned before

cur_frm.cscript.on_submit = function(frm) {
var newdoc = frappe.new_doc(“Item”);
msgprint(newdoc);
}

I am getting the following error in the console :
TypeError: newdoc is undefined

write it in server side (.py file) using frappe.call or Hooks

e.g.

https://github.com/frappe/erpnext/blob/develop/erpnext/accounts/doctype/sales_invoice/pos.py#L452-L463