Create Document with a API Method + Custom Scripts

Hi,

I’m new to Frappe / ERPNext.

I’ve create a custom script that makes some math and set some values in fields automatically.
It makes this when a document is submitted, via UI, but it don’t seem to work when I use the API to create the document.

Can someone tell me is there’s a way to use the REST API methods to create a document and also run the changes that the custom script do? Does it work only in the UI?

Thanks,

Rafael H Chacon

you can insert document using API call in javascript as below:

var doc = {
	"doctype" : 'Customer',
	"customer_name"  :   'Customer 1',
	'customer_group' :  'All Customer Groups',
	'territory' : 'All Territories',
	'customer_type' : 'Company'

}; 
frappe.call({
	method: "frappe.client.insert",
	args: {"doc": doc}, // use JSON.parse(JSON.stringify(doc)) for parsing to json object
	callback: function(r) {
		if(r.exc) {
			msgprint(__("There were errors."));
		} else {
			msgprint(__("Document inserted."));
		}
	}
})
1 Like

Thanks for replying @sanjay.

I guess this may work in other cases, but mine is a little too specific, you see, I have a whole other system that will consume data from my custom app in frappe, and when it does, I want to create a document with the data that was in that document at that time, plus with some others that will be calculated in the moment according with some of the data.

I already made a workaround for this issue by using first a POST to create the document, then in the other system I make the decisions and all, and finally then I call a PUT request to update the fields with the information I needed. I just wish I could do this in one go… Just call a POST request, already changing the document status to 2, and the Custom Script would run and make everything in one go.

But it was just a shot in dark, because being a the Custom Scripts, basically, a javascript, I had little hopes for this to be possible.

Anyway… Thanks for the reply!

Best regards!

Rafael H Chacon

Hi, did you find a solution

If you need the operation to be purely client side, OP has already described the solution. If you can use Server Scripts, the process can be much simpler.