Skip to main content
March 16, 2023
Solved

Update a given date on some conditions

  • March 16, 2023
  • 8 replies
  • 0 views

Hi,

I have a requirement to update a date based on a condition that if the given date is of last 2 dates of that particular month, then I want my rule to return first date of the next month and it should be applicable for all the dates whether the month has 30 or 31 days or if it is February or leap year.

For eg: 27/02/2023 should be converted to 01/03/2023.

I have written following code for this, but my senior asks me to optimize the code and reduce the use of if conditions. Please tell me what else I can do

edit:

a!localVariables(
  local!currentMonth: month(ri!requestDate),
  local!currentDay: day(ri!requestDate),
  local!currentYear: year(ri!requestDate),
  local!isLeapYear: isleapyear(local!currentYear),
  todate(
    if(
      and(
        tointeger(local!currentMonth) = 2,
        or(local!isLeapYear),
        contains(
          tointeger({ 28, 29 }),
          tointeger(local!currentDay)
        )
      ),
      concat(
        tointeger(local!currentMonth) + 1,
        "/",
        1,
        "/",
        local!currentYear
      ),
      if(
        and(
          tointeger(local!currentMonth) = 2,
          not(or(local!isLeapYear)),
          contains(
            tointeger({ 27, 28 }),
            tointeger(local!currentDay)
          )
        ),
        concat(
          tointeger(local!currentMonth) + 1,
          "/",
          1,
          "/",
          local!currentYear
        ),
        if(
          and(
            tointeger(local!currentMonth) <> 2,
            contains(
              tointeger({ 30, 31 }),
              tointeger(local!currentDay)
            )
          ),
          concat(
            if(
              tointeger(local!currentMonth) = 12,
              1,
              local!currentMonth + 1
            ),
            "/",
            1,
            "/",
            if(
              tointeger(local!currentMonth) = 12,
              local!currentYear + 1,
              local!currentYear
            )
          ),
          ri!requestDate
        )
      )
    )
  )
)

Thanks you for help

Best answer by mikes0011

For the longest time I didn't know about the function "eomonth()" and it probably caused me a lot of heartburn - i figure it might help you out here.

I'm a little unclear what's being requested when ri!requestDate isn't one of the last 2 days of the given month, but this seems to work for what I understand of your requirement:

a!localVariables(
  
  local!eomonthDate: eomonth(ri!requestDate, 0),
  local!nextMonthStartDate: local!eomonthDate + 1,
  
  local!isApplicableDate: or(
    local!eomonthDate = {ri!requestDate, ri!requestDate + 1}
  ),
  
  if(
    local!isApplicableDate,
    local!nextMonthStartDate,
    ri!requestDate
  )
)

8 replies

March 16, 2023

First of all, edit this question and re paste your code in a code box.

March 16, 2023

Sorry for that I have edited it

April 19, 2023

Thank you so much for the response.

mikes0011
mikes0011Answer
Brainy
March 16, 2023

For the longest time I didn't know about the function "eomonth()" and it probably caused me a lot of heartburn - i figure it might help you out here.

I'm a little unclear what's being requested when ri!requestDate isn't one of the last 2 days of the given month, but this seems to work for what I understand of your requirement:

a!localVariables(
  
  local!eomonthDate: eomonth(ri!requestDate, 0),
  local!nextMonthStartDate: local!eomonthDate + 1,
  
  local!isApplicableDate: or(
    local!eomonthDate = {ri!requestDate, ri!requestDate + 1}
  ),
  
  if(
    local!isApplicableDate,
    local!nextMonthStartDate,
    ri!requestDate
  )
)

March 16, 2023

Frankly speaking I didn't had enough patience and dare to look into what you have written in your code but AFAIU with the description, you can make it work like this by using some OOTB Appian functions and a simple logic.

a!localVariables(
  local!currentMonth: month(ri!requestDate),
  local!currentDay: day(ri!requestDate),
  local!currentYear: year(ri!requestDate),
  local!daysDifference: daysinmonth(local!currentMonth, local!currentYear) - local!currentDay,
  todate(
    adddays(
      todatetime(ri!requestDate),
      a!match(
        value: local!daysDifference,
        equals: 0,
        then: 1,
        equals: 1,
        then: 2,
        default: 0
      )
    )
  )
)

March 16, 2023

Thank you so much for the response, this is exactly what I needed I'm amazed