How can I add a new node to the XML document? I ha...
# suitescript
n
How can I add a new node to the XML document? I have tried this: xmlDocument.createElement({tagName: 'tranSales:orderLine'});
b
you need to share more about what you are trying and what error you are getting
n
This is the XML: <record xsi:type="tranSales:ItemFulfillment" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tranSales="urn:sales_2019_1.transactions.webservices.netsuite.com" xmlns:platformCore="urn:core_2019_1.platform.webservices.netsuite.com" xmlns:platformCommon="urn:common_2019_1.platform.webservices.netsuite.com"> <tranSales:createdFrom internalId="-1"> <platformCorename&gt;SO36161&lt;/platformCorename> </tranSales:createdFrom> <tranSalesshippedDate&gt;2022 05 13T100000Z&lt;/tranSalesshippedDate> <tranSalesshipStatus&gt; shipped&lt;/tranSalesshipStatus> <tranSales:itemList> <tranSales:item> <tranSalesitemReceive&gt;true&lt;/tranSalesitemReceive> <tranSalesquantity&gt;10.0&lt;/tranSalesquantity> <tranSales:inventoryDetail> <platformCommon:inventoryAssignmentList> <platformCommon:inventoryAssignment> <platformCommon:issueInventoryNumber internalId="33096"> </platformCommon:issueInventoryNumber> <platformCommonquantity&gt;10.0&lt;/platformCommonquantity> </platformCommon:inventoryAssignment> </platformCommon:inventoryAssignmentList> </tranSales:inventoryDetail> <tranSales:serialNumbers>136713/10/2021(10.0)</tranSales:serialNumbers> <tranSales:item internalId="1367"> </tranSales:item> <tranSalesorderLine&gt;1&lt;/tranSalesorderLine> <tranSales:customFieldList> <platformCore:customField xsi:type="platformCore:StringCustomFieldRef" scriptId="custcol_sps_lotnumbers"> <platformCore:value>136713/10/2021(10.0)</platformCore:value> </platformCore:customField> <platformCore:customField xsi:type="platformCore:StringCustomFieldRef" scriptId="custcol_sps_actualqty"> <platformCorevalue&gt;10.0&lt;/platformCorevalue> </platformCore:customField> </tranSales:customFieldList> </tranSales:item> </tranSales:itemList> <tranSales:customFieldList> <platformCore:customField xsi:type="platformCore:StringCustomFieldRef" scriptId="custbody_sps_carrierpronumber"> <platformCorevalue&gt;SO36161&lt;/platformCorevalue> </platformCore:customField> </tranSales:customFieldList> </record> I want to add transales:orderLine attribute to the tranSales:itemList I have tried this so far:
Copy code
var newPriceNode = xmlDocument.createElement("tranSales:orderLine");
var newPriceNodeValue = xmlDocument.createTextNode(lineNumber);
newPriceNode.appendChild(newPriceNodeValue);
itemElements[i].appendChild(newPriceNode);
But it throws an unexpected error.
b
still not enough
i would not be able to reproduce the error on my side
you didnt share what is
itemElements[i]
the obvious choice of the item element is is questionable given that there is already an orderLine
n
there is orderline for this XML, but there are other similar xml which don't have this
itemElements is
Copy code
var itemElements = xml.XPath.select({
   node: xmlDocument,
   xpath: 'record/tranSales:itemList/tranSales:item'
});
When I try to conver the xmlDocument to string, it throws unexpected error.
b
Use Document.createElementNS when there are namespaces involved
n
tranSales is the namespace?
I have tried this on the client side and it worked. But MR throws unexpected error.
b
N/xml is different client side and serverside
expect to learn how xml namespaces work
tranSales is the prefix, not the namespace itself
n
@battk I revised the code to this:
Copy code
var newOrderLineNode = xmlDocument.createElementNS({
   namespaceURI: 'urn:<http://sales_2019_1.transactions.webservices.netsuite.com|sales_2019_1.transactions.webservices.netsuite.com>',
   qualifiedName: 'tranSales:orderLine'
});
var newOrderLineNodeValue = xmlDocument.createTextNode(lineNumber);
newOrderLineNode.appendChild(newOrderLineNodeValue);
itemElements[i].appendChild(newOrderLineNode);
Now I am getting the error: "message":"HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted."
Changed it to this:
Copy code
var newOrderLineNode = xmlDocument.createElementNS({
   namespaceURI: 'urn:<http://sales_2019_1.transactions.webservices.netsuite.com|sales_2019_1.transactions.webservices.netsuite.com>',
   qualifiedName: 'orderLine'
});
var newOrderLineNodeValue = xmlDocument.createTextNode(lineNumber);
newOrderLineNode.appendChild(newOrderLineNodeValue);
itemElements[i].appendChild(newOrderLineNode);
Issue seems to have been resolved.
orderLine is same as the line number of the item on the order? @battk
Thanks for your help @battk with the orderLine element issue.