Null DateTime Evaluations
I am trying to retrieve the month (in int form) from a DateTime column.
The code is simple for the int column Calculated Value Expression:
{
if ([[Birthday]] != null)
{
return [[Birthday]].Month;
}
else
{
return null;
}
}
However, we receive an error message when the DateTime field is blank. It reads:
Error on evaluating Statements: Line 39 (italic) Cannot implicitly convert type 'object' to 'System.DateTime'. an explicit conversion exists (are you missing a cast?)
It would seem that it passes into the first block, wrongly.
I have used this formula instead which works, but it would be good to know why it not work when using the DateTime methods?
{
if ([[Birthday]] != null)
{
string date = [[Birthday]].ToString().Substring(3,2);
return Int32.Parse(date);
}
else
{
return null;
}
}
-
Hi James Wake
An empty DateTime field returns an object of type string - not a null object!
Therefore you have to handle the input value like this:
if (typeof [[Birthday]] !== 'string') {
return [[Birthday]].Month;
}
return null;0 -
Thanks Christof,
It still doesn't work. I try the code given but it Returns:
Error: Error on evaluating statements: Line 37: Too many characters in character literal
So I place string inside double quotes, and then receive the following:
Error: Error on evaluating statements: Line 37: Syntax error, '(' expected Line 37: ) expected Line 37: Invalid expression term '=' Line 37: ) expected Line 37: ; expected Line 37: Invalid expression term ')' Line 37: ; expected
0 -
Hi James Wake
Make sure you use Function code expression type.
This works fine for me to get the month as number:
if (typeof [[Birthday]] !== 'string') {
return [[Birthday]].getMonth()+1;
}
return null;0 -
I'm am doing this in the Solution Studio in a Calculated Value Expression not in the Rich Forms.
0 -
This expression worked in my Background Calculation case without any issues:
if([[Birthday]]) {
return [[Birthday]].getMonth()+1;
}
return null;0
Please sign in to leave a comment.
Comments
5 comments