0

I'm trying to get a nested IF statement to work but the ELSE statement doesn't seem to be resolving. Everything else seems to behaving correctly including the various outputs. What am I missing in my statement?

{ IF { MERGEFIELD CODE } = 0001 "Case 1" 
{ IF { MERGEFIELD CODE } = 0002 "Case 2" 
{ IF { MERGEFIELD CODE } = 0003 "Case 3"
{ IF { MERGEFIELD CODE } = 0004 "Case 4"
{ IF { MERGEFIELD CODE } = 0005 "Case 5"
{ IF { MERGEFIELD CODE } = 0006 "Case 6"
{ IF { MERGEFIELD CODE } = 0007 "Case 7" 
}}}}}}
"{ MERGEFIELD ELSEOUTPUT }"
}
macropod
  • 12,757
  • 2
  • 9
  • 21
BoBFiSh
  • 102
  • 9
  • 1
    You really do not need these to be nested. – Charles Kenyon Aug 13 '22 at 14:13
  • «You really do not need these to be nested» Oh, really? – macropod Aug 14 '22 at 05:41
  • Might as well spell it out in case the OP reappears - If you separate out the 8 possibilities with no nesting, then you are OK as long as there is no 1-7 match. Because then you just get the ELSEOUTPUT output. But the trouble is if you do that, even if if there is a match with 1 to 7, you *still* get the ELSEOUTPUT. So to provide an answer to OP's question, you still have to suggest something that excludes the ELSEOUTPUT when any of the 1-7 matches occurs. – jonsson Aug 14 '22 at 20:10
  • Apologies on my delayed response. I was on holiday. Useful to know, I did resolve this in the end but shall read the answers below so we can get one marked as answered if correct :) – BoBFiSh Sep 07 '22 at 09:13

2 Answers2

1

If your outputs positive numbers if valid and 0 or nothing otherwise, you could use just:

