Send Order Status to ERP
You'll now send order status information back to ERP to complete the bi-directional flow.
ERP has provided an endpoint called sendOrderInfo and requested that the payload is structured as:
{
"order": {
"workOrderName": "260526-001",
"itemName": "SC-2024",
"reqdQty": 30000,
"actualQty": 28500
}
}
In a real project, the ERP team gives you the URL of this endpoint on their system. For this training, the ERP simulator you installed in Get Orders from ERP provides it at http://localhost:8088/system/webdev/kanoaERPSim/api/sendOrderInfo. You call it from MES with Ignition's system.net.httpPost() function.
- Open the ERP simulator in a browser so you can see what it receives.
- Copy the following code block into the Script Console in the Ignition Designer. It doesn't matter from which project.
- Set
GATEWAYto the address you open the gateway with in your browser, then run it.
The Script Console runs on your computer, not on the gateway, so it needs the address your computer uses to reach the gateway: the one in your browser's address bar, such as http://localhost:8088 or https://kanoa-dev.localtest.me. If your gateway runs in Docker or on another machine, localhost:8088 fails with Connection refused.
Scripts that run on the gateway itself, like the simulator calling sendMO or a gateway event script calling ERP, use http://localhost:8088.
Script Console code
# The address you open the gateway with in your browser, e.g. 'http://localhost:8088'
GATEWAY = 'http://localhost:8088'
def sendOrderUpdateToERP(orderInfo):
url = GATEWAY + "/system/webdev/kanoaERPSim/api/sendOrderInfo"
payload = {'order': orderInfo}
try:
resp = system.net.httpPost(url, postData=system.util.jsonEncode(payload), contentType="application/json")
except:
# Turn the common failures into something you can act on.
import sys
error = str(sys.exc_info()[1])
if 'Connection refused' in error:
return 'Could not connect to %s. Set GATEWAY to the address you open the gateway with in your browser.' % GATEWAY
if 'code: 402' in error:
return 'The WebDev trial has expired. On the Gateway web page, click Reset Trial, then run this again.'
if 'code: 404' in error:
return 'Nothing at %s. Check that the ERP simulator project is installed and named kanoaERPSim.' % url
return 'Request failed: %s' % error
return system.util.jsonDecode(resp)
# Send only the fields ERP asked for, not the whole Kanoa production order.
orderInfo = {
"workOrderName": "260526-001",
"itemName": "SC-2024",
"reqdQty": 30000,
"actualQty": 28500
}
print sendOrderUpdateToERP(orderInfo)
The Script Console prints the simulator's reply, {"ok": true, ...}, and the ERP simulator opens a popup showing the order it received. If the call fails, it prints what went wrong instead, such as an expired WebDev trial (HTTP 402) or a gateway address it can't reach.