Macro Resolver Exception Errorin Kentico 9

Dan McDevitt asked on March 26, 2020 20:52

We're getting a ResolveMacroData error in one of our transformations that may be bogging down our site. The transformation includes a check of any categories applied to the document, but that check is apparently causing an Exception error.

Here's the full transformation:

    {% x=0; startDate = FormatDateTime(EventDate, "MMM d"); endDate = FormatDateTime(EventEndDate, "MMM d"); return null; %}
<div class="list-item">
   <div class="list-item-date">
      <span class="list-item-date_small">{% FormatDateTime(EventDate, "yyyy") %}</span>
      <div class="list-item-date_large">{% startDate %}{% if(startDate != endDate) { if(FormatDateTime(EventEndDate, "MMM") != FormatDateTime(EventDate, "MMM")) { return "-" + endDate } else { return FormatDateTime(EventEndDate, "-d")} } #%}</div>
      <span class="list-item-date_small">{% FormatDateTime(EventDate, "h:mm tt") %} {% if(Contains(EventTimeZone.toString(), "GMT")) { return Substring(Replace(EventTimeZone.toString(), " (US & Canada)", ""),12) } else { return EventTimeZone; }#%}</span>
      <span class="list-item-date_small">{% if (EventCity.Length > 0) { %} <p class="list-item-meta">{% EventCity %}, {% EventStateCode %}</p> {% } #%}</span>
   </div>
   <div class="list-item-text">
      <h4 class="list-item-title"><a href="{% EventURL.ToString()==""?Transformation.GetDocumentUrl():EventURL #%}" target="{% EventURL.ToString()==""?"_top":"_blank" #%}" >{% DocumentName %}</a></h4>
      {% IfEmpty(ETPCode, "", "<p class=\"list-item-meta mar-bottom\"><a href=\"" + Documents["/Events"].RelativeURL + "?filter=" + ETPCode + "\">" + IfCompare(ETPCode, "Chapter", "", EventChapter + " ") + ETPCode + "</a></p>")#%}
      <p class="list-item-meta">{% foreach(cat in Documents[NodeAliasPath].Categories.Where("CategoryNamePath Like '/NASBA/%' AND CategoryEnabled = 1").TopN(1)){ return "NASBA: " } #%} {% foreach(cat in Documents[NodeAliasPath].Categories.Where("CategoryNamePath Like '/NASBA/%' AND CategoryEnabled = 1")){ if(x >= 1) { print(", "); } x++; print(cat.CategoryDisplayName + IfCompare(cat.CategoryParentID, 19, " (Non-Technical)", " (Technical)")); }#%}</p>
   </div>
</div>

Here's the error message:

Error while evaluating expression: foreach(cat in Documents[NodeAliasPath].Categories.Where("CategoryNamePath Like '/NASBA/%' AND CategoryEnabled = 1")){ if(x >= 1) { print(", "); } x++; print(cat.CategoryDisplayName + IfCompare(cat.CategoryParentID, 19, " (Non-Technical)", " (Technical)")); }|(user)administrator|(hash)d202490aa4578353a98ad8394f1d577bf9cb712fcc7ed8b3fce4d90265a8120d

CMS.MacroEngine.EvaluationException: Exception occured while evaluation of the expression 'foreach (cat in Categories.Where("CategoryNamePath Like \'/NASBA/%\' AND CategoryEnabled = 1")) { ... }': Enumerable object expected within foreach loop.

I know the transformation works because I can see the proper output on our site in the repeater. I'm at a loss to determine what could be causing this exception. Any help would be appreciated.

Recent Answers


Peter Mogilnitski answered on March 26, 2020 22:50 (last edited on March 26, 2020 22:51)

Try to change your last section to something like that:

{%
categoryList = Documents[NodeAliasPath].Categories.Where("CategoryNamePath Like '/NASBA/%' AND CategoryEnabled = 1");
result = "";
if (categoryList.Count > 0 ) {
  result =  "NASBA: ";
  foreach(cat in categoryList)
  { 
      result += cat.CategoryDisplayName;
      if (cat.CategoryParentID == 19) { 
        result += " (Non-Technical), " 
      } 
      else { 
        result += " (Technical), " 
      }
  }
  result = result.TrimEnd(", ");
}
return result;
#%}

Keep in mind every time you do Documents[...]. You produce a sql query. Ideally you should do only rendering in transformation.

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on March 26, 2020 23:01

Hi Dan,

One of the possible reasons could be that this Macro code is used somewhere else (in different transformation, HTML before, some conditions) and it crashes there, but actually not in the transformation you are looking at. I had this a couple of times. You can try to search in System > Macro > Report interface for something like Categories.Where("CategoryNamePath Like \'/NASBA/%\' AND CategoryEnabled = 1") to make sure it is used only in this particular transformation.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on March 28, 2020 01:50

Dan,

If your transformation is that complex, you should be considering a custom transformation method to handle this. Transformations should be simple in nature and not have a lot of custom logic built into it.

1 votesVote for this answer Mark as a Correct answer

Dan McDevitt answered on March 30, 2020 14:16

Thanks everyone.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.