Dmiller35 Posted March 5, 2021 Posted March 5, 2021 I'm still new to js but I'm starting to figure it out. I know that I could accomplish what I need by creating multiple rules but I'm trying to figure out how to do this within one rule. I often have to create an address block and have multiple fields for the name. i.e. Prefix, First, Middle, Last, Suffix. I would like to combine all of those into one field but some of them are empty a fair amount. Can someone tell me how to accomplish this? I'm pretty sure I could do it by creating some super complex else-if statement but I feel like there's probably an easier way. Quote
jwhittaker Posted March 8, 2021 Posted March 8, 2021 You can use the append function. http://forums.pti.com/showthread.php?t=2056&highlight=append Quote
Dmiller35 Posted March 16, 2021 Author Posted March 16, 2021 That works. Thanks! One question, does the append text have to be in Global or can this all be done within one rule? Quote
Dan Korn Posted March 17, 2021 Posted March 17, 2021 If you need to call a function from more than one rule, then you should put it into the JavaScript Globals. If it's only used in one rule, then you can either leave it in the rule or put it in the Globals, whichever you prefer. However, in this case, I think a simpler solution would be just: return ["Prefix", "First", "Middle", "Last", "Suffix"].map(Field).filter(String).join(' '); This basically says: for this list of strings, call the Field function on each, filter out any empty ones, and delimit them with spaces. Quote
Dmiller35 Posted March 22, 2021 Author Posted March 22, 2021 Thanks Dan. Yeah I think that's much simpler. But for the Suffix, if I need to add a Comma before, is there a way to do that? Quote
Dan Korn Posted March 22, 2021 Posted March 22, 2021 Thanks Dan. Yeah I think that's much simpler. But for the Suffix, if I need to add a Comma before, is there a way to do that? Sure, something like this: var result = ["Prefix", "First", "Middle", "Last"].map(Field).filter(String).join(' '); return [result, Field("Suffix")].map(String).join(', '); Or: var result = ["Prefix", "First", "Middle", "Last"].map(Field).filter(String).join(' '); if (!Field("Suffix")) return result; return result + ', ' + Field("Suffix"); Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.