SOLVED

Need Help With a Velocity Script

Go to solution
Joey_Forcelli1
Level 5

Need Help With a Velocity Script

Hey All,

I am trying to put together a simple script to populate an image url based upon a integer value.  I essentially want 1 image if the value is greater than 0 and a default image if the value = 0.  I tried using the below script but it did not work (I have replaced the actual URL with place holder text for demonstration purposes only).

pastedImage_0.png

Now, if I set the "highest score" equal to XX, the script will work for leads with a highest score field value = XX.

pastedImage_1.png

Once I have this part figured out, I will also need to add an OR statement to check if "highest score 2" > 0

Sanford Whiteman​ - Hoping you can help me out.

Thanks in advance.

-JF

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Need Help With a Velocity Script

Sounds like you're getting empty strings, not just "0" strings. As long as those fields both exist (even if empty) on every Opportunity object, this works for empty strings and numeric values.

#set( $targetOpp = $OpportunityList[0] )

#foreach ( $oppScoreField in ["Highest_Score__c", "Highest_Score_2__c"] )

#if( $targetOpp[$oppScoreField].isEmpty() )

  #set( $targetOpp[$oppScoreField] = 0 )

#else

  #set( $targetOpp[$oppScoreField] = $convert.parseNumber($targetOpp[$oppScoreField]) )

#end

#end

#if( $targetOpp.Highest_Score__c > 0 || $targetOpp.Highest_Score_2__c > 0 )

One or both fields is > 0.

#else

Neither field is > 0.

#end

Note that I use the square brackets [0] syntax as get() is just OOP chaff.

View solution in original post

11 REPLIES 11
Joey_Forcelli1
Level 5

Re: Need Help With a Velocity Script

Still trying to troubleshoot this and I am thinking that the field is not being recognized as a integer field.

I set $test = highest score + 1 and the output was [highest score]1

SanfordWhiteman
Level 10 - Community Moderator

Re: Need Help With a Velocity Script

#if( $convert.parseNumber($OpportunityList[0].Highest_Score__c) > 0 )

Joey_Forcelli1
Level 5

Re: Need Help With a Velocity Script

Thank you Sanford.

To turn this into an OR statement, would the logic need to be:

#if( $convert.parseNumber($OpportunityList.get(0).Highest_Score__c) > 0 || $convert.parseNumber(${OpportunityList.get(0).Highest_Score_2__c}) > 0 )

SanfordWhiteman
Level 10 - Community Moderator

Re: Need Help With a Velocity Script

You don't need the one with curly braces at all.  $convert coerces the string to a number.

Joey_Forcelli1
Level 5

Re: Need Help With a Velocity Script

The second part of that statement is a different field I also want to evaluate.  Are you saying to drop the "$convert.parsenumber" piece?

I just tried the following and it did not work:

#if( $convert.parseNumber($OpportunityList.get(0).Highest_Score__c) > 0 || ($OpportunityList.get(0).Highest_Score_2__c) > 0 )

SanfordWhiteman
Level 10 - Community Moderator

Re: Need Help With a Velocity Script

You need to wrap all string vars in $convert.parseNumber(). Sorry if that wasn't clear!

Joey_Forcelli1
Level 5

Re: Need Help With a Velocity Script

Sorry dude, very new to velocity scripting.

I just tried the following and again it didn't work:

#if( $convert.parseNumber($OpportunityList.get(0).Highest_Score__c) > 0 || $convert.parseNumber($OpportunityList.get(0).Highest_Score_2__c) > 0 )

Appreciate your help with this.

SanfordWhiteman
Level 10 - Community Moderator

Re: Need Help With a Velocity Script

Sounds like you're getting empty strings, not just "0" strings. As long as those fields both exist (even if empty) on every Opportunity object, this works for empty strings and numeric values.

#set( $targetOpp = $OpportunityList[0] )

#foreach ( $oppScoreField in ["Highest_Score__c", "Highest_Score_2__c"] )

#if( $targetOpp[$oppScoreField].isEmpty() )

  #set( $targetOpp[$oppScoreField] = 0 )

#else

  #set( $targetOpp[$oppScoreField] = $convert.parseNumber($targetOpp[$oppScoreField]) )

#end

#end

#if( $targetOpp.Highest_Score__c > 0 || $targetOpp.Highest_Score_2__c > 0 )

One or both fields is > 0.

#else

Neither field is > 0.

#end

Note that I use the square brackets [0] syntax as get() is just OOP chaff.

Joey_Forcelli1
Level 5

Re: Need Help With a Velocity Script

Regret to say but this didn't work for me either.  Going to go an alternate route and create a Boolean field that gets updated by the 2 fields I need evaluated and run my script off that field.

I still don't understand why #if( $convert.parseNumber($OpportunityList.get(0).Highest_Score__c) > 0 || $convert.parseNumber($OpportunityList.get(0).Highest_Score_2__c) > 0 ) does not work.  The first half of the formula worked perfectly but when I added the pipes and the second half it wasn't returning the correct value.  The lead I tested the script with had highest score = 22 and highest score 2 = 0 and rendered a false output.

Thanks for all your help Sanford.  Thought we had it.