Re: Velocity script to check custom object field is null

joemadafferi
Level 1

Velocity script to check custom object field is null

I'm trying to develop a velocity script where it checks if a field in a custom object (which is being synced from a Dynamics custom entity) is not empty. If this field is empty, I would like to print a message saying the details are not available at this time. The entity is being synced with Marketo using the standard process, and I can call information from this entity successfully in other scripts.

 

The data is structured as follows:

  • The contact/lead is linked a record on the 'contactQualification' object, this is a one-to-many relationship.
  • A qualification record has a lookup field to 'externalCourseList', this is a one-to-one relationship.
  • A courseList record has a field 'friendly name' which is what I would like to print.

Contacts can have multiple qualifications, but I'm happy to print only the most recent one (that is, only the record at index 0). If there are more than one, this is irrelevant.

 

My code is currently:

#if($contactqualificationList.isEmpty())
Your qualification details are not available at this time.
#elseif(!$contactqualificationList.get(0).qualificationid || $contactqualificationList.get(0).qualificationid.isEmpty())
Your qualification details are not available at this time.
#elseif($contactqualificationList.get(0).externalcourseList.isEmpty())
Your qualification details are not available at this time.
#else
You are studying $contactqualificationList.get(0).externalcourseList.get(0).name.
#end

 

My results from this are:

  • For records that have a qualification record - the script works successfully, and I can see the friendly name printing.
  • For all records that don't have this record filled in but do have a record on contactQualification (the field is null) - I get the following (not really helpful!) error:
Cannot get email content- <div>An error occurred when procesing the email Body! </div> <p>None.get near</p> <div><pre ></pre></div>​
  • Records with no qualification applied to their record - the correct error message will display.

 

I do have another version of the code, but it is less reliable:

#if( $contactqualificationList.isEmpty() )
Your qualification details are not available at this time.
#elseif( $contactqualificationList.qualificationid.isEmpty() )
Your qualification details are not available at this time.
#else
You are studying $contactqualificationList.get(0).externalcourseList.get(0).name
#end

 

What is causing this error and how do I correctly reference if null fields in custom objects in Velocity? I am able to successfully use the isEmpty method in other scripts, however this example keeps failing! I have tried calling the data through a loop, but I will easily exceed the call limit.

5 REPLIES 5
joemadafferi
Level 1

Re: Velocity script to check custom object field is null

I have tried iterating over this script but still no luck.  Even if I set a variable outside of the foreach function as error validation:

#set( $qual = "Unavailable" )
#foreach( $qualificationid in $contactqualificationList )
  #if( !$contactqualificationList.qualificationid.isEmpty() )
    #set( $qual = $contactqualificationList.get(0).externalcourseList.get(0).name )
    #break
  #end
#end
${qual}

This still fails with the same non-descript error:

Cannot get email content- <div>An error occurred when procesing the email Body! </div> <p>None.get near</p> <div><pre ></pre></div>

Does anyone have any insights to why this is happening?

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity script to check custom object field is null

The code as written doesn't make sense because you're referencing a property qualificationid of the List $contactqualificationList. That's what $contactqualificationList.qualificationid would be. But that's incorrect.

 

If $contactqualificationList is a List, then it has elements which may be objects with a property named qualificationid. The list itself doesn't have a single property qualificationid.

 

So fundamentally, if $contactqualificationList is a List of elements, and the elements each have a (presumably String) property qualificationid and another List property externalCourseList, this is how you'd iterate:

#foreach( $qualitem in $contactqualificationList )
    ---
    qualification id: ${qualitem.qualificationid}
    #foreach( $course in $qualitem.externalcourselist )
      course name:  ${course.name}
    #end
#end

 

Try not prematurely add conditions if you don't understand the shape of the objects exactly. You want to dump first, filter later.

joemadafferi
Level 1

Re: Velocity script to check custom object field is null

Hi @SanfordWhiteman, thank you for your reply I appreciate the help.

 

I have taken your response and was able to correctly set up the 'foreach' loop as below:

#set ( $qual = "Unavailable" )
#foreach( $qualitem in $contactqualificationList )
    #set ( $qual = $qualitem.qualificationid )
    #foreach( $course in $qualitem.externalcourseList )
    	#set ( $qual = $course.name )
    #end
#end
$qual

 

However, I'm still having my initial problem where:

  • If a contact does not have an element from $contactqualificationList - the script works as expected by printing 'unavailable'
  • if a contact has an element from the $contactqualificationList list where the property qualificationid is empty - the email will fail to send due to a fatal error:
Cannot get email content- <div>An error occurred when procesing the email Rendered_Email_Velocity_Error_Area_?! </div> <p>None.get near</p> <div><pre >?</pre></div>
  • If the contact has qualificationid filled in and not empty - the friendly name from $course.name from the secondary table will print.

 

I have confirmed the above with actual test sends and not by sending sample emails.

 

I have tried to catch the error by following other suggestions made in the forum (like Solved: Error with velocity token sending sample email: Re... - Marketing Nation (marketo.com)) such as using $display and using $set before the loop, but I can't protect against the error for said contacts.

 

 

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity script to check custom object field is null


if a contact has an element from the $contactqualificationList list where the property qualificationid is empty - the email will fail to send due to a fatal error:

"empty" or null?

 

Please post the output of this:

#foreach( $qualitem in $contactqualificationList )
    ${foreach.index} ${qualitem.qualificationid.class} qualificationid
    ${foreach.index} ${qualitem.externalcourseList.class} externalcourseList
    #foreach( $course in $qualitem.externalcourseList )
    	${foreach.parent.index}.${foreach.index} ${course.class} course
    #end
#end

 

joemadafferi
Level 1

Re: Velocity script to check custom object field is null

 


@SanfordWhiteman wrote:

if a contact has an element from the $contactqualificationList list where the property qualificationid is empty - the email will fail to send due to a fatal error:

"empty" or null?


These fields are null in Dynamics and being populated by the standard custom object sync. 


This is the results of the code you supplied:

  • Contact does not have an element from $contactqualificationList - no output

  • Contact has an element from the $contactqualificationList list where the property qualificationid is empty - still getting the same error
Cannot get email content- <div>An error occurred when procesing the email Rendered_Email_Velocity_Error_Area_?! </div> <p>None.get near</p> <div><pre >?</pre></div>

 

  • Contact has an element from the $contactqualificationList and  qualificationid filled in and not empty - if the contact only has one element
0 class java.lang.String qualificationid
0 class java.util.ArrayList externalcourseList
0.0 class java.util.LinkedHashMap course

if the contact has multiple elements in $contactqualificationList

0 class java.lang.String qualificationid
0 class java.util.ArrayList externalcourseList
0.0 class java.util.LinkedHashMap course
1 class java.lang.String qualificationid
1 class java.util.ArrayList externalcourseList
1.0 class java.util.LinkedHashMap course