getVariableAsString() returning undefined
Posted: Thu Apr 09, 2026 6:16 pm
Hello all,
I'm running into a problem and was wondering if I could get some guidance.
I am receiving an order via a web to print system. I am attempting to extract delivery information from it's XML.
Sometimes the customer does not include a company name, in which case we just default to a hard coded string.
Here is a very basic example of where the company name would be situated in the XML:
I have written this script expression to get the company name and if it's empty add in the hard coded name instead:
I'd expect in this case for the result of companyString to be 'Company Name', but what I am actually getting is the string 'undefined'
If I interrogate the result of company directly, I can see that it is returning the string 'undefined' and I'm not really sure as to why.
The manual doesn't mention returning undefined upon failure. I've also checked the logic of in Inspect jobs and can't find anything untoward there either. I've also checked the xpath in an external program and the path matches what I'd expect.
On top of this, I'm sure I've used this pattern in other flows without this issue presenting itself.
Can anyone shed some light on why this is happening?
Thanks in advance,
Dan
I'm running into a problem and was wondering if I could get some guidance.
I am receiving an order via a web to print system. I am attempting to extract delivery information from it's XML.
Sometimes the customer does not include a company name, in which case we just default to a hard coded string.
Here is a very basic example of where the company name would be situated in the XML:
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<Order>
<ShippingAddress>
<CompanyName></CompanyName>
</ShippingAddress>
</Order>Code: Select all
async function calculateScriptExpression(s: Switch, flowElement: FlowElement, job: Job): Promise<string> {
const company = await job.getVariableAsString('[Metadata.Text:Dataset="XML",Model="XML",Path="//Order/ShippingAddress/CompanyName",Space="trim"]')
const companyString = (company.length ? company : 'Company Name')
return companyString
}If I interrogate the result of company directly, I can see that it is returning the string 'undefined' and I'm not really sure as to why.
The manual doesn't mention returning undefined upon failure. I've also checked the logic of
Code: Select all
[Metadata.Text:Dataset="XML",Model="XML",Path="//Order/ShippingAddress/CompanyName",Space="trim"]On top of this, I'm sure I've used this pattern in other flows without this issue presenting itself.
Can anyone shed some light on why this is happening?
Thanks in advance,
Dan