I am executing this exact POST, several times, over and over.
Expected Results:
First POST should succeed.
Second POST should fail. Because it is a duplicate Customer (same name as previous, which is the primary key)
Actual Results:
First POST succeeds. But the name is not āmy_customer_idā. ERPNext writes āAcme Corpā as the name
Second POST succeeds. It uses a new name = āAcme Corp 2ā
Third POST succeeds. It uses a new name = āAcme Corp 3ā
Question
Can anyone see where Iāve made a mistake? I would like the REST API to respect the name I am providing. So that duplicate Customers are not created by accident.
Iām assuming Iāve made some kind of silly mistake.
That would solve the REST problem. But introduce another. I donāt want Users manually choosing the name field of Customers, when they do data-entry in the web browser.
I like the auto-generated Naming Series:
CUST-2021-0001
CUST-2021-0002
CUST-2021-0003
However, Iām concerned about the API caller making a mistake. Instead of creating 1 new customer named āAcme Corpā (with an auto-generated name) it might 2 new customers. Or 3. Or 100 new ones?
I did a partial hack. I added code that forbids anyone from calling POST, and in the payload, including a ānameā value that already exists. Screenshot below. Method is handle() within the file āapi.pyā
Supplier and Customer are special cases. Not only do they have āNaming Seriesā, but they have special controls in āSelling Settingsā and āBuying Settingsā.
Screenshot below of āBuying Settingsā, which applies to your case. Suppliers can either get their name from āSupplier Nameā or āNaming Seriesā. This will override anything you pass in an API call.
I am not sure, if you reached the goal with it, to define the name from the API call? (Well, I am understanding the exception, when there is a duplicate), but name is anyway a primary key, isnāt it?
When using the Import interface, there is no problem to define name and I just expected the REST API to behave the same wayā¦ but is there a trick, to define the name value?
Sorry, if I am missing it from your snippet .
I just have had a look at the current codeā¦ I am not very farmiliar with frappe/python yet, but when I refer the comment from get_doc
It seems, that the API is just not providing the 2nd parameter as a name (if it would have been given)
Sure, the property name will be in data, but probably the ORM wont respect this by default, if not explicitely given.
Youāre correct; name is a primary key. However, it was being ignored in the API call.
So I wanted a way to tell the Caller: āHi, you are passing a ānameā in a POST, but that name already exists. Donāt do that. Why did you think this ānameā is available?ā
This forced the caller to rethink what they were doing. Otherwise, without this code, the POST succeeded. And the Caller might never realize the name they passed was ignored.
This was just a temporary fix, to prevent my API callers from doing strange things.
What youāre doing, -setting- the name value during a call? Thatās awesome. That is what Iād hoped to figure out later, when I had time to return to this issue.
Very interested in trying your __newname solution.
Yea, I think the __newname property is exactly what you were looking for, when I interprete this correctly
At least it was exactly what I was looking for ā¦ and thinking that I understood you the same way
I also tried the API Call with Supplier, which worked pretty well (as expected, but now proven)ā¦ so it will (imho) work for the most DocTypes (at least those, which donāt have an explicit coding to avoid that).