Xporter : How to iterate over a multi-element custom field

xavierhd April 4, 2018

Hi,

I'm trying to iterate a custom field containing multiple value.

The field is provided by PowerDatabaseFields PRO.

I tried the following without success, it print nothing:

#{for ${CustomFieldName[11300]}}
${(${CustomFieldName[11300]})[n]}
#{end}

 and

#{for ${CustomFieldName[11300]}}
${(${CustomFieldName[11300]})[n].Name}
#{end}

 I also tested printing Labels field and compare it to the custom field which have similar behavior:

${CustomFieldName[11300]}
${Labels}

//print
EFR45590S###EFR47012###EPR-5590S###KSS-67115-DE###KWS-21100
R&D Symix addon warning

1 answer

1 vote
xavierhd April 4, 2018

I made a super ugly workaround that does what i expected from

#{for ${CustomFieldName[11300]}}
${(${CustomFieldName[11300]})[n]}
#{end}

but that does not support advanced formating which look like:

%{
var re = /regexSelector/g;
var match, outstring = "";
while(match = re.exec('${CustomFieldName[11300]}'))
{
outstring += match[1] + "\n";
}
outstring
}

If you use this, remember to uglify it to a oneliner.

Also, if you know a more legit solution please share since this is less than ideal!

Santiago Estel April 23, 2020

I tried your solution, but it did not work in Word. - Xporter is just printing the entire code.

xavierhd April 24, 2020

Take note that this was working with Word in Xporter V5.5.1... But since the workaround is in javascript, it should work across versions...

What I have shared had been beautified to make it more understandable, but it won't work as is. You need to uglify it to a one-liner. Make sure that the whole workaround code stand on one line (eg: No line break)

Also the since the raw output of the multi-element field was in fact a string of values separated by "###", the regex need to match the content of your multi-element field.

In my case, what I really used was this oneliner which processed two item types :

%{var re = /((?:KWS[-\w]+[\w|-]*)|(?:[A-Z]*?(?:-|)[0-9]+S[\w|-]*)|(?:KSS-[0-9]+-[A-Z]+[\w|-]*))|([A-Z]+(?:-|[0-9])+[\w|-]*)/gi; var match, outstring = ""; while(match = re.exec('${Product names[11300]}')) { if(match[1]) outstring += match[1] + "    x 1 pouch\n"; if(match[2]) outstring += match[2] + "    x 5\n"; }outstring}

Tips: Use some regex builder (like regex 101) to build and test your javascript regex to split the raw output of your field.

Santiago Estel May 5, 2020

After updating to version 8.3.3

This is now working:

#{for Preventive Tasks}

  • ${Preventive Tasks[n]}

#{end}

Suggest an answer

Log in or Sign up to answer