{MERGEFIELD CODE \# "'Case '0;;'{MERGEFIELD ELSEOUTPUT}'"}

Otherwise, you might use:

{IF{MERGEFIELD CODE \# 0000}= "0001" "Case 1" 
{IF{MERGEFIELD CODE \# 0000}= "0002" "Case 2" 
{IF{MERGEFIELD CODE \# 0000}= "0003" "Case 3" 
{IF{MERGEFIELD CODE \# 0000}= "0004" "Case 4" 
{IF{MERGEFIELD CODE \# 0000}= "0005" "Case 5" 
{IF{MERGEFIELD CODE \# 0000}= "0006" "Case 6" 
{IF{MERGEFIELD CODE \# 0000}= "0007" "Case 7" {MERGEFIELD ELSEOUTPUT}}}}}}}}

or:

{IF{MERGEFIELD CODE}= 1 "Case 1" 
{IF{MERGEFIELD CODE}= 2 "Case 2" 
{IF{MERGEFIELD CODE}= 3 "Case 3" 
{IF{MERGEFIELD CODE}= 4 "Case 4" 
{IF{MERGEFIELD CODE}= 5 "Case 5" 
{IF{MERGEFIELD CODE}= 6 "Case 6" 
{IF{MERGEFIELD CODE}= 7 "Case 7" {MERGEFIELD ELSEOUTPUT}}}}}}}}
macropod
  • 12,757
  • 2
  • 9
  • 21
  • I think this is what we ended up doing to resolve it, moving the merge elseoutput to the right place. Both your and Jonsson's answer cover it. Jonsson's goes into quite a bit more detail so a little torn on which I should put forward as a solution. – BoBFiSh Sep 07 '22 at 09:17
  • Righto. I will admit, I don't have time to test all of their options and the result was effectively what you've got here. I've accepted an answer but also given both upvotes as well, since I found both useful. – BoBFiSh Sep 07 '22 at 09:28
  • @macropod Whether something is "peripheral" or not depends on the exact problem the OP has to solve - you can't guarantee from the OP's original statement of the question that the options you proposed wouls actually work either. As it happens, they seem to, so no big deal. But the OP's question was "what am I missing?" so I tried to answer that. I don't see why you have to be negative about an Answer that covers the ground. If you see errors, please point them out, with evidence. – jonsson Sep 07 '22 at 10:57
1

The main thing wrong is the nesting - you need to relocate your

"{ MERGEFIELD ELSEOUTPUT }"

so that you have

"Case 7" "{ MERGEFIELD ELSEEOUTPUT } }

The explanation is as follows.

The syntax for an IF field is (simplified)

{ IF condition true-result false-result }

Anything after false-result will just be ignored. Try, e.g.

{ IF 1 = 1 "A" "B" "C" }

(You should see A )

and

{ IF 1 = 2 "A" "B" "C" }

(You should see B )

The "C" value will never be in the result.

Simplifying your IF statement down to a couple of cases, you have something like

{ IF X = 1 "1" { IF X = 2 "2" } { ELSEOUTPUT } }

So the "1" is the equivalent of the "A", the { IF X = 2 "2" } the equivalent of the "B" and the { ELSEOUTPUT } is the equivalent of the "C"

i.e. the { ELSEOUTPUT } will never be in the result.

So you need to move your { MERGEFIELD ELSEOUTPUT } so that it is the false-result of the 0007 test, i.e.perhaps

{ IF { MERGEFIELD CODE } = 0001 "Case 1" 
{ IF { MERGEFIELD CODE } = 0002 "Case 2" 
{ IF { MERGEFIELD CODE } = 0003 "Case 3"
{ IF { MERGEFIELD CODE } = 0004 "Case 4"
{ IF { MERGEFIELD CODE } = 0005 "Case 5"
{ IF { MERGEFIELD CODE } = 0006 "Case 6"
{ IF { MERGEFIELD CODE } = 0007 "Case 7" "{ MERGEFIELD ELSEOUTPUT }"
}}}}}}}

or to follow your existing layout, something like

{ IF { MERGEFIELD CODE } = 0001 "Case 1" 
{ IF { MERGEFIELD CODE } = 0002 "Case 2" 
{ IF { MERGEFIELD CODE } = 0003 "Case 3"
{ IF { MERGEFIELD CODE } = 0004 "Case 4"
{ IF { MERGEFIELD CODE } = 0005 "Case 5"
{ IF { MERGEFIELD CODE } = 0006 "Case 6"
{ IF { MERGEFIELD CODE } = 0007 "Case 7" 
  "{ MERGEFIELD ELSEOUTPUT }"
}}}}}}}

Beyond that, whether the IF will work exactly as you intend depends on what values the CODE field can have and how you want each value to be treated.

As long as your codes are all 4-digit numeric codes, or they are numeric codes and you want "1", "01" etc. to be treated the same way as "0001" then the IF field should work as it is. Or you could simplify it in various ways.

In the rather less common situation where you need "1" to be treated differently from "0001", you would need to quote the { MERGEFIELD CODE } field, e.g. assuming "1" needs to result in ELSEOUTPUT you need

{ IF "{ MERGEFIELD CODE }" = 0001 "Case 1" 
{ IF "{ MERGEFIELD CODE }" = 0002 "Case 2" 
{ IF "{ MERGEFIELD CODE }" = 0003 "Case 3"
{ IF "{ MERGEFIELD CODE }" = 0004 "Case 4"
{ IF "{ MERGEFIELD CODE }" = 0005 "Case 5"
{ IF "{ MERGEFIELD CODE }" = 0006 "Case 6"
{ IF "{ MERGEFIELD CODE }" = 0007 "Case 7" 
  "{ MERGEFIELD ELSEOUTPUT }"
}}}}}}}

Although to make everything as clear as possible I would favour

{ IF "{ MERGEFIELD CODE }" = "0001" "Case 1" 
{ IF "{ MERGEFIELD CODE }" = "0002" "Case 2" 
{ IF "{ MERGEFIELD CODE }" = "0003" "Case 3"
{ IF "{ MERGEFIELD CODE }" = "0004" "Case 4"
{ IF "{ MERGEFIELD CODE }" = "0005" "Case 5"
{ IF "{ MERGEFIELD CODE }" = "0006" "Case 6"
{ IF "{ MERGEFIELD CODE }" = "0007" "Case 7" 
  "{ MERGEFIELD ELSEOUTPUT }"
}}}}}}}

Quoting the "0001" and not quoting the "{ MERGEFIELD CODE }" means that the comparison is still numeric and won't work as intended.

That will also deal with two other "edge" cases, i.e.

  • If your codes can be alphanumeric, if you do not quote the { MERGEFIELD CODE } field, codes like "3ABC", "03AB" and even "01+2" will also match with 0003.

  • If { MERGEFIELD CODE } resolves to the name of a bookmark in your mail merge main document and you do not quote the field or the other comparand, the IF field will actually compare the value of that bookmark. Yes, it's a really weird quirk of the IF field, but it's why I favour quoting anything that you want to be treated as alphanumeric.

So e.g. if { MERGEFIELD CODE } has value "ABCD" and somewhere before your { IF } field you have { SET ABCD 4 } if you have

{ IF { MERGEFIELD CODE } = 0004 "Case 4" "{ MERGEFIELD ELSEOUTPUT }" }

then the result will be Case 4, not the ELSEOUTPUT value.

jonsson
  • 655
  • 1
  • 3
  • 10