What is the proper way to create sales order from postman

hi friends I want to create sales order from postman but I don’t know I tried this way but didn’t work

the json format

{
	"customer_name": "Zia",
	"delivery_date": "09-11-2018",
	"address": "Street 15, Karete char",
	"item":{
		"item_code": "0009",
		"item_name": "Pencil",
		"item_group": "Products"
	}
}

I get the fowllowing error

Traceback (most recent call last):
  File "/opt/bench/erpnext/apps/frappe/frappe/app.py", line 66, in application
    response = frappe.api.handle()
  File "/opt/bench/erpnext/apps/frappe/frappe/api.py", line 122, in handle
    "data": frappe.get_doc(data).insert().as_dict()
  File "/opt/bench/erpnext/apps/frappe/frappe/model/document.py", line 222, in insert
    self.run_before_save_methods()
  File "/opt/bench/erpnext/apps/frappe/frappe/model/document.py", line 876, in run_before_save_methods
    self.run_method("validate")
  File "/opt/bench/erpnext/apps/frappe/frappe/model/document.py", line 772, in run_method
    out = Document.hook(fn)(self, *args, **kwargs)
  File "/opt/bench/erpnext/apps/frappe/frappe/model/document.py", line 1048, in composer
    return composed(self, method, *args, **kwargs)
  File "/opt/bench/erpnext/apps/frappe/frappe/model/document.py", line 1031, in runner
    add_to_return_value(self, fn(self, *args, **kwargs))
  File "/opt/bench/erpnext/apps/frappe/frappe/model/document.py", line 766, in 
    fn = lambda self, *args, **kwargs: getattr(self, method)(*args, **kwargs)
  File "/opt/bench/erpnext/apps/erpnext/erpnext/selling/doctype/sales_order/sales_order.py", line 32, in validate
    super(SalesOrder, self).validate()
  File "/opt/bench/erpnext/apps/erpnext/erpnext/controllers/selling_controller.py", line 38, in validate
    super(SellingController, self).validate()
  File "/opt/bench/erpnext/apps/erpnext/erpnext/controllers/stock_controller.py", line 17, in validate
    super(StockController, self).validate()
  File "/opt/bench/erpnext/apps/erpnext/erpnext/controllers/accounts_controller.py", line 61, in validate
    self.set_missing_values(for_validate=True)
  File "/opt/bench/erpnext/apps/erpnext/erpnext/controllers/selling_controller.py", line 52, in set_missing_values
    self.set_price_list_and_item_details(for_validate=for_validate)
  File "/opt/bench/erpnext/apps/erpnext/erpnext/controllers/selling_controller.py", line 80, in set_price_list_and_item_details
    self.set_missing_item_details(for_validate=for_validate)
  File "/opt/bench/erpnext/apps/erpnext/erpnext/controllers/accounts_controller.py", line 229, in set_missing_item_details
    if item.get("item_code"):
AttributeError: 'unicode' object has no attribute 'get'

“items”: [ {"item_code… } ]

2 Likes

Change this item to items.

However I have doubt you can directly enter this into the Sales Order Item childtable. It may require a different type of format.

1 Like

thank you for responding me
sorry to make u confuse; in postman I wrote items but in code its item.
i’m getting this error
AttributeError: 'unicode' object has no attribute 'get'
I don’t know what it means

sorry for my mistake
if you zoom the screen shot in postman I wrote items not item, in case of items i get the above error

you should pass items in json array like [{“item_code”:123,“item_name”:“Test”},{“item_code”:456,“item_name”}] in your screen shot you passed item without [].
And also you should pass data arguments as form_data not query string.

1 Like

when you create Sales Order directly in ERPNext itself; Customer and Items are mandatory, now I write in this way;

 [{
	 "order_type":"Sales",
	 "customer":{
		 "customer_name":"Zia Akbari",
		 "customer_group":"All Customer Groups",
		 "territory":"All Territories"
	 },
	 "items":{
		"item_code":123,
		"item_name":"Pencil",
		"item_group":"All Item Groups",
		"delivery_date":"14-11-2018"
	 }
 }]

in one line

[{"order_type":"Sales","customer":{"customer_name":"Zia Akbari","customer_group":"All Customer Groups","territory":"All Territories"},"items":{"item_code":123,"item_name":"Pencil","item_group":"All Item Groups","delivery_date":"14-11-2018"}}]

I pass as form-data

I get this error

Server Error

Traceback (most recent call last):
  File "/opt/bench/erpnext/apps/frappe/frappe/app.py", line 60, in application
    response = frappe.api.handle()
  File "/opt/bench/erpnext/apps/frappe/frappe/api.py", line 118, in handle
    data.update({
AttributeError: 'list' object has no attribute 'update'
Home

Choose raw instead of form-data and use the string like below:

{
	 "order_type":"Sales",
	 "customer":{
		 "customer_name":"Zia Akbari",
		 "customer_group":"All Customer Groups",
		 "territory":"All Territories"
	 },
	 "items":{
		"item_code":123,
		"item_name":"Pencil",
		"item_group":"All Item Groups",
		"delivery_date":"2018-11-14"
	 }
 }
1 Like

thank you for replying
I get bellow error if send as json format in raw

Not Permitted

You do not have enough permissions to complete the action

Home

Have you logged in your user? Does your user have permission to create Sales Order?

1 Like

yes, logged in as Administrator, I’m able to get Sales Order and create customer and items

I have tested following code, it worked:

{
	 "order_type":"Sales",
	 "customer":"Guest",
	"transaction_date":"2018-11-14",
	 "items":[{
		"item_code":"your exist code item",
		"item_name":"item name",
		"delivery_date":"2018-11-14",
		"qty":1,
		"rate":23
	 }]
 }
1 Like

I have an item in erp its id is 01 and its name is ‘test item name’, i send in like this

{
	"order_type":"Sales",
	"customer":"Guest",
	"transaction_date":"2018-11-14",
	"items":[{
		"item_code":01,
		"item_name":"test item name",
		"delivery_date":"2018-11-14",
		"qty":1,
		"rate":23
	 }]
 }

show me this error

Server Error

Traceback (most recent call last):
  File "/opt/bench/erpnext/apps/frappe/frappe/app.py", line 52, in application
    init_request(request)
  File "/opt/bench/erpnext/apps/frappe/frappe/app.py", line 112, in init_request
    make_form_dict(request)
  File "/opt/bench/erpnext/apps/frappe/frappe/app.py", line 120, in make_form_dict
    args = json.loads(request.data)
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 380, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting , delimiter: line 6 column 11 (char 107)
Home

The error tells you missed a comma, but you don’t.
Try below:

Item_code is string, should be in double or single quote.

{
	"order_type":"Sales",
	"customer":"Guest",
	"transaction_date":"2018-11-14",
	"items":[{
		"item_code":"01",
		"item_name":"test item name",
		"delivery_date":"2018-11-14",
		"qty":1,
		"rate":23
	 }]
 }
1 Like

It worked, but the customer should already be created, can I create Customer at the time of Order creation?

I don’t think you can create customer when Sales Order creation by built-in api.

If you wish to, you have to make your own api which create customer if not exist, before create Sales Order.

1 Like

I’ll try, thank you