Update a given date on some conditions

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

  Discussion posts and replies are publicly visible

Parents
  • 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
          )
        )
      )
    )

Reply
  • 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
          )
        )
      )
    )

Children