For Zoho services only


I'm currently part of a wider delivery team at Ascent Business Solutions, recognised as a leading Zoho Premium Solutions Partner in the United Kingdom.

Ascent Business Solutions support organisations with everything from targeted technical fixes through to full Zoho CRM implementations and long-term platform adoption. Working as a team rather than a one-person consultancy allows projects to move forward consistently, with access to the right skills at each stage.

The team I manage specialises in API integrations between Zoho and third-party finance and commerce platforms such as Xero, Shopify, WooCommerce, and eBay. Much of our work involves solving integration challenges that fall outside standard documentation, supporting new ideas, new sectors, and evolving business models.

Success is measured through practical outcomes and return on investment, ranging from scaling small operations into high-turnover businesses to delivering rapid gains through online payments, automation, and streamlined digital workflows.

If you are looking for structured Zoho expertise backed by an established consultancy, you can contact Ascent Business Solutions on 0121 392 8140 (UK), email info@ascentbusiness.co.uk, or visit https://www.ascentbusiness.co.uk.

Zoho Deluge - First Monday of Month or Last Tuesday of Month

What?
An article on setting a date field to either the first Monday of the next month or to the last Tuesday of the current/next month. What I mean by the last Tuesday is if the last Tuesday of the month is before the current date, then set it to the last Tuesday of next month.

Why?
This was a request by a client who instead of specifying the 1st of every month, or 15th of each month, to say the first Monday of the month (ignoring bank holidays) or to say the last Tuesday of the month.

How?
Quite easily. For the first Monday of the month, it is likely that even if today was the first day of the month and coincidentally a Monday, then we would want to specify the field date to be the first Monday of the next month. If however we want the last Tuesday of the month and that Tuesday happens before today, then we want the last Tuesday of the next month.

