Capture Local date time in Custom Actions

Hi,
I am trying to capture local date time to a variable using custom action in java.
It is throwing exception at local date time class
I cannot import date time packages for the same, so how to implement various packages for the same in any custom action.
What is the other solution to print local date and time.

This is my code:

    <?xml version="1.0" encoding="UTF-8"?>
    <config xmlns="http://web-harvest.sourceforge.net/schema/1.0/config" scriptlang="groovy">
        <robotics-flow>
            <robot driver="universal" close-on-completion="true" start-in-private="true">
                <capability name="SEARCH_ALL_WINDOWS" value="true" />
                <script><![CDATA[
                    import com.workfusion.studio.rpa.recorder.api.internal.representation.*
                    import com.workfusion.studio.rpa.recorder.api.*
                    import com.workfusion.studio.rpa.recorder.api.types.*
                    import com.workfusion.studio.rpa.recorder.api.custom.*
                    import static com.workfusion.studio.rpa.recorder.api.RandomValues.CharacterSet.*
     
     def customscript()
     {
             LocalDateTime dtnow = LocalDateTime.now();  
               
     }
              customscript()	
           println dtnow  
              
                ]]></script>
            </robot>
        </robotics-flow>
        <export include-original-data="true"/>
    </config>

Hi,
The following works for me. I then use script actions to get the part of the string that I want. Paste the result “date” into notepad to see what comes back.

Hope that helps.

@CustomScriptAction(
output = 'date'
)

def customScript() {
def date = RDateTime.now()
}
2 Likes

This is the error i get while running your script:

Error executing CustomScriptAction
com.workfusion.studio.rpa.recorder.playback.PlaybackException: Error executing TemplateAction[templateName=CustomScriptAction.ftl,id=1,name=Optional[CustomScriptAction],parent=-1,arguments=ActionArguments[delay=[0],xsi:type=[recorder:CustomScriptAction, recorder:CustomScriptAction],name=[csnow],pollingInterval=[300],active=[true],actionDetails=[(csnow)],script=[@CustomScriptAction(
output = ‘date’
)

def customScript() {
def date = RDateTime.now()
}],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

As per error I have defined a variable named date in my recorder variable then to it is showing this error.
I will check the execution log to see if current date and time is stored in that ‘date’ variable or not.

My workflow:

Hi

I have my “date” variable defined as type “DateTime”. Not sure if that makes any difference.

The above error is definitely caused by not having the output variable defined in recorder (you always need both input and output variables defined in recorder exactly as used in the script).

2 Likes

Hi @Bonnero,
I understood your point,
I have defined output but, what will i use as an input for the same as i dont need any.
Just capturing the local date time in output variable.
So what to do?

Hi
It doesn’t need any input as this uses standard functionality. Here is a screenshot of how I’m using it in a little backup program for my RPAE files.

Then use the date format action to capture the part of the DateTime function that you need for your program.

1 Like

Just checked - this is the full string that I get when I execute this script:

2018-08-08T15:02:14+01:00[Europe/London]

Not sure if your execution will give your local time or if you will have to manipulate to get a difference to GMT. This might use the time location on the computer but I’m really not sure.

1 Like

@Bonnero the time zone is taken from Java on your machine.

Hi @Bonnero,

In your Recorder Variable you have defined ‘date’ variable as type DateTime and this variable has default value as Date,time,timezone,location. (Which you can see in Recorder Variables)
This isn’t result of your custom action.
Because RDateTime.now() function only gives date and time and not location and timezone.
So the output you get when you execute your script is 2018-08-08T15:02:14+01:00[Europe/London] because of the default value of ‘date’ variable.

Hi,

I’m using this script to get daytime for different time zones.
Where “my_timezone” is defined as String in Recorder with value i.e. “Europe/London” (without quotes).
Of course you can also change the output daytime format.

@CustomScriptAction(
    input = ['my_timezone'],    
    output = 'temp_date'   
)
 
def customScript() {

def now = new Date()
temp_date=RDateTime.fromRepresentation(now.format("yyyy-MM-dd HH:mm:ss", TimeZone.getTimeZone("${my_timezone}")), "yyyy-MM-dd HH:mm:ss;en-US;${my_timezone}")

}
2 Likes

Can you explain me in a short detail how did you put this code in a custom action.

@karan_dave there are several code samples in the KB that show how to code a custom action https://kb.workfusion.com/display/RPAe/Script+as+Custom+Action

Hi Asha ,

would request you to kindly share your reviews/knowledge on how to use custom script for gettig today’s time.

the one which am using is -

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

I have defined variable date1 with variable type datetime
and date2 with variable type string.

but am getting error message -

Error executing CustomScriptAction
com.workfusion.studio.rpa.recorder.playback.PlaybackException: Error executing TemplateAction[templateName=CustomScriptAction.ftl,id=4,name=Optional[CustomScriptAction],parent=-1,nextSibling=5,arguments=ActionArguments[delay=[0],xsi:type=[recorder:CustomScriptAction, recorder:CustomScriptAction],name=[date1],pollingInterval=[300],active=[true],actionDetails=[(date1)],script=[@CustomScriptAction(
input = [‘date1’],
output = ‘date2’
)
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:
General error during canonicalization: Output parameter should be defined as variable name

com.workfusion.studio.rpa.recorder.api.custom.exception.CustomScriptFormatException: Output parameter should be defined as variable name

Kindly please assist,

You define the output variable as date2, but in the script you use date1 as output. You need to change it to date2, and it has to be a DateTime variable.

The script you shared gets yesterday’s date.
Do you need to get current time?

Thanks alot Mam , let me give it a try.

yes current time please.

Then you don’t need a custom action for it, you can do it using Date Format action, Convert Date to String option.
You just need to write the correct format - only the time.

Here is a sample script (made in version 2.1.4).

current_time.zip (635 Bytes)

Thanks alot , let me try this way…

but my main requirement is like i want the bot to capture the date daily.
as using this i think it will cover only current date and time.

correct me if am wrong.

It captures the current date and time every time you play this recording, so today it will capture 2018-12-07 and the time when it is executed, tomorrow it will capture 2018-12-08 and the time when it is executed.

Great , thanks alot …

i tried but still throwing me an error message.

sorry to bother you alot.