How to convert from string to code

Hi, I’ve always had this question in my mind. Let’s make some examples.

Let’s say I have to make some search in SAP by a date. The client wants me put 1 day before. So I put like always dateQuery=now.addDays(-1)

but maybe the client now wants to search by an specific date, like 01/01/2020. Or the client wants to search by the current day

I was thinking “Maybe if I can put this code ‘now’ into an text type asset and then convert it into a datetime value when I’ll get it will help a lot”

So I was thinking. ¿UiPath has some way to convert a text ‘now’ to a code ‘now’?

The Idea is to save code like text and when I need it just convert it to code. Like the Eval() function of javaScript.

There is some ways?

Hey @KevinDS, you can run any JavaScript function in UiPath if you want to.
For simple date and time calculations:-

  1. For date and time comparisons check these posts:-
    How to compare datetime variable (with MM/dd/yyyy hh:mm:ss format)?
1 Like

@KevinDS

Check below for your reference

Convert Date & Time Format Tutorial

Hope this helps you

Follow Link

Thanks

1 Like

I want to save in a text file the code like a json:

{
“queryDate”:“now.addDays(-1)”
}

and if the client tells me to change the queryDate for tomorrow:

{
“queryDate”:“now.addDays(1)
}

and if the client tells me to change to a specific date:

{
“queryDate”:“DateTime.ParseExact(01/01/2020, “yyyyMMdd”, CultureInfo.InvariantCulture)
}

The thing is. I want to make some parts of the code like this date easy to change. But I don’t know how to change it to Datetime in uipath. Like:

ASSIGN stringQueryDate = jobject(“queryDate”)
ASSIGN dateTimeVariable = stringQueryDate.ConverToCode :sweat_smile:

@KevinDS

Can you tell me where your input will come from, what is the source

also explain your requirement with a small example

it will help to understand your issue

Thanks

1 Like

Type Into:

“{”"" + “queryDate”"" + “:”"" + DateTime.ParseExact(01/01/2020, “yyyyMMdd”, CultureInfo.InvariantCulture) + “”"" + “}”

should do the trick.

In order to access it from a JObject I’d simply do some string manipulation with .replace to add the additional quotes needed in order to type <">

For the awkward reverse process you intend to do (I can only describe it as “eval” from JavaScript:
There is really no need to do that.

If you client tells you to change to X you set your values in an Assign to X.
With an attended process and input dialog with some buttons or drop down?

1 Like

Maybe NCalc can do the trick?

Cheers

1 Like

@KevinDS

to the question about is an EVAL() available - Short Answer: No, not available in current available Infrastructure ( some additional words on it refer below)

Your motivation is well understood and not unusually. Have a look on following approach on how we can overcome the missing eval and can implement in UiPath essential

Lets start an implementation with following Commands ( in String which you can set / use in your config JSON)


so we do handle a specific date, yesterday (-1),Today(0), Tommorow(+1)

After parsing / splitting the command within an switch we can handle

  • split on #
  • react on command
  • execute the command by using the provided parameters
    • e.g. the date string
    • e.g the add days value

Testing this we got this output:
grafik

So the solution approach

  • is limited on that it can handle only commands that are implemented or well known.
  • it is strict to its implementation risks not open the doors for hack and exploit by executing malicious code

About evals / other infrastructure additionals that can be checked within RnD

  • Roslyn plattform with scripting and the RunAsync method (not available / not ready to use)
  • other nuget packages implementing this
  • routing to under technologies like Powershell, Python …

But as mentioned above:

it is strict to its implementation risks not open the doors for hack and exploit by executing malicious code

the solution is not open to execute any code fragments and requires the implementation of getting implemented the commands. So using it we would be very relaxed when Bot code goes under a security check or implementation audit.

Find demo xaml here:
Pattern_DateCalculation_ByCommandString.xaml (7.7 KB)

Let us know your feedback

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.