A really quick article to test when to use isNull and isBlank.
Why?
So I've noticed that looking at people's Zoho Deluge code, there will often be a check on a null before or after the variable:
if(v_Test.isBlank())
{
...
}
VS
if(isBlank(v_Test))
{
...
}
isBlank() can be used to check if the string only contains blanks or if a value is null...How?
Consider the following test function:
void fn_Test()
{
info "---------------------- Value of TEST is a BLANK String";
v_Test = "";
if(isBlank(v_Test))
{
info "isBlank Preceding is TRUE";
}
else
{
info "isBlank Preceding is FALSE";
}
if(v_Test.isBlank())
{
info "isBlank Succeeding is TRUE";
}
else
{
info "isBlank Succeeding is FALSE";
}
if(isNull(v_Test))
{
info "isNull Preceding is TRUE";
}
else
{
info "isNull Preceding is FALSE";
}
if(v_Test.isNull())
{
info "isNull Succeeding is TRUE";
}
else
{
info "isNull Succeeding is FALSE";
}
if(v_Test == null)
{
info "Equals Null is TRUE";
}
else
{
info "Equals Null is FALSE";
}
info "---------------------- Value of TEST is NULL";
v_Test = null;
if(isBlank(v_Test))
{
info "isBlank Preceding is TRUE";
}
else
{
info "isBlank Preceding is FALSE";
}
try
{
if(v_Test.isBlank())
{
info "Blank Succeeding is TRUE";
}
}
catch (e)
{
info "isBlank Succeeding is FALSE: " + e;
}
if(isNull(v_Test))
{
info "isNull Preceding is TRUE";
}
else
{
info "isNull Preceding is FALSE";
}
if(v_Test.isNull())
{
info "isNull Succeeding is TRUE";
}
else
{
info "isNull Succeeding is FALSE";
}
if(v_Test == "")
{
info "Equals Blank is TRUE";
}
else
{
info "Equals Blank is FALSE";
}
}
//
// yields
/*
---------------------- Value of TEST is a BLANK String
isBlank Preceding is TRUE
isBlank Succeeding is TRUE
isNull Preceding is TRUE
isNull Succeeding is TRUE
Equals Null is FALSE
---------------------- Value of TEST is NULL
isBlank Preceding is TRUE
isBlank Succeeding is FALSE: Error at line : 57, Value is empty and 'isBlank' function cannot be applied
isNull Preceding is TRUE
isNull Succeeding is TRUE
Equals Blank is FALSE
*/
I've tried this in both Zoho Creator and Zoho CRM with the same results. I don't code with the method appended at the end of the variable but in more complex method chaining, I can see myself accidentally doing this. It's more that I saw other code like this and when reverse engineering or debugging the code, it may be worthwhile having a definitive idea of what will happen.The intended purpose of isBlank()
void fn_Test()
{
info "---------------------- Value of TEST is a String with 2 blank spaces";
v_Test = " ";
if(isBlank(v_Test))
{
info "isBlank Preceding is TRUE";
}
else
{
info "isBlank Preceding is FALSE";
}
if(v_Test.isBlank())
{
info "isBlank Succeeding is TRUE";
}
else
{
info "isBlank Succeeding is FALSE";
}
if(isNull(v_Test))
{
info "isNull Preceding is TRUE";
}
else
{
info "isNull Preceding is FALSE";
}
if(v_Test.isNull())
{
info "isNull Succeeding is TRUE";
}
else
{
info "isNull Succeeding is FALSE";
}
if(v_Test == null)
{
info "Equals Null is TRUE";
}
else
{
info "Equals Null is FALSE";
}
}
//
// yields
/*
---------------------- Value of TEST is a String with 2 blank spaces
isBlank Preceding is TRUE
isBlank Succeeding is TRUE
isNull Preceding is FALSE
isNull Succeeding is FALSE
Equals Null is FALSE
*/
Conclusion
The recommended best practice for checking if the variable is null, contains blanks, or is simply an empty string would be:
if(isBlank(v_Test))
{
info "isBlank Preceding is TRUE";
}
If you're not expecting the user to enter any spaces at all and just want to check if the variable exists or is an empty string, then continue with:
if(isNull(v_Test))
{
info "isNull Preceding is TRUE";
}
Additional Note(s):
- I've noticed that if the accepted content on an invokeURL in ZohoCreator is not JSON, then isNull() preceeding will not work.
Instead you will need to do something like the following:
m_Header = Map(); m_Header.put("Authorization","Bearer <my_token>"); m_Header.put("Accept","application/vnd.github+json"); m_Header.put("X-GitHub-Api-Version","10-02-2025"); // // query this user's events v_Endpoint_Events = "https://api.github.com/users/joel-the-awesomest/events"; r_Events = invokeurl [ url :v_Endpoint_Events type :GET headers:m_Header ]; for each m_Event in r_Events { if(m_Event.get("id") != null) { ... do stuff } }
Source(s):
Discussion
Comments
Questions, corrections and useful additions are reviewed before appearing here.
No comments yet. Start the discussion.