SOLVED

Re: Velocity Script - Adding 2 times "and" before and after last items of array, and how to remove blank space between array's items

Go to solution
Cristina_Leonet
Level 2

Hello!

I'm seeking your help to fix this code that I created to output a list of "true" values split by "," where the last item has an "and" before instead of a comma.

When I run a test, I see the "and" is repeated after and before the last item, and also I'm not able to find out how to remove the blank space between the array products name.

 

Here you can find what I've done

 

 

#set ($Partners = $lead.becomeapartner_test)
#set ($BI = $lead.sNBusinessIntelligence)
#set ($Cloud = $lead.sNCloud)
#set ($Embedded = $lead.sNEmbeddedIntelligence)
#set ($Hyper = $lead.sNHyperIntelligence)
#set ($Consulting = $lead.sNProfessionalServices)

#set ($PartnersValue = "")
#if ($Partners=="1")
	#set ($PartnersValue = "Become a Partner")
#end

#set ($BIValue = "")
#if ($BI=="1")
	#set ($BIValue = "Business Intelligence")
#end

#set ($CloudValue = "")
#if ($Cloud=="1")
	#set ($CloudValue = "Cloud")
#end

#set ($EmbeddedValue = "")
#if($Embedded=="1")
	#set ($EmbeddedValue = "Embedded Analytics")
#end

#set ($HyperValue = "")
#if($Hyper=="1")
	#set ($HyperValue = "HyperIntelligence")
#end

#set ($ConsultingValue = "")
#if($Consulting=="1")
	#set ($ConsultingValue = "Consulting")
#end

#set ($productsValues = [$PartnersValue, $BIValue, $CloudValue, $EmbeddedValue, $HyperValue, $ConsultingValue])
#set ($productsValuesLenght = $productsValues.size())
#set ($Last = $productsValuesLenght - 1)
#set ($Start = $productsValuesLenght - 2)
#foreach( $productValue in $productsValues )
	#if ($productValue != "") 
    	$productValue
		#if($foreach.count <= $Start)
        	,
        #elseif($foreach.count == $Last)
         and
        #end
	#end
#end

 

 

Thanks in advance for your suggestions!

Cheers! 😊 

1 ACCEPTED SOLUTION
SanfordWhiteman
Level 10 - Community Moderator

I would do this whole thing more efficiently and extensibly.

 

Always try for a configuration-centric or collection-centric approach. Think of your friendly names as part of your configuration, not your code. Code shouldn't be peppered w/new strings everywhere.

 

Here you see the config — your interesting fields and their friendly labels — at the top. Just loop over them and copy the true ones to a new list. Display that new list.

#set( $fieldsAndFriendlyNames = {
  "becomeapartner_test" : "Label1",
  "sNBusinessIntelligence" : "Label2",
  "sNCloud" : "Label3",
  "sNEmbeddedIntelligence" : "Label4",
  "sNHyperIntelligence" : "Label5",
  "sNProfessionalServices" : "Label6"
} )
#set( $trueFields = [] )
#foreach( $fieldDesc in $fieldsAndFriendlyNames.entrySet() )
#set( $fieldRealName = $fieldDesc.getKey() )
#set( $fieldFriendlyName = $fieldDesc.getValue() )
#if( $lead[$fieldRealName].equals("1") )
#set( $void = $trueFields.add($fieldFriendlyName) )
#end
#end
${display.list($trueFields)}

 

View solution in original post

4 REPLIES 4
SanfordWhiteman
Level 10 - Community Moderator

I would do this whole thing more efficiently and extensibly.

 

Always try for a configuration-centric or collection-centric approach. Think of your friendly names as part of your configuration, not your code. Code shouldn't be peppered w/new strings everywhere.

 

Here you see the config — your interesting fields and their friendly labels — at the top. Just loop over them and copy the true ones to a new list. Display that new list.

#set( $fieldsAndFriendlyNames = {
  "becomeapartner_test" : "Label1",
  "sNBusinessIntelligence" : "Label2",
  "sNCloud" : "Label3",
  "sNEmbeddedIntelligence" : "Label4",
  "sNHyperIntelligence" : "Label5",
  "sNProfessionalServices" : "Label6"
} )
#set( $trueFields = [] )
#foreach( $fieldDesc in $fieldsAndFriendlyNames.entrySet() )
#set( $fieldRealName = $fieldDesc.getKey() )
#set( $fieldFriendlyName = $fieldDesc.getValue() )
#if( $lead[$fieldRealName].equals("1") )
#set( $void = $trueFields.add($fieldFriendlyName) )
#end
#end
${display.list($trueFields)}

 

Cristina_Leonet
Level 2

Flawless as always! Thank you so much! @SanfordWhiteman 

 

I'm really interested in learn more about velocity, do you recommend some learning sources for beginners! 

 

Cheers!

 

 

 

SanfordWhiteman
Level 10 - Community Moderator

I'm really interested in learn more about velocity, do you recommend some learning sources for beginners! 

Well... aside from the resources on my blog (most of which are cross-posted here to the Products Blog) there’s not much. And even those posts aren’t so beginner-ish. You might look up my talk from this year’s Adobe Experience Makers, which gives some new angles. Also plan to host some Velocity webinars soon, if I can remember how to do makeup. 🙂

Jon_Wright
Level 4

Going to try and beat @SanfordWhiteman to this one, but this should do it rather than the for loop.

$display.list($productsValues, ", ", " and ")

 Sure Sanford has feedback on how do do all of this more succinctly though.