1

Avoid spaces in a sum function when a variable is empty/void

Hello, the product name in my products database is a sum of 9 text and number fields, like this

article + " " + Color + " " + more stuffs + " " +kg + " " lt"

 

it works for normal use but in print view, when a field is empty, it still leaves the space (of course), like this:

article color   lt

I can't remove the space from the function or i end up like this

articlecolorlt

Is there a better way/funciton instead of sum to avoid this? I would like that when a field is empty/void it just writes nothing. How? Thanks

8 replies

null
    • Fred
    • 1 yr ago
    • Reported - view

    I'm sure there is a way to figure out how not to put in empty fields, but here is a regex way of doing it.

    You can create a formula field and put this in:

    replacex(originalformulafield, "\s{2,}", " ")
    

    This will find all instance of two or more spaces and replace it with a single space.

    • John_Halls
    • 1 yr ago
    • Reported - view

    Hi Lorenzo

    If you create an array of the field values you can then use concat to create a string, and replace to remove the commas.

    let a := [article,Color,more stuff,kg,lt];
    let b := concat(a);
    let c := replace(b,",","")
    

    I thought you could use the join function but this does not remove empty array elements where concat does.

    This still isn't ideal, as any commas within your fields will also be removed.

    Regards John

    • John_Halls
    • 1 yr ago
    • Reported - view

    A better solution is

    let a := [article,Color,more stuff,kg,lt];
    let b:= a[let c := this; length(c) > 1];
    let d := join(b," ")
    
    

    Regards John

    • Alain_Fontaine
    • 1 yr ago
    • Reported - view

    Or even simpler:

    let a := [article,Color,more stuff,kg,lt];
    let b:= join(a[!=""]," ")
    
    • Ninox partner
    • RoSoft_Steven.1
    • 1 yr ago
    • Reported - view

    Or maybe a oneliner ? 😉

    Article + if Color then " " + Color end + if 'More stufs' then " " + 'More stufs' end + if Kg then " " + Kg end + if Lt then " " + Lt end
    • Fred
    • 1 yr ago
    • Reported - view

    Love the community engagement.

    By the way you can also take my suggestion and add it to your original code. :)

    let x := original code;
    replacex(x, "\s{2,}", " ")
    
      • Lorenzo_Milani
      • 1 yr ago
      • Reported - view

       Thanks it worked like a charm!
      I ended up using your code because of how easy it was to just copy/paste my code in yours.

    • Lorenzo_Milani
    • 1 yr ago
    • Reported - view

    Thanks everyone, I appreciated all of your answers and the different solutions you came up with. Awesome community!

Content aside

  • Status Answered
  • 1 Likes
  • 1 yr agoLast active
  • 8Replies
  • 146Views
  • 5 Following