Portal Engine Questions on portal engine and web parts.
Version 7.x > Portal Engine > EvalInteger returns a value if output in transformation but not when used in an if statement View modes: 
User avatar
Member
Member
matt-awg - 12/3/2012 3:18:57 PM
   
EvalInteger returns a value if output in transformation but not when used in an if statement
I am displaying item availability on a product page using the Basic repeater web part and this code in the Product Detail transformation:
<%if ( EvalInteger("SKUAvailableItems") >= 1 )  
{ %>
<span class="stock green">In stock</span>
<%}
else
{ %>
<span class="stock red">Out of stock</span>
Available Today: <%# EvalInteger("SKUAvailableItems").ToString() %>
<%} %>

This outputs:
<span class="stock red">Out of stock</span>Available Today: 10

This output shows the EvalInteger("SKUAvailableItems") returns 10 when output so how does the first if statement not evaluate as (10 >= 1) and show the in stock message?

I also tried creating a custom transformation function and passing in the value of SKUAvailableItems field using this as a parameter to the function:

CMS.GlobalHelper.ValidationHelper.GetInteger(Eval("SKUAvailableItems"), -1)

And then in the custom function I output the value of this parameter to the the system log and it is passing in the -1 default instead of the value of the SKUAvailableItems field, which is 10. Any ideas/help would be greatly appreciated.

Thanks,
Matt

User avatar
Member
Member
kentico_davidb2 - 12/5/2012 4:24:54 AM
   
RE:EvalInteger returns a value if output in transformation but not when used in an if statement
Hello,

you could create a custom transformation function, that would take an integer (SKUAvailableItems) and return the code text:
<span class=\"stock green\">In stock</span>"
or
"<span class=\"stock red\">Out of stock</span> Available Today: " + SKUAvailableItems

based on simple if statement.

in the transformation, you would then only call it similarly to:
<%# GetSKUAvailabilityText(EvalInteger("SKUAvailableItems")) %>

Dave

User avatar
Member
Member
matt-awg - 12/5/2012 11:33:42 AM
   
RE:EvalInteger returns a value if output in transformation but not when used in an if statement
Thank you Dave... I realize that and that is what I ended up doing but I feel it is an ugly solution because now designers cannot edit this HTML and the entire app has to recompile if the designers ask me to change this HTML in the future. I was hoping to leave as much of the HTML inside the transformation instead of having it in the custom transformation function inside the app_code folder. I guess this is just not possible with Kentico.

User avatar
Member
Member
kentico_davidb2 - 12/6/2012 3:00:13 AM
   
RE:EvalInteger returns a value if output in transformation but not when used in an if statement
Hello,
you can also make a more general transformation method such as:

public object IfGreaterEqualInt(int firstValue, int secondValue, object trueResult, object falseResult)
{
if (firstValue != null && secondValue != null && firstValue >= secondValue) return trueResult; else return falseResult;
}

And then use is like:

<%# IfGreaterEqualInt(EvalInteger("SKUAvailableItems"),1,"<span class=\"stock green\">In stock</span>","<span class=\"stock red\">Out of stock</span> Available Today: " + EvalInteger("SKUAvailableItems").ToString()) %>

This will enable you to customize everything within the transformation itself.

Dave