Skip to main content
Participating Frequently
Mar 16, 2023 6:13 PM UTC
Solved

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

mikes0011
mikes0011Answer
Brainy
Mar 16, 2023 6:25 PM UTC

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

The Expression Guru

8 replies

sanchitg0002
Inspiring
Mar 16, 2023 6:16 PM UTC

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

Participating Frequently
Mar 16, 2023 6:19 PM UTC

Sorry for that I have edited it

New Participant
Apr 19, 2023 9:59 AM UTC

Thank you so much for the response.

mikes0011
mikes0011Answer
Brainy
Mar 16, 2023 6:25 PM UTC

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

The Expression Guru
sanchitg0002
Inspiring
Mar 16, 2023 6:30 PM UTC

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

Participating Frequently
Mar 16, 2023 7:07 PM UTC

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