Hi, I made a web hook.
It is correctly communicating with the external app.
I want to get the response information from the webhook response and use it in one of sales invoice. How do I implement that
Hi, I made a web hook.
It is correctly communicating with the external app.
I want to get the response information from the webhook response and use it in one of sales invoice. How do I implement that
Webhook is mainly for notifying about events. To send a request and process the response, try using a Server Script.
For API calls, use the following helpers:
frappe.make_get_requestfrappe.make_post_requestfrappe.make_put_requestfrappe.make_patch_requestfrappe.make_delete_requestIt can also access common APIs like frappe.get_doc, frappe.new_doc, frappe.get_list, frappe.get_all, frappe.db.get_value, frappe.db.set_value, frappe.throw, frappe.log_error, frappe.enqueue, frappe.call, frappe.utils.*, json.loads, and json.dumps.
Use a “DocType Event” Server Script on Sales Invoice, event “Before Submit”. This lets the script update the triggering doc directly before it is submitted.
Example: call an external invoice-risk API using the Sales Invoice fields Customer (customer), Currency (currency), and Grand Total (grand_total), then write the response into custom fields like External Risk Status (external_risk_status), External Risk Score (external_risk_score), and External Checked At (external_checked_at).
if not doc.customer:
frappe.throw("Customer is required before checking invoice risk")
response = frappe.make_post_request(
"https://api.example.com/invoice-risk",
headers={
"Authorization": "Bearer YOUR_API_TOKEN",
"Accept": "application/json",
},
json={
"invoice": doc.name,
"customer": doc.customer,
"currency": doc.currency,
"grand_total": doc.grand_total,
},
)
risk_status = response.get("status")
risk_score = response.get("score")
if risk_status == "blocked":
frappe.throw("External risk service blocked this Sales Invoice")
doc.external_risk_status = risk_status
doc.external_risk_score = risk_score
doc.external_checked_at = frappe.utils.now()
Thank you, for the help. I managed to get the response data into the tables!!