Print

Zoho Creator: Copy Subform to other Subforms

What?
A quick article on a snippet of code which copies one subform to other subforms in the same form.

Why?
I'm creating an appointment/booking system and I want the user to be able to set entries in a subform called "Mondays" then to click a button which copies it to the remaining working days.

How?
So I have a form with 6 subforms which list staff/employee shifts from Monday to Saturday. The form I have looks something like the following:
Zoho Creator: Copy Subform to other Subforms: Design
and displays as something like this:
Zoho Creator: Copy Subform to other Subforms: Form

The code
I create a workflow that when the decision box "Copy Monday to the Other Days" is clicked it runs the following deluge script:
copyraw
// only run if true
if(input.Copy_Monday_to_the_Other_Days)
{
    // init
    c_Tuesday = Collection();
    c_Wednesday = Collection();
    c_Thursday = Collection();
    c_Friday = Collection();
    c_Saturday = Collection();
    //
    // loop through Monday subform
    for each r_Row in input.Mondays
    {
        if(r_Row.In_Effect)
        {
            // get Monday entries/rows
            v_EventType = r_Row.Event_Type;
            v_EventStart = r_Row.Start_Time;
            v_EventEnd = r_Row.End_Time;
            //
            // create Tuesdays subform entries
            r_RowTu = Shifts.Tuesdays();
            r_RowTu.Event_Type = v_EventType;
            r_RowTu.Start_Time = v_EventStart;
            r_RowTu.End_Time = v_EventEnd;
            r_RowTu.In_Effect = true;
            c_Tuesday.insert(r_RowTu);
            //
            // create Wednesdays subform entries
            r_RowWe = Shifts.Wednesdays();
            r_RowWe.Event_Type = v_EventType;
            r_RowWe.Start_Time = v_EventStart;
            r_RowWe.End_Time = v_EventEnd;
            r_RowWe.In_Effect = true;
            c_Wednesday.insert(r_RowWe);
            //
            // create Thursdays subform entries
            r_RowTh = Shifts.Thursdays();
            r_RowTh.Event_Type = v_EventType;
            r_RowTh.Start_Time = v_EventStart;
            r_RowTh.End_Time = v_EventEnd;
            r_RowTh.In_Effect = true;
            c_Thursday.insert(r_RowTh);
            //
            // create Fridays subform entries
            r_RowFr = Shifts.Fridays();
            r_RowFr.Event_Type = v_EventType;
            r_RowFr.Start_Time = v_EventStart;
            r_RowFr.End_Time = v_EventEnd;
            r_RowFr.In_Effect = true;
            c_Friday.insert(r_RowFr);
            //
            // create Saturdays subform entries
            r_RowSa = Shifts.Saturdays();
            r_RowSa.Event_Type = v_EventType;
            r_RowSa.Start_Time = v_EventStart;
            r_RowSa.End_Time = v_EventEnd;
            r_RowSa.In_Effect = true;
            c_Saturday.insert(r_RowSa);
        }
    } 
    //
    // clear the other subforms of their entries
    input.Tuesdays.clear();
    input.Wednesdays.clear();
    input.Thursdays.clear();
    input.Fridays.clear();
    input.Saturdays.clear();
    //
    // populate the other subforms with our collections
    input.Tuesdays.insert(c_Tuesday);
    input.Wednesdays.insert(c_Wednesday);
    input.Thursdays.insert(c_Thursday);
    input.Fridays.insert(c_Friday);
    input.Saturdays.insert(c_Saturday);
    //
    // [OPTIONAL] Reset the decision box to false
    input.Copy_Monday_to_the_Other_Days = false;
}
  1.  // only run if true 
  2.  if(input.Copy_Monday_to_the_Other_Days) 
  3.  { 
  4.      // init 
  5.      c_Tuesday = Collection()
  6.      c_Wednesday = Collection()
  7.      c_Thursday = Collection()
  8.      c_Friday = Collection()
  9.      c_Saturday = Collection()
  10.      // 
  11.      // loop through Monday subform 
  12.      for each r_Row in input.Mondays 
  13.      { 
  14.          if(r_Row.In_Effect) 
  15.          { 
  16.              // get Monday entries/rows 
  17.              v_EventType = r_Row.Event_Type; 
  18.              v_EventStart = r_Row.Start_Time; 
  19.              v_EventEnd = r_Row.End_Time; 
  20.              // 
  21.              // create Tuesdays subform entries 
  22.              r_RowTu = Shifts.Tuesdays()
  23.              r_RowTu.Event_Type = v_EventType; 
  24.              r_RowTu.Start_Time = v_EventStart; 
  25.              r_RowTu.End_Time = v_EventEnd; 
  26.              r_RowTu.In_Effect = true
  27.              c_Tuesday.insert(r_RowTu)
  28.              // 
  29.              // create Wednesdays subform entries 
  30.              r_RowWe = Shifts.Wednesdays()
  31.              r_RowWe.Event_Type = v_EventType; 
  32.              r_RowWe.Start_Time = v_EventStart; 
  33.              r_RowWe.End_Time = v_EventEnd; 
  34.              r_RowWe.In_Effect = true
  35.              c_Wednesday.insert(r_RowWe)
  36.              // 
  37.              // create Thursdays subform entries 
  38.              r_RowTh = Shifts.Thursdays()
  39.              r_RowTh.Event_Type = v_EventType; 
  40.              r_RowTh.Start_Time = v_EventStart; 
  41.              r_RowTh.End_Time = v_EventEnd; 
  42.              r_RowTh.In_Effect = true
  43.              c_Thursday.insert(r_RowTh)
  44.              // 
  45.              // create Fridays subform entries 
  46.              r_RowFr = Shifts.Fridays()
  47.              r_RowFr.Event_Type = v_EventType; 
  48.              r_RowFr.Start_Time = v_EventStart; 
  49.              r_RowFr.End_Time = v_EventEnd; 
  50.              r_RowFr.In_Effect = true
  51.              c_Friday.insert(r_RowFr)
  52.              // 
  53.              // create Saturdays subform entries 
  54.              r_RowSa = Shifts.Saturdays()
  55.              r_RowSa.Event_Type = v_EventType; 
  56.              r_RowSa.Start_Time = v_EventStart; 
  57.              r_RowSa.End_Time = v_EventEnd; 
  58.              r_RowSa.In_Effect = true
  59.              c_Saturday.insert(r_RowSa)
  60.          } 
  61.      } 
  62.      // 
  63.      // clear the other subforms of their entries 
  64.      input.Tuesdays.clear()
  65.      input.Wednesdays.clear()
  66.      input.Thursdays.clear()
  67.      input.Fridays.clear()
  68.      input.Saturdays.clear()
  69.      // 
  70.      // populate the other subforms with our collections 
  71.      input.Tuesdays.insert(c_Tuesday)
  72.      input.Wednesdays.insert(c_Wednesday)
  73.      input.Thursdays.insert(c_Thursday)
  74.      input.Fridays.insert(c_Friday)
  75.      input.Saturdays.insert(c_Saturday)
  76.      // 
  77.      // [OPTIONAL] Reset the decision box to false 
  78.      input.Copy_Monday_to_the_Other_Days = false
  79.  } 

The Result
So now when I tick this decision box, I get something like the following:
Zoho Creator: Copy Subform to other Subforms: Result


Source(s):
Category: Zoho :: Article: 785