Create Item using Rest API

Hi,
I wanted to create item on button click using rest api from JS file.

Here is the code,

frappe.ui.form.on('Compound Item', {
    create_items: function(frm) {
		frappe.call({
			url: "/api/resource/Item/",
			type: "post",
			data: JSON.stringify({data:{first_name:"Test Item"}}),
			callback: function(r) {
				//alert();
			}
		});
	}
});

Its not working at all.

Please help me out.

1 Like

Hi, I found the solution and here is the code.

frappe.ui.form.on('Compound Item', {
  var compound_item = frappe.model.get_doc(cdt, cdn);
  frappe.call({
			url: "/api/resource/Item/",
			type: "post",
			args: {
				data: { "item_name" :  compound_item.primary_item_code + '_' + compound_item.primary_item_name }
			},
			callback: function(r) {
				console.log(JSON.stringify(r));
			}
		});

})
1 Like