使用NetSuite PHP API无法向新建销售订单添加商品
使用带命名空间和自动加载的NetSuite PHPToolKit(Ryan Winchester开发的NetSuite PHP API客户端)时,尝试给新建销售订单添加商品触发类型错误,具体代码及错误信息如下:
构建商品数组代码
$items = array(); foreach ($order->basketDetails->basketItems as $item) { $cartItem = new SalesOrderItem(); $cartItem->item = $itemResponse->searchResult->recordList->record[0]->internalId; $cartItem->quantity = $item->productQuantity; $cartItem->rate = "42.97"; $items[] = $cartItem; }
创建SalesOrderItemList代码
$itemList = new SalesOrderItemList(); $itemList->item = $items;
关联到销售订单代码
$sale->itemList->item = $itemList;
触发的错误信息
[04-Apr-2023 15:11:52 UTC] PHP Fatal error: Uncaught SoapFault exception: [soapenv:Server.userException] org.xml.sax.SAXException: item on {urn:sales_2021_1.transactions.webservices.netsuite.com}SalesOrderItemList must be of type {urn:sales_2021_1.transactions.webservices.netsuite.com}SalesOrderItem in /var/www/composer/vendor/ryanwinchester/netsuite-php/src/NetSuiteClient.php:196
解决方案
修正销售订单与itemList的关联逻辑
错误代码将SalesOrderItemList对象赋值给了$sale->itemList->item,但item字段需要的是SalesOrderItem数组,而$sale->itemList本身应该接收SalesOrderItemList对象。正确代码:$sale->itemList = $itemList;确保使用正确的命名空间
必须带上NetSuite的命名空间实例化类,否则创建的对象并非API要求的类型:$cartItem = new \NetSuite\Classes\SalesOrderItem(); $itemList = new \NetSuite\Classes\SalesOrderItemList();添加replaceAll属性(可选但推荐)
NetSuite API要求SalesOrderItemList需明确设置replaceAll属性,避免潜在的同步问题:$itemList->replaceAll = true;
内容的提问来源于stack exchange,提问作者John

