getVariableAsString() returning undefined

Post Reply
DtM
Member
Posts: 36
Joined: Tue Aug 04, 2015 3:02 pm

getVariableAsString() returning undefined

Post by DtM »

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:

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<Order>
  <ShippingAddress>
    <CompanyName></CompanyName>
  </ShippingAddress>
</Order>
I have written this script expression to get the company name and if it's empty add in the hard coded name instead:

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
}
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

Code: Select all

[Metadata.Text:Dataset="XML",Model="XML",Path="//Order/ShippingAddress/CompanyName",Space="trim"]
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
User avatar
samw
Newbie
Posts: 8
Joined: Fri Jan 09, 2026 3:31 pm
Location: UK
Contact:

Re: getVariableAsString() returning undefined

Post by samw »

Hi Dan,

Thanks for bringing this to our attention.

I just checked the implementation of Job.getVariableAsString() on the Switch side, and the method is simply returning the value coerced to a string, i.e:

Code: Select all

return String(value)
This unfortunately means that if the value is undefined, you will get the string "undefined", which I would consider a bug.

Please could you log a Support case for this, including a link to this post? That way we can link our internal ticket and notify you when it's resolved.
DtM
Member
Posts: 36
Joined: Tue Aug 04, 2015 3:02 pm

Re: getVariableAsString() returning undefined

Post by DtM »

Hi Sam,

Thanks for getting back to me.

I'll raise this with support now.
Post Reply