In the following code, consider that the date field we want to set is called "Switch_Over_Day". For the first Monday, we are going to select the first day of the month and then loop until we find a Monday. Same for the last Tuesday except that we will count backwards from the end of the month:
copyraw
if(input.Switch_Over_Day=="First Monday of Month")
{
    v_NextMonthDate = zoho.currentdate.addMonth(1);
    v_NextMonthDateFirst = v_NextMonthDate.toStartOfMonth();
    if(v_NextMonthDateFirst.getDayOfWeek()!=2)
    {
        for each v_MyDay in {1,2,3,4,5,6,7,8}
        {
            v_NextMonthDateFirst = v_NextMonthDateFirst.toDate();
            v_NextMonthMonday = v_NextMonthDateFirst.addDay(v_MyDay);
            if(v_NextMonthMonday.getDayOfWeek()==2)
            {
                break;
            }
        } 
        input.Next_Switch_Date = v_NextMonthMonday.toDate();
    }
}
else if(input.Switch_Over_Day=="Last Tuesday of Month")
{
    v_NextMonthDate = zoho.currentdate.addMonth(1);
    v_NextMonthDateLast = v_NextMonthDate.toStartOfMonth().subDay(1);
    if(v_NextMonthDateLast.getDayOfWeek()!=3)
    {
        for each v_MyDay in {1,2,3,4,5,6,7,8}
        {
            v_NextMonthDateLast = v_NextMonthDateLast.toDate();
            v_NextMonthTuesday = v_NextMonthDateLast.subDay(v_MyDay);
            if(v_NextMonthTuesday.getDayOfWeek()==3)
            {
                break;
            }
        } 
        // if last Tuesday is before today
        if(v_NextMonthTuesday.toDate() <= zoho.currentdate)
        {
            v_NextMonthDate = zoho.currentdate.addMonth(2);
            v_NextMonthDateLast = v_NextMonthDate.toStartOfMonth().subDay(1);
            if(v_NextMonthDateLast.getDayOfWeek()!=3)
            {
                for each v_MyDay in {1,2,3,4,5,6,7,8}
                {
                    v_NextMonthDateLast = v_NextMonthDateLast.toDate();
                    v_NextMonthTuesday = v_NextMonthDateLast.subDay(v_MyDay);
                    if(v_NextMonthTuesday.getDayOfWeek()==3)
                    {
                        break;
                    }
                } 
            }
        }
        input.Next_Switch_Date = v_NextMonthTuesday.toDate();
    }
}
  1.  if(input.Switch_Over_Day=="First Monday of Month") 
  2.  { 
  3.      v_NextMonthDate = zoho.currentdate.addMonth(1)
  4.      v_NextMonthDateFirst = v_NextMonthDate.toStartOfMonth()
  5.      if(v_NextMonthDateFirst.getDayOfWeek()!=2) 
  6.      { 
  7.          for each v_MyDay in {1,2,3,4,5,6,7,8} 
  8.          { 
  9.              v_NextMonthDateFirst = v_NextMonthDateFirst.toDate()
  10.              v_NextMonthMonday = v_NextMonthDateFirst.addDay(v_MyDay)
  11.              if(v_NextMonthMonday.getDayOfWeek()==2) 
  12.              { 
  13.                  break; 
  14.              } 
  15.          } 
  16.          input.Next_Switch_Date = v_NextMonthMonday.toDate()
  17.      } 
  18.  } 
  19.  else if(input.Switch_Over_Day=="Last Tuesday of Month") 
  20.  { 
  21.      v_NextMonthDate = zoho.currentdate.addMonth(1)
  22.      v_NextMonthDateLast = v_NextMonthDate.toStartOfMonth().subDay(1)
  23.      if(v_NextMonthDateLast.getDayOfWeek()!=3) 
  24.      { 
  25.          for each v_MyDay in {1,2,3,4,5,6,7,8} 
  26.          { 
  27.              v_NextMonthDateLast = v_NextMonthDateLast.toDate()
  28.              v_NextMonthTuesday = v_NextMonthDateLast.subDay(v_MyDay)
  29.              if(v_NextMonthTuesday.getDayOfWeek()==3) 
  30.              { 
  31.                  break; 
  32.              } 
  33.          } 
  34.          // if last Tuesday is before today 
  35.          if(v_NextMonthTuesday.toDate() <= zoho.currentdate) 
  36.          { 
  37.              v_NextMonthDate = zoho.currentdate.addMonth(2)
  38.              v_NextMonthDateLast = v_NextMonthDate.toStartOfMonth().subDay(1)
  39.              if(v_NextMonthDateLast.getDayOfWeek()!=3) 
  40.              { 
  41.                  for each v_MyDay in {1,2,3,4,5,6,7,8} 
  42.                  { 
  43.                      v_NextMonthDateLast = v_NextMonthDateLast.toDate()
  44.                      v_NextMonthTuesday = v_NextMonthDateLast.subDay(v_MyDay)
  45.                      if(v_NextMonthTuesday.getDayOfWeek()==3) 
  46.                      { 
  47.                          break; 
  48.                      } 
  49.                  } 
  50.              } 
  51.          } 
  52.          input.Next_Switch_Date = v_NextMonthTuesday.toDate()
  53.      } 
  54.  } 

