Map Tutorial Intermediate

Map Tutorial Intermediate

This is a follow up post to map tutorial (Beginners)

Hello ZC fans,

I in the last post I discussed some of the basics about the map() function/feature. The last thing I discussed in the post was how to pull in map values from a map function (in functions tab) to a local script (form, view).

But what if you wanted to do the opposite? What if you wanted to transfer a map from a form or view to a function.

Pass a local map variable from form or view to a function

First let's make our map

myMap = map();
myMap.put("key1","value1");
myMap.put("key2",2);
myMap.put("key3",false);

Now let's send this map variable to a function

thisapp.PushmapFunction(myMap);

There! now your map keys and values are ready to be manipulated by your function!

You will find that sending maps to functions is a very useful way to send a lot of parameters to a function quickly.. Sending a single map to your function can save you time and confusion later. They also give you a little more flexibility.

Here's a sample function that could be a little cubersome. It looks like this when you call it.....

thisapp.myCrazyFunction(input.field1, input.field2, input.field3, input.field4, input.field5, input.field6, input.field7, input.field8, input.field9);

This is TOTALLY A VALID FUNCTION! But what if you wanted to change the form field type of input.field2 from a string value to a long value? ZC functions require you to assign an input type to each function parameter. So since you already set "myCrazyFunction" to take a string value by changing input.field2 from string to long, your function becomes invalid.

You would either need to convert input.field2 to a string value (field2_str = field2.toString();) or you would need to change "myCrazyFunction" to except a long value.

If you create and pass a single map variable that holds all your 9 form input field values YOU BYPASS the individual field types. This keeps your function valid and saves you time! (this works the same way for lists).

Now don't get me wrong.. Parameters are a good thing and there are many places where sending individual parameters is a better practice.



The truly powerful feature about maps is that keys can be dynamically named.. You can do some wonderful stuff when you combine the traversable/looping power of lists WITH the "key/value" feature of maps.


"keys can be dynamically named.... "

If you don't know what I mean by this then let's take look at the following INVALID ZC SCRIPT...

value_list = {1,2,3,4,5};
for each value in value_list
{
value+"value" = value;
}

What I was trying to do is dynamically name the variables in the loop. So that I come up with something like this...

value1 = 1;, value2=2; value3 = 3, value4=4;

BUT YOU CAN'T DO THIS IN ZOHO CREATOR. You cannot name variables dynamically. You CAN do this in a full featured scripting language. However, ZC is purposely stripped down. (That's the beauty of it!)

Just so you know... in PHP naming variables is simple..  ${"value".1}  = 1 renders to $value1 = 1.

Anyways this isn't a PHP tutorial. Back on track... :)


Now let's look at the same sort of list loop but this time we are going to use a map and THE ZC DS WILL BE VALID SCRIPT!

value_list = {1,2,3,4,5};
myMap = map();

for each value in value_list
{
myMap.put("value"+value,value);
}

So what does this give us?
myMap = {value1=1, value2=2, value3=3, value4=4, value5=5};

Now we can get all of our "value_list" values by using a key!

myMap.get("value1") == 1;

So dynamic naming of keys is again so powerful. And this is all possible because key
values are string values NOT a true variable.

To hammer down my point of the power of dynamic naming of keys even more.
Let's look at the
following very powerful piece of code...

Put all your form fields and form values into a map variable WITH 7 LINES OF CODE!

fields = getFieldNames();
form_map = map();
for each field in fields
{
value = getFieldValue(field);
form_map.put(field, value);
}

fields = getFieldNames();

returns a string list of all of the form field names.

form_map = map();

creates a new map

for each field in fields{}
is a loop we use to populate form_map map variable

value = getFieldValue(field);

by first creating a list of the field names in the first
line we are able to loop through the form and get the field values for each field!

form_map.put(field, value);

 now we just put our field name from the list as our key and the value
we got using the list as our value.

ISN'T THAT COOL?! With just 7 lines of code we were able to take all the data from the
form and turn it into a map! You could take this form_map and pass it to a function
or you could pass it to a postURL() function that takes the VALID JSON and posts it to
an outside server PHP script to save to a database! Whatever you want.

The point is that lists and maps a just great to use together.

Here's another example of how we can use a list and a map together to create powerful
code. Let's take our fields list and our form_map and send them to a function!

//form side send list and map to function

thisapp.myMapFunction(fields, form_map);

//on the function end

void myMapFunction(list fields, map form_map)
{
for each field in fields
{
if(!field.contains("client_"))
{
form_map.remove(field);
}
}
request_map = map();
request_map.put("form_map",form_map);
response = xml_response.get("responseText");
response = response.executeXPath("/response/result/message/text()");
return response;
}


So in this last example we...

1. looped through each field name that we got from the form

2. We validated that the form field HAD THE STRING "client_" in it. (This means that
you can change which fields get used in your script SIMPLY BY CHANGING/APPENDING YOUR FIELD
NAME! This is a great tool to use if you are building marketplace apps as you give the user
more control without knowing DS)

3. We removed any map key,value pairs that didn't match the "client_" field name
validation

4. We sent the map/JSON to a RESTful resource/web service to be processed.

5. We get the response...


I hope you found these things useful.. In the next post I will be focusing on more
advanced map function snippets and I'll post my FAVORIATE ZC discovery of all time!

Hopefully it's something new to the community. Maybe it's not. If you have ideas or
other great Map tricks! PLEASE POST THEM!