Switch Calculation - If Variable don't exist

Post Reply
schmitzdp
Member
Posts: 54
Joined: Thu Aug 06, 2020 1:36 pm

Switch Calculation - If Variable don't exist

Post by schmitzdp »

Hello,

I would like to obtain a calculation with these 2 variables.

[Switch.Calculation:Expression="[Metadata.Text:Dataset="Xml",Model="XML",Path="/xml/Quantity"]*[Metadata.Text:Path="/xml/Version",Dataset="Xml",Model="XML"]"]

Some of our products do not include the "Version" variable because of their characteristics.

If the variable is not found, is there a way or a function to replace it with the value '1' for example ?

Thanks.
User avatar
magnussandstrom
Advanced member
Posts: 510
Joined: Thu Jul 30, 2020 6:34 pm
Location: Sweden
Contact:

Re: Switch Calculation - If Variable don't exist

Post by magnussandstrom »

I had a similar case and solved it by using the app String replace before XML pickup, to set the value to 1 if the XML-field is not populated.

Like this:

<Version></Version>=<Version>1</Version>
<Version/>=<Version>1</Version>

Maybe not the most clean solution but it works.
User avatar
magnussandstrom
Advanced member
Posts: 510
Joined: Thu Jul 30, 2020 6:34 pm
Location: Sweden
Contact:

Re: Switch Calculation - If Variable don't exist

Post by magnussandstrom »

This might be a better solution (especially if the field Version does not exist):

1. Set the Version value as private data:
Version=[Metadata.Text:Dataset="Xml",Model="XML",Path="//Version"]

2. Use the app Private Data Magician to set the value to 1 if no value is present (this will only affect if there is no value):
*** Version
= 1

3. Then calculate with the Private data value instead:
[Switch.Calculation:Expression="[Metadata.Text:Dataset="Xml",Model="XML",Path="/xml/Quantity"]*[Job.PrivateData:Key="Version"]"]
schmitzdp
Member
Posts: 54
Joined: Thu Aug 06, 2020 1:36 pm

Re: Switch Calculation - If Variable don't exist

Post by schmitzdp »

I finally used the app String replace to standardise this data for all my products.

It was a bit tricky but it works.

Thanks magnussandstrom ;)
Post Reply