Re: Getting values from Object by Object Key

Adam
Level 2

Getting values from Object by Object Key

Hello! I'm trying to get an object value by its key. It seems to work fine if i manually get the ID (i.e. $clinics.Map.get("784423").NME) but when I use a variable instead of the manual number (i.e. $clinics.Map.get($string_item).NME) it always prints out the last item in the reminder list object, which in the below code is $lead.reminderNextAppt4ClinicID. Any ideas?

 

Here is a sample of the Array:

 

#set($clinics = {})
#set( $clinics.Map = {"1535262":{"CID":10115,"LCID":1535262,"NME":"Lifemark Saint John","ADDR1":"206-555 Somerset St.","ADDR2":"","CTY":"Saint John","PROV":"New Brunswick","PC":"E2K 4X2","TEL":"(506) 642-7872","EM":"saintjohn@lifemark.ca","LAT":45.28752,"LNG":-66.081231,"DIRNME":"Cathy Simon","GDIR":"node/1262/maps/dir","GPSVY":"search.google.com/local/reviews?placeid=ChIJu96SdUWzp0wRTeHgxkeVb50","FBU":"https://www.facebook.com/lifemark.saintjohn","CBR":"Lifemark Physiotherapy"},"184410":{"CID":10131,"LCID":184410,"NME":"Lifemark McKenzie","ADDR1":"1555 McKenzie Avenue","ADDR2":"Suite 275","CTY":"Victoria","PROV":"British Columbia","PC":"V8N 1A4","TEL":"(250) 477-1441","EM":"mckenzie@lifemark.ca","LAT":48.467892,"LNG":-123.332512,"DIRNME":"Chris Josue","GDIR":"node/1263/maps/dir","GPSVY":"search.google.com/local/reviews?placeid=ChIJkVSGseBzj1QRV31xZtPQZ9Q","FBU":"https://www.facebook.com/lifemark.mckenzie","CBR":"Lifemark Physiotherapy"})

 

This is the code I use to grab the array contents. $lead.reminderNextApptxClinicID refers to the user account settings value, which works fine.

#set($reminderList = {})
$reminderList.put($lead.reminderNextAppt1ClinicID,'')
$reminderList.put($lead.reminderNextAppt2ClinicID,'')
$reminderList.put($lead.reminderNextAppt3ClinicID,'')
$reminderList.put($lead.reminderNextAppt4ClinicID,'')

#foreach( $item in $reminderList.keySet())
#set($string_item = $convert.toString( $item ) )
#set($clinic = $clinics.Map.get( $item ) )

ID from Object: $item ##works correctly
ID from Object converted .toString(): $string_item ##returns the same output as the ID from Object above
ID from Object converted .toString() used in clinics.Map object: $clinics.Map.get($string_item).NME ##this just prints out code
ID set manually with a string from clinics.Map object: $clinics.Map.get("784423").NME ##this gets the correct value

#end

 

5 REPLIES 5
SanfordWhiteman
Level 10 - Community Moderator

Re: Getting values from Object by Object Key

Have to a see a dump of your $lead properties at runtime.

 

Works for me if $lead.reminderNextAppt4ClinicID is Integer 184410 or String "184410", for example.

 

Also, you should swallow the null return:

#set( $void = $reminderList.put($lead.reminderNextAppt1ClinicID,'') )
#set( $void = $reminderList.put($lead.reminderNextAppt2ClinicID,'') )
#set( $void = $reminderList.put($lead.reminderNextAppt3ClinicID,'') )
#set( $void = $reminderList.put($lead.reminderNextAppt4ClinicID,'') )

 

(Why use a Map here instead of a List, if the properties are always the same empty String?)

 

 

Adam
Level 2

Re: Getting values from Object by Object Key

I just tested something and I forgot to mention something important.

 

#set($clinics = {})#set( $clinics.Map = {...}

 

That is actually being pulled in from a text token. If i put that whole object in the same Email Script token, it seems to work fine. But this is not how its set up. The idea is that there is a centralized place for data in a text token and the Email Script token just fetches the data from that text token. Any ideas that could help using this information?

SanfordWhiteman
Level 10 - Community Moderator

Re: Getting values from Object by Object Key

Why use a Text token as opposed to a Velocity token? All of our "config" tokens are Velocity tokens as a matter of course. Positive results with a Text token can be alluring, but aren't guaranteed.

Adam
Level 2

Re: Getting values from Object by Object Key

Initially it was all set up to store Config data using Script Tokens, but the API to manage that is gone so we switched over to Text Tokens. The config data is populated using the API from a different system, its not manually set.

SanfordWhiteman
Level 10 - Community Moderator

Re: Getting values from Object by Object Key

Eh, OK, I understand the urge, but there are lots of weird things that happen when you use a Text token (differences between trigger and batch contexts, for one big example).

 

I'd still like to see the output of those $lead properties at runtime. And the class(es) of the keys in the Map, too.