I'm working on a section of code that creates coupons in our emails. We're pulling the coupon number that we want to use from a data extension @de_send. We have a second data extension @de_coupon that has all of the information to be populated into the coupon. We use a key @coupnum that exists in both data extensions to correlate who gets what coupon. I'm looking to avoid a possible error where the person in @de_send has a blank value in @coupnum.

Essentially what I want to do is skip a whole section of ampscript code if the value is blank, however due to the complexity of the nested if-else statements we're already using I'd like to set an if empty(@coupnum) then {goto end} statement.

I realize if I set if not empty(@coupnum) then I can put all the code I want to skip inside that if-else, but I have to make sure that the else endif doesn't get removed accidentally at the end by someone editing the block.

Long story short does anyone know of any other way to execute a "goto" command in Ampscript?

Simplified Concept:

SET @sendRowset = LOOKUPROWS(@de_send, "email", emailaddr)
if ROWCOUNT(@sendRowset) > 0 then
  set @desendrow = ROW(@sendRowset, 1)
  set @coupnum = FIELD(@desendrow, "coupnum")
  /* other variables etc. */
else /* not in list */
  var @coupnum /* set to null */

/* goto command here */
if empty(@coupnum) then {goto end}
  /* rest of very extensive coupon code here */

No there isn't. However as far as I understand your use case the else-part isn't necessary, so your block could look like this:

if not empty(@coupnum) then
    /* Your code to skip if @coupnum is empty */

Validation should also fail if the endif is removed, so accidentally removing it should be easy to identify. Adding a comment to the endif which includes information about the corresponding if-statement should also reduce the risk of accidentally removing the statement.

Additional note: goto-statements are generally a pretty bad thing to use as the flow of complex scripts/programs is harder to follow for other developers in the future.

  • Yeah, I'm not a huge fan of goto either, but we'll be nested about 5 if-thens deep at some points of the code if I wrap the whole coupon code in another if statement. sounds like that's the best option though. Thanks for the answer! – Kevin Illick Jun 8 '17 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.