Create work orders from sales order with API

I am attempting to create Work Orders programmatically using the rest-api. I am using the white listed function, make_work_orders in sales_order.py.
This function’s first parameter is called “items”. Tracing through the javascript that ERPNext uses to call this function… It looks like items are generated in the non-whitelisted function in sales_order.py called get_work_order_items.
Does anyone know how to go about creating work orders from a sales order using the rest-api without custom scripting or changes to the source code?

After a bit of digging I’ve found that it looks like I can compose the “items” object myself from the bom_item and sales_order_item doctypes for the given item_id, but it seems that I would just be duplicating the efforts of the get_work_order_items function. Is there a reason that function is not whitelisted? My original question stands as far as doing this with simple API calls without manipulating JSON myself… I don’t mind doing so if that’s the only way, but I’m attempting to make my interactions with ERPNext as simple as possible for continued maintenance of code.

This command will generate, from a Sales Order, the JSON structure to pass into a Sales Invoice POST instruction.

curl --silent --location --request POST 'https://dev.yourpublic.work/api/method/erpnext.selling.doctype.sales_order.sales_order.make_sales_invoice' \
--header 'Authorization: token ec226d15b52879e:ca6deade2ff3d0e' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'source_name=SAL-ORD-2020-00001'

If what you want to do is actually possible then I expect if you search the Sales Order source code you will find a whitelisted function similar to :

erpnext.selling.doctype.sales_order.sales_order.make_sales_invoice

Hi Martin. You helped me with creating a sales invoice from a sales order in the past. in How to create invoice from sales order using API Which was super helpful.

But Not sure if you are suggesting that code similar to that would help me create a work order. Or if you missed that I’m trying to create a work order from a sales order.

I’m suggesting you look for something like:

erpnext.selling.doctype.sales_order.sales_order.make_work_order

Try here:

Thank you Martin. I did find the function and was already using it. Just a matter of passing it the arguments it wants. The first one is an array of objects representing the items. Which it looks like I need to create semi manually out of JSON representations of BOMS and sales_order_items. Which is fine. Just wondering if there was a specific path I was missing.

AHA!

Line 158

Yes, it’s that first parameter “items” which is the crux of my question.

Oh I see I misread your question. Duh.

No problem.

It probably wasn’t very clear. I’m not great at writing concise clear questions. But I appreciate your help.

I’ll go about constructing the objects using jq. Which was a super helpful recommendation for a Linux tool that you gave me in our previous exchange.

You mentioned using, get_work_order_items but not:

.... sales_order.make_work_orders(items, sales_order, company, project=None)

You have tried both?

get work order items is not whitelisted. My understanding is I can’t call non-whitelisted functions using the rest-api.

Yeah! jq is insane. You could spend months solving jq “puzzles” and learn not even half of what it can do.