Simple RPC call won't create Sales Order

Hello there,

I have a simple RPC call, which won’t create a Sales Order. I do not get any errors. After lots of “trial and error” (with no error) the Sales Order is created once. As soon as I delete it and make my RPC Call again, it won’t work again.

My RPC call returns success. The executed code is the following:

try:
	item = frappe.get_doc({
		"doctype": "Sales Order",
		"customer": customer.name,
		"transaction_date": date_purchased,
		"delivery_date": delivery_date,
		"currency": data['currency_code'],
		"price_list": "Standard-Shop",
		"price_list_currency": data['currency_code'],
		"territory": territory.name,
		"customer_group": customer_group.name,
		"customer_address": customer_address.name,
		"fiscal_year": fiscal_year,
		"source": "Shop",
		"company": "Shop.de",
		"order_type": "Sales",
		"ignore_pricing_rule": 1,
		"apply_discount_on": "Net Total",
		"items": get_items_list(data['items'], data['totals']),
		"taxes": get_taxes_list(data['items'], data['totals']),
		"ext_id": int(data['orders_id']),
		"ci_order_status": frappe.db.get_value("CI Order Status", {"external_id": str(data['orders_status'])}, "name"),
		"ci_payment_method": ci_payment_method
	})

	status = switch_order_status_actions(data['orders_status'])
	item.status = status
	feedback = item.insert(True)

	msg("Success")
except Exception as e:
	msg("No success...")
	msg(repr(e))

The code works properly importing thousands of orders in a loop and fails as described above importing one single, the latest, order, when using it by RPC.

Does anyone have a clue? Thank you in advance.

@ci2016, do you can share the error?

As you can see in my code, I use try: … except Exception as e: …

My RPC call always returns “success”, so there is no error.

@ci2016m if have no errors, what is the motive of your question?

@max_morais_dmm The sales order is not being created with no error.

The issue is: Do you tryied frappe.new_doc, instead of frappe.get_doc?

This did not work, because frappe.new_doc does not accept a dictionary. My code works in a loop from testing console and by calling the function via jQuery-Button in the Frappé frontend.

It just don’t work by calling it via RPC / API.

@ci2016

d = frappe.new_doc("Doc")
d.update({dict})

@max_morais_dmm I tried you suggestion. Still the same problem. My code works in normal execution, but not via RPC call. I receive “success” with no errors, but no Sales Order is created.

May there be any problem with the RPC?

show the “rpc” context, and how do you are calling it!

@max_morais_dmm Of course.

This is my whitelisted function.

@frappe.whitelist(allow_guest=True)
def get_latest_order():
    frappe.set_user("Administrator")

    try:
	feedback = run_get_order()
    except Exception as e:
	return msg(str(repr(e)))

return feedback

run_get_order is importing a json file and passes it to my frappe.get_doc({}) code.

I call it via browser with full path through my module.

http://localhost:8000/api/method/module_name.module_name.doctype.get_order.get_order.get_latest_order

The link returns

{“message”:“success”}

@ci2016 add frappe.db.commit() after feedback = run_get_order()

2 Likes

@max_morais_dmm Great! That worked! But why do I need it in this case? Is it because of the RPC?

@ci2016, yes, when do you define your own function to manage the data, is your responsability determine when commit or when roolback the database!

1 Like

@max_morais_dmm I understand. I will use it, when using the RPC. I thought it would do it itself. How may I thank you for your help?

@ci2016 to me is a pleasure help others users here! To thank me, just help others when you can!

1 Like

@max_morais_dmm Thank you. I will have to go deeper in Frappé / ERPNext, but I think I am headed in the right direction.

@ci2016 Frappe will automatically commit for POST queries.

GET queries are supposed to be for reading.

1 Like