All Variations
How about the code for all variations such as "First Wednesday of Month", "Third Thursday of Month", "Last Friday of Month"? So in the following code we do this, we're checking that the option selected doesn't have a digit and then checking for a day name and then checking if "First", "Second", "Third", "Fourth" or "Last" day (no point in "Fifth" as "Last" ought to be similar):
copyraw
v_DayFreq = 0;
l_AllDays = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32};
v_InputSwitch = input.Switch_Over_Day;
v_InputSwitchCheck = v_InputSwitch.replaceAll("[^0-9]","");
if(v_InputSwitchCheck == "")
{
	// what day of week
	if(v_InputSwitch.toString().indexOf("Monday") > 0)
	{
		v_DayIndex = 2;
	}
	else if(v_InputSwitch.toString().indexOf("Tuesday") > 0)
	{
		v_DayIndex = 3;
	}
	else if(v_InputSwitch.toString().indexOf("Wednesday") > 0)
	{
		v_DayIndex = 4;
	}
	else if(v_InputSwitch.toString().indexOf("Thursday") > 0)
	{
		v_DayIndex = 5;
	}
	else if(v_InputSwitch.toString().indexOf("Friday") > 0)
	{
		v_DayIndex = 6;
	}
	// occurrence
	if(v_InputSwitch.toString().indexOf("First") >= 0)
	{
		v_DayOccur = 1;
	}
	else if(v_InputSwitch.toString().indexOf("Second") >= 0)
	{
		v_DayOccur = 2;
	}
	else if(v_InputSwitch.toString().indexOf("Third") >= 0)
	{
		v_DayOccur = 3;
	}
	else if(v_InputSwitch.toString().indexOf("Fourth") >= 0)
	{
		v_DayOccur = 4;
	}
	else if(v_InputSwitch.toString().indexOf("Last") >= 0)
	{
		v_DayOccur = -1;
	}
	//
	// calculate date
	v_NextMonthDate = zoho.currentdate;
	if(v_DayOccur > 0)
	{
		v_NextDateStart = v_NextMonthDate.toStartOfMonth().subDay(1);
		for each  v_MyDay in l_AllDays
		{
			v_NextDate = v_NextDateStart.addDay(v_MyDay);
			if(v_NextDate.getDayOfWeek() == v_DayIndex)
			{
				v_DayFreq = v_DayFreq + 1;
				if(v_DayOccur == v_DayFreq)
				{
					break;
				}
			}
		}
		// if next Day is today or before today
		if(v_NextDate.toDate() <= zoho.currentdate)
		{
			v_DayFreq = 0;
			v_NextMonthDate = zoho.currentdate.addMonth(1);
			v_NextDateStart = v_NextMonthDate.toStartOfMonth();
			if(v_NextDateStart.getDayOfWeek() != v_DayIndex)
			{
				for each  v_MyDay in l_AllDays
				{
					v_NextDate = v_NextDateStart.addDay(v_MyDay);
					if(v_NextDate.getDayOfWeek() == v_DayIndex)
					{
						v_DayFreq = v_DayFreq + 1;
						if(v_DayOccur == v_DayFreq)
						{
							break;
						}
					}
				}
			}
			else
			{
				v_NextDate = v_NextDateStart.toDate();
			}
		}
	}
	else
	{
		v_NextMonthDate = zoho.currentdate.addMonth(1);
		v_NextDateStart = v_NextMonthDate.toStartOfMonth().subDay(1);
		if(v_NextDateStart.getDayOfWeek() != v_DayIndex)
		{
			for each  v_MyDay in l_AllDays
			{
				v_NextDate = v_NextDateStart.subDay(v_MyDay);
				if(v_NextDate.getDayOfWeek() == v_DayIndex)
				{
					break;
				}
			}
			// if last Day is before today
			if(v_NextDate.toDate() <= zoho.currentdate)
			{
				v_NextMonthDate = zoho.currentdate.addMonth(2);
				v_NextDateStart = v_NextMonthDate.toStartOfMonth().subDay(1);
				if(v_NextDateStart.getDayOfWeek() != v_DayIndex)
				{
					for each  v_MyDay in l_AllDays
					{
						v_NextDate = v_NextMonthDate.subDay(v_MyDay);
						if(v_NextDate.getDayOfWeek() == v_DayIndex)
						{
							break;
						}
					}
				}
			}
		}
		else
		{
			v_NextDate = v_NextDateStart.toDate();
		}
	}
	input.Next_Switch_Date = v_NextDate.toDate();
}
  1.  v_DayFreq = 0
  2.  l_AllDays = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32}
  3.  v_InputSwitch = input.Switch_Over_Day; 
  4.  v_InputSwitchCheck = v_InputSwitch.replaceAll("[^0-9]","")
  5.  if(v_InputSwitchCheck == "") 
  6.  { 
  7.      // what day of week 
  8.      if(v_InputSwitch.toString().indexOf("Monday") > 0) 
  9.      { 
  10.          v_DayIndex = 2
  11.      } 
  12.      else if(v_InputSwitch.toString().indexOf("Tuesday") > 0) 
  13.      { 
  14.          v_DayIndex = 3
  15.      } 
  16.      else if(v_InputSwitch.toString().indexOf("Wednesday") > 0) 
  17.      { 
  18.          v_DayIndex = 4
  19.      } 
  20.      else if(v_InputSwitch.toString().indexOf("Thursday") > 0) 
  21.      { 
  22.          v_DayIndex = 5
  23.      } 
  24.      else if(v_InputSwitch.toString().indexOf("Friday") > 0) 
  25.      { 
  26.          v_DayIndex = 6
  27.      } 
  28.      // occurrence 
  29.      if(v_InputSwitch.toString().indexOf("First") >= 0) 
  30.      { 
  31.          v_DayOccur = 1
  32.      } 
  33.      else if(v_InputSwitch.toString().indexOf("Second") >= 0) 
  34.      { 
  35.          v_DayOccur = 2
  36.      } 
  37.      else if(v_InputSwitch.toString().indexOf("Third") >= 0) 
  38.      { 
  39.          v_DayOccur = 3
  40.      } 
  41.      else if(v_InputSwitch.toString().indexOf("Fourth") >= 0) 
  42.      { 
  43.          v_DayOccur = 4
  44.      } 
  45.      else if(v_InputSwitch.toString().indexOf("Last") >= 0) 
  46.      { 
  47.          v_DayOccur = -1
  48.      } 
  49.      // 
  50.      // calculate date 
  51.      v_NextMonthDate = zoho.currentdate; 
  52.      if(v_DayOccur > 0) 
  53.      { 
  54.          v_NextDateStart = v_NextMonthDate.toStartOfMonth().subDay(1)
  55.          for each  v_MyDay in l_AllDays 
  56.          { 
  57.              v_NextDate = v_NextDateStart.addDay(v_MyDay)
  58.              if(v_NextDate.getDayOfWeek() == v_DayIndex) 
  59.              { 
  60.                  v_DayFreq = v_DayFreq + 1
  61.                  if(v_DayOccur == v_DayFreq) 
  62.                  { 
  63.                      break; 
  64.                  } 
  65.              } 
  66.          } 
  67.          // if next Day is today or before today 
  68.          if(v_NextDate.toDate() <= zoho.currentdate) 
  69.          { 
  70.              v_DayFreq = 0
  71.              v_NextMonthDate = zoho.currentdate.addMonth(1)
  72.              v_NextDateStart = v_NextMonthDate.toStartOfMonth()
  73.              if(v_NextDateStart.getDayOfWeek() != v_DayIndex) 
  74.              { 
  75.                  for each  v_MyDay in l_AllDays 
  76.                  { 
  77.                      v_NextDate = v_NextDateStart.addDay(v_MyDay)
  78.                      if(v_NextDate.getDayOfWeek() == v_DayIndex) 
  79.                      { 
  80.                          v_DayFreq = v_DayFreq + 1
  81.                          if(v_DayOccur == v_DayFreq) 
  82.                          { 
  83.                              break; 
  84.                          } 
  85.                      } 
  86.                  } 
  87.              } 
  88.              else 
  89.              { 
  90.                  v_NextDate = v_NextDateStart.toDate()
  91.              } 
  92.          } 
  93.      } 
  94.      else 
  95.      { 
  96.          v_NextMonthDate = zoho.currentdate.addMonth(1)
  97.          v_NextDateStart = v_NextMonthDate.toStartOfMonth().subDay(1)
  98.          if(v_NextDateStart.getDayOfWeek() != v_DayIndex) 
  99.          { 
  100.              for each  v_MyDay in l_AllDays 
  101.              { 
  102.                  v_NextDate = v_NextDateStart.subDay(v_MyDay)
  103.                  if(v_NextDate.getDayOfWeek() == v_DayIndex) 
  104.                  { 
  105.                      break; 
  106.                  } 
  107.              } 
  108.              // if last Day is before today 
  109.              if(v_NextDate.toDate() <= zoho.currentdate) 
  110.              { 
  111.                  v_NextMonthDate = zoho.currentdate.addMonth(2)
  112.                  v_NextDateStart = v_NextMonthDate.toStartOfMonth().subDay(1)
  113.                  if(v_NextDateStart.getDayOfWeek() != v_DayIndex) 
  114.                  { 
  115.                      for each  v_MyDay in l_AllDays 
  116.                      { 
  117.                          v_NextDate = v_NextMonthDate.subDay(v_MyDay)
  118.                          if(v_NextDate.getDayOfWeek() == v_DayIndex) 
  119.                          { 
  120.                              break; 
  121.                          } 
  122.                      } 
  123.                  } 
  124.              } 
  125.          } 
  126.          else 
  127.          { 
  128.              v_NextDate = v_NextDateStart.toDate()
  129.          } 
  130.      } 
  131.      input.Next_Switch_Date = v_NextDate.toDate()
  132.  } 

Additional Note(s):
  • The first day of the week in Creator is Sunday=1
  • The day index is as follows: Sunday=1, Monday=2, Tuesday=3, Wednesday=4, Thursday=5, Friday=6, Saturday=7
Category: Zoho Deluge :: Article: 247

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com