How to generate last monday date based on today's date?

HI WF,

I have requirement to select last monday’s date based on today’s date and save it as string variable.

Please suggest me options, except reading from excel.

Regards,
Narendra

Hi @narendra_purTG try using this approach:

  1. assign the current date to a variable (for example, ${date1})

  2. get the weekday of the current day using this groovy code in Constant value action: ${date1.getDayOfWeek()}

  1. use While loop with condition (if ${weekday} != MONDAY)
  2. inside the loop:
  • use a simple custom action to subtract 1 day:
@CustomScriptAction(
    input = ['date1'], 
    output = 'date1'
    )
    def customeScript() {
    date1 = date1.minus(java.time.Duration.ofDays(1))
    }

  • use the Constant value action again to get the weekday of the resulting day

Once the weekday equals Monday, the bot will exit the loop and ${date1} will contain the correct date

  1. then just convert ${date1} from date to string

Hi @ashapkina,

I’m getting below error in custom actions script.

Error executing CustomScriptAction
com.workfusion.studio.rpa.recorder.playback.PlaybackException: Error executing TemplateAction[templateName=CustomScriptAction.ftl,id=4,name=Optional[CustomScriptAction],parent=3,nextSibling=5,arguments=ActionArguments[delay=[0],xsi:type=[recorder:CustomScriptAction, recorder:CustomScriptAction],name=[Generate Date],pollingInterval=[300],active=[true],actionDetails=[(Generate Date)],script=[@CustomScriptAction(
input = [‘date1’],
output = ‘date1’
)
def customeScript() {
date1 = date1.minus(java.time.Duration.ofDays(1))
],awaitTimeout=[5000]]]
at com.workfusion.studio.rpa.recorder.playback.flow.StandardControlFlow.execute(StandardControlFlow.java:54)
at com.workfusion.studio.rpa.recorder.playback.action.template.TemplateAction.execute(TemplateAction.java:28)
at com.workfusion.studio.rpa.recorder.playback.action.template.TemplateAction.execute(TemplateAction.java:15)
at com.workfusion.studio.rpa.recorder.playback.player.ActionPlayer.next(ActionPlayer.java:53)
at com.workfusion.studio.rpa.recorder.player.PlaybackLogic.playNextAction(PlaybackLogic.java:152)
at com.workfusion.studio.rpa.recorder.player.PlaybackLogic.run(PlaybackLogic.java:112)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script5.groovy: 13: expecting ‘}’, found ‘’ @ line 13, column 1.

Regards,
Narendra

Looks like the last bracket is missing at the end of the script.

@CustomScriptAction(
    input = ['date1'],
    output = 'date1'
    )
    def customeScript() {
    date1 = date1.minus(java.time.Duration.ofDays(1))
    }

Hi,
This is the script i used now

@CustomScriptAction
(
input = [‘date1’],
output = ‘date2’
)
def customeScript() {
date2 = date1.minus(java.time.Duration.ofDays(1))
}

Error executing CustomScriptAction
com.workfusion.studio.rpa.recorder.playback.PlaybackException: Error executing TemplateAction[templateName=CustomScriptAction.ftl,id=4,name=Optional[CustomScriptAction],parent=3,nextSibling=5,arguments=ActionArguments[delay=[0],xsi:type=[recorder:CustomScriptAction, recorder:CustomScriptAction],name=[Generate Date],pollingInterval=[300],active=[true],actionDetails=[(Generate Date)],script=[@CustomScriptAction
(
input = [‘date1’],
output = ‘date2’
)
def customeScript() {
date2 = date1.minus(java.time.Duration.ofDays(1))
}],awaitTimeout=[5000]]]
at com.workfusion.studio.rpa.recorder.playback.flow.StandardControlFlow.execute(StandardControlFlow.java:54)
at com.workfusion.studio.rpa.recorder.playback.action.template.TemplateAction.execute(TemplateAction.java:28)
at com.workfusion.studio.rpa.recorder.playback.action.template.TemplateAction.execute(TemplateAction.java:15)
at com.workfusion.studio.rpa.recorder.playback.player.ActionPlayer.next(ActionPlayer.java:53)
at com.workfusion.studio.rpa.recorder.player.PlaybackLogic.playNextAction(PlaybackLogic.java:152)
at com.workfusion.studio.rpa.recorder.player.PlaybackLogic.run(PlaybackLogic.java:112)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
General error during canonicalization: Output parameter should be defined as variable name

Hi @ashapkina,

Please ignore. Got the result. Thank you for the help.

Regards,
Narendra

1 Like

Hi @ashapkina,

Can we get yesterday’s date using this script.

I tried using date1 = date1.minus(java.time.Duration.ofDays(2)) in custom script, however the output is always Monday.

Appreciate the help on this.

Thanks,
Narendra

Do you need just the date, not the day of the week?

In this case you just need to use this custom script without the Constant value action that returned the day of the week.

@CustomScriptAction(
input = ['date1'], 
output = 'date2'
)

def customeScript() {
date2 = date1.minus(java.time.Duration.ofDays(1))
}

Or even easier, you can use this code to return yesterday’s date.

@CustomScriptAction(
   output = 'yesterday'
   )

   def customeScript() {
   RDateTime.now().minus(java.time.Duration.ofDays(1))
   }

In this case, you don’t need the second date variable.

Hi @ashapkina,

Thanks for the inputs. This was my exact requirement.

Regards,
Narendra

1 Like

Hello @ashapkina,

I am trying to get Day for a particular week as a number as return type and using getDayOfWeek() and save in a variable and the same variable I want to subtract from today’s Date,

Eg- For Today:-

If today is 18/09/2018. I want today’s weekday: as 03 ( Considering week is starting from Sunday and as today is Tuesday, so 03 as output)
Now, I want to save that output in a variable, say - day_number
Expecting, the return type as Integer. So the variable will store - 03

Now, I want to subtract that variable in the previous function shared by you. Below code will represent the above -

@CustomScriptAction(
 	 
     output = 'previous'
 )
  
 def customScript() {
      def my_date = RDateTime.now()
      def day_number = my_date.getDayOfWeek() // **Here My code is failing**
      def previous = my_date.minus(java.time.Duration.ofDays(day_number))
      
 }

If you can help me with getDayOfWeek() or some logic that would return weekday as Numberthen it would save a huge time of mine. Thanking you in advance.
Note, Please let me know if you need more information related to this.

So basically you always need to get the date of the last Saturday, right?
Then you can use the same code as above when you needed to get the date of Monday.

@ashapkina No, I am looking for the Integer value of any particular day.

getDayOfWeek() always generates a String.

But you can convert it to Integer with a couple of recorder actions.

You can create a Table recorder variable with 2 columns : 1st for the number of the weekday, 2nd for the name of the weekday.

image

After you get the weekday (you can do it in a Constant value actions instead of the custom action) you can iterate through the table and get the number which corresponds to the weekday.

Then you can convert it to an integer using Number Format action.

I’ve attached a sample recording (it was done using version 2.1.0.).previous-date.rar (965 Bytes)

Note: you cannot use an Integer in RDateTime.minus() method. It required TemporalAmount, not Number.

Hi @narendra_purTG,

Java supports getting the numeric value of weekday with function getValue(). So, to get the Integer value of any particular day you can use this line:

def day_number = my_date.getDayOfWeek().getValue()
1 Like

@adimitriou Thank you. It is working now. Can anyone please tell me how to get two output from RPA Script as a recorder variable? Note- in RPA I have already created those as a Recorder variable -

I want something like below- and not working -

@CustomScriptAction(
       output = ['previous','week_start_day']
   )

I don’t know if something changed but at version 2.0.1 I’m using there was available only one output variable, so I’m using a list with predefined order(to know which place has what var value) to return more than one values and read them later.

1 Like

Yes, 1 output variable is available in custom action. It hasn’t been changed.

Hi @ashapkina
I have done the same steps but it is not working
Kindly advice

What actions did you have exactly? Can you share your recording?