Velocity script to check custom object field is null | Community
Skip to main content
New Participant
January 11, 2023
Question

Velocity script to check custom object field is null

  • January 11, 2023
  • 1 reply
  • 3526 views

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.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

1 reply

New Participant
January 19, 2023

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
New Participant
January 20, 2023

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.

New Participant
January 23, 2023

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.