Re: #if #else logic with isEmpty () not working

hjmpie22
Level 1

#if #else logic with isEmpty () not working

I'm trying to build a token using fields from a Marketo Custom Object. I want the token to print the field value if the field isn't empty, and to print nothing if the field is empty. When I run the code below it prints the full script of the token when the field value is empty: 

 

#if( $sep2021TermsAndConditions_cList.get(0).policyName2.isEmpty() )
#else
<br />$sep2021TermsAndConditions_cList.get(0).policyName2 | WC$sep2021TermsAndConditions_cList.get(0).policyNumber2
#end

 

I've also tried this and again for empty fields I just see the full printed script in the email. 

 

#if ( !$sep2021TermsAndConditions_cList.get(0).policyName2.isEmpty() )
<br /> $sep2021TermsAndConditions_cList.get(0).policyName2 | WC$sep2021TermsAndConditions_cList.get(0).policyNumber2
#end

 

 When I tried this nothing was output even when the policyName2 field was not empty: 

 

#if ( $sep2021TermsAndConditions_cList.get(0).policyName2.isEmpty() )
<br /> $sep2021TermsAndConditions_cList.get(0).policyName2 | WC$sep2021TermsAndConditions_cList.get(0).policyNumber2
#end

 

 I'd love any guidance on where I'm going wrong or if there's a better way to evaluate null/empty values in Marketo Custom Object fields. Thanks!

3 REPLIES 3
SanfordWhiteman
Level 10 - Community Moderator

Re: #if #else logic with isEmpty () not working


I'd love any guidance on where I'm going wrong or if there's a better way to evaluate null/empty values in Marketo Custom Object fields. Thanks!

null and empty values are extraordinarily different. You should treat them as near-opposites.

 

Are these null values, or empty strings?

 

Also — is there a specific reason why you’re checking only the first (0th) record in your list? Perhaps you’re using a client app that ensures there’s a max of one object of that type for any lead, but I’d like to hear how you’re 100% sure of that. Generally if you’re dealing with a list, iterate over the list, even if you think it has only one member.

hjmpie22
Level 1

Re: #if #else logic with isEmpty () not working

I believe they are empty strings - the custom object field type is string and there was no information uploaded to the field.

Because it's a Marketo Custom Object that I created I know that there's only one of that specific object for each lead. I could definitely be missing something though - let me know!

SanfordWhiteman
Level 10 - Community Moderator

Re: #if #else logic with isEmpty () not working


I believe they are empty strings - the custom object field type is string and there was no information uploaded to the field.

That’s a null, then, not an empty string.

 

 


Because it's a Marketo Custom Object that I created I know that there's only one of that specific object for each lead. I could definitely be missing something though - let me know!

My point about collections/lists is Marketo will never stop you from having more than one CO record per lead.

 

Every time I’ve worked in an instance where the importing process is said to “never” create more than one object for a lead, there’s always at least one lead — though perhaps just a test lead — for which that isn’t true! So I always recommend treating the collection as what it is, iterating over the objects until you find the first one that matches your expectations. Even if it turns out there’s only one the vast majority of the time.