--- /dev/null
+/*\r
+ * SPDX-FileCopyrightText: 2024 Chris Duncan <chris@flowpex.dev>\r
+ * SPDX-License-Identifier: GPL-3.0-or-later\r
+ */\r
+\r
+global class System_Date_daysInMonth {\r
+\r
+ @InvocableMethod(label='System.Date.daysInMonth()' category='Flowpex' iconName='slds:standard:number_input' description='Returns the number of days in the month for the specified year and month (1=Jan).')\r
+\r
+ global static List<Response> System_Date_daysInMonth (List<Request> requests) {\r
+ List<Response> responses = new List<Response>();\r
+ for (Request req : requests) {\r
+ Response res = new Response();\r
+ try {\r
+ res.daysInMonth = Date.daysInMonth(req.year, req.month);\r
+ } catch (Exception e) {\r
+ System.debug(e);\r
+ }\r
+ responses.add(res);\r
+ }\r
+ return responses;\r
+ }\r
+\r
+ global class Request {\r
+ @InvocableVariable(required='true' label='Year' description='Year to use in calculating number of days in specified month.')\r
+ global Integer year;\r
+ \r
+ @InvocableVariable(required='true' label='Month' description='Month from which to return number of days.')\r
+ global Integer month;\r
+\r
+ global Request () {}\r
+ global Request (Integer y, Integer m) {\r
+ this.year = y;\r
+ this.month = m;\r
+ }\r
+ }\r
+\r
+ global class Response {\r
+ @InvocableVariable(label='Days in Month' description='Number of days between two provided dates.')\r
+ global Integer daysInMonth;\r
+ }\r
+}
\ No newline at end of file
--- /dev/null
+/*\r
+ * SPDX-FileCopyrightText: 2024 Chris Duncan <chris@flowpex.dev>\r
+ * SPDX-License-Identifier: GPL-3.0-or-later\r
+ */\r
+\r
+@isTest\r
+private class System_Date_daysInMonth_TEST {\r
+\r
+ @isTest\r
+ static void testNoArgs () {\r
+ List<System_Date_daysInMonth.Request> requests = new List<System_Date_daysInMonth.Request>();\r
+\r
+ Test.startTest();\r
+ List<System_Date_daysInMonth.Response> responses = System_Date_daysInMonth.System_Date_daysInMonth(requests);\r
+ Test.stopTest();\r
+\r
+ Assert.areEqual(0, responses.size());\r
+ }\r
+\r
+ @isTest\r
+ static void testEmptyRequest () {\r
+ List<System_Date_daysInMonth.Request> requests = new List<System_Date_daysInMonth.Request>();\r
+ System_Date_daysInMonth.Request req = new System_Date_daysInMonth.Request();\r
+ requests.add(req);\r
+\r
+ Test.startTest();\r
+ List<System_Date_daysInMonth.Response> responses = System_Date_daysInMonth.System_Date_daysInMonth(requests);\r
+ Test.stopTest();\r
+\r
+ Assert.areEqual(1, responses.size());\r
+ Assert.isNull(responses[0].daysInMonth);\r
+ }\r
+\r
+ @isTest\r
+ static void testBlankArgs () {\r
+ List<System_Date_daysInMonth.Request> requests = new List<System_Date_daysInMonth.Request>();\r
+ System_Date_daysInMonth.Request req = new System_Date_daysInMonth.Request(null, null);\r
+ requests.add(req);\r
+\r
+ Test.startTest();\r
+ List<System_Date_daysInMonth.Response> responses = System_Date_daysInMonth.System_Date_daysInMonth(requests);\r
+ Test.stopTest();\r
+\r
+ Assert.areEqual(1, responses.size());\r
+ Assert.isNull(responses[0].daysInMonth);\r
+ }\r
+\r
+ @isTest\r
+ static void testManyArgs () {\r
+ Date nonleapCentury = Date.newInstance(1900, 2, 1);\r
+ Date leapCentury = Date.newInstance(2000, 2, 1);\r
+ Date nonleapYear = Date.newInstance(2002, 2, 1);\r
+ Date leapYear = Date.newInstance(2004, 2, 1);\r
+ List<Date> dates = new List<Date>{nonleapCentury, leapCentury, nonleapYear, leapYear};\r
+\r
+ Test.startTest();\r
+ List<System_Date_daysInMonth.Request> requests = new List<System_Date_daysInMonth.Request>();\r
+ for (Date d : dates) {\r
+ System_Date_daysInMonth.Request req = new System_Date_daysInMonth.Request(d.year(), d.month());\r
+ requests.add(req);\r
+ }\r
+ List<System_Date_daysInMonth.Response> responses = System_Date_daysInMonth.System_Date_daysInMonth(requests);\r
+ Test.stopTest();\r
+\r
+ Assert.areEqual(4, responses.size());\r
+ for (Integer i = 0; i < 4; i++) {\r
+ System_Date_daysInMonth.Response res = responses[i];\r
+ Date d = dates[i];\r
+ Assert.isTrue(res.daysInMonth == 28 || res.daysInMonth == 29);\r
+ Assert.areEqual(Date.daysInMonth(d.year(), d.month()), res.daysInMonth);\r
+ }\r
+ }\r
+}
\ No newline at end of file