This is an article on a trick I picked up from one of our interviewees as well as apparently in one of the Zoho documentation manuals. It uses leftpad to create a string of a length, converts it to a list, then lets you generate an array or list of any size.
Why?
My use case here is to run a schedule that will populate a list with the upcoming dates for the next week (and for any numbers of weeks thereafter). This needs to be dynamic as it is a schedule run daily checking the availability of a staff member for the upcoming week.
How?
As mentioned, using leftpad takes 2 parameters, the first is the string to start with, the second is the number of spaces to pad with.
// // set number of times to iterate through loop (plus 1) v_CheckDaysAhead = 7; // // create a string with this many spaces using leftpad v_GeneratedList = leftpad(" ", v_CheckDaysAhead).replaceAll(" ",","); // // convert string to a list l_GeneratedList = v_GeneratedList.toList(); // // initialize my list to populate l_CheckDates = List(); // // loop through generated list and add dates for each index v_Index in l_GeneratedList { if(v_Index>0) { l_CheckDates.add(zoho.currentdate.addDay(v_Index).toString("yyyy-MM-dd")); } } // // output info l_CheckDates; // // outputs: 2022-10-22,2022-10-23,2022-10-24,2022-10-25,2022-10-26,2022-10-27,2022-10-28
- //
- // set number of times to iterate through loop (plus 1)
- v_CheckDaysAhead = 7;
- //
- // create a string with this many spaces using leftpad
- v_GeneratedList = leftpad(" ", v_CheckDaysAhead).replaceAll(" ",",");
- //
- // convert string to a list
- l_GeneratedList = v_GeneratedList.toList();
- //
- // initialize my list to populate
- l_CheckDates = List();
- //
- // loop through generated list and add dates
- for each index v_Index in l_GeneratedList
- {
- if(v_Index>0)
- {
- l_CheckDates.add(zoho.currentdate.addDay(v_Index).toString("yyyy-MM-dd"));
- }
- }
- //
- // output
- info l_CheckDates;
- //
- // outputs: 2022-10-22,2022-10-23,2022-10-24,2022-10-25,2022-10-26,2022-10-27,2022-10-28