Add new item row in sales invoice on update through restful api

Hi,
I want to update item row in sales invoice through Restful Api. When I was update, previous item was replace.
Request is:
sales_invoice = curl -H “Content-Type: application/json” -X PUT -d ‘{“items” : [{“qty”: “1”, “rate”: 1000.0, “item_code”: “plate”, “docstatus”: 1 }], “update_stock”: 1, “naming”: “00056”, “docstatus”: 1, “submit_on_creation”:“1”}’ http://127.1.1.214/api/resource/Sales%20Invoice/00056 -b d.cookie

Need to help please.

Hi @Nitin_Rakesh

Can you please explain your use case.

When I use the rest api, I recreate the entire object with existing items and the changed ones. So basicaaly I recreat the doctype with the data it should have and then do the update on the doctype usings its ‘name’ field.

regards

yes because you are updating it with new.

Better way create a custom method to append new row and use this method as API.

Hi @Sangram,
Actually I need to pass multiple items in sales invoice. I stored all items in a string like this -
str = “"items" : [{"qty": "1", "rate": 1699.0, "item_code": "CC_112017_01X2", "docstatus": 1},{"qty": "1", "rate": 399.0, "item_code": "Coaster_002", "docstatus": 1}]”

when I passed this string through API its gave error: ValueError: Expecting : delimiter: line 1 column 365 (char 364)\

If I removed double quotes in string and pass then it worked. But how to remove double quotes in string in ruby.

Also I tried to store items in Array or Array convert into json. json also have double quotes.
Request with pass multiple items:
sales_invoice = curl -H "Content-Type: application/json" -X POST -d '{ "taxes_and_charges": "Out of State GST", "taxes": [{ "doctype": "Sales Taxes and Charges", "rate": 18.00, "charge_type": "On Net Total", "account_head": "IGST - CDPL", "description": "IGST @ 18.0", "parenttype": "Sales Invoice", "parentfield": "taxes", "included_in_print_rate": 1 }], "due_date": "20180116", "debit_to": "Debtors - CDPL", "customer": "Nitin", "#{str}" , "do**cstatus": 1, "customer_name": "Nitin","update_stock": 1,"remarks": "0111010", "submit_on_creation":"1", "currency":"INR","payments":[{ "posting_date": "20180116", "payment_type": "Receive", "mode_of_payment": "Cash", "paid_amount": "2988" }], "paid_amount": "2988", "status": "Submitted" }' http://xxx.xxx.xxx.xxx/api/resource/Sales%20Invoice -b d.cookie

from where you get a string or why are you storing it as the string?

Create dictionary and pass as it is.

Because of add multiple items in a sales invoice.

use the dictionary instead of String or convert it to the expected format.

Also I used dictionary.
arr = []
str_hash = Hash.new
str_hash2 = Hash.new
shipment.line_items.each do |item|
str_hash = {“qty”: “#{item.quantity}”, “rate”: item.price.to_f, “item_code”: “#{item.variant.sku}”, “docstatus”: 1 }
arr << str_hash
end
Output is:
arr = [{:qty=>“1”, :rate=>1699.0, :item_code=>“CC_112017_01X2”, :docstatus=>1}, {:qty=>“1”, :rate=>399.0, :item_code=>“Coaster_002”, :docstatus=>1}]

And I passed arr in request:
sales_invoice = curl -H "Content-Type: application/json" -X POST -d '{ "taxes_and_charges": "Out of State GST", "taxes": [{ "doctype": "Sales Taxes and Charges", "rate": 18.00, "charge_type": "On Net Total", "account_head": "IGST - CDPL", "description": "IGST @ 18.0", "parenttype": "Sales Invoice", "parentfield": "taxes", "included_in_print_rate": 1 }], "due_date": "#{due_date}", "debit_to": "Debtors - CDPL", "customer": "Nitin", "items": #{arr} , "docstatus": 1, "customer_name": "Nitin","update_stock": 1,"remarks": "#{invoice_number}", "submit_on_creation":"1", "currency":"INR", "discount_amount": '#{total_discount},'"payments":[{ "posting_date": "#{due_date}", "payment_type": "Receive", "mode_of_payment": "Cash", "paid_amount": "#{invoice_total_amount}" }], "paid_amount": "#{invoice_total_amount}", "status": "Submitted" }' http://xxx.xxx.xx.xxx/api/resource/Sales%20Invoice -b d.cookie