elsa-core icon indicating copy to clipboard operation
elsa-core copied to clipboard

System.InvalidCastException when executing JavaScript

Open debjit93 opened this issue 1 year ago • 4 comments

I’m writing JavaScript to pause execution for a few seconds. I’m receiving an InvalidCastException stating that the object must implement IConvertible. Could anyone help me identify the mistake?

`const timerInSeconds = 10; let current = currentInstant(); let waitTill = addDurationTo(current, Duration.FromSeconds(timerInSeconds)); const zeroSecond = 0; while(durationBetween(waitTill, currentInstant).Seconds > 0){

}`

debjit93 avatar Feb 19 '24 06:02 debjit93

Based on the context provided, it seems like you are trying to write JavaScript code to pause execution within an Elsa workflow. The error message you mentioned, "InvalidCastException stating that the object must implement IConvertible," typically occurs when there is a type mismatch or an attempt to convert an object to a type that it does not support.

In the JavaScript code snippet you provided, there are a few potential issues:

  1. currentInstant() and addDurationTo are not standard JavaScript functions. If these are custom functions or provided by Elsa's JavaScript activities, ensure they are implemented correctly and return the expected types.

  2. Duration.FromSeconds(timerInSeconds) appears to be a .NET method call, not JavaScript. If you are trying to use .NET methods within JavaScript, this could be the source of the error. JavaScript does not have a native Duration object like .NET.

  3. The while loop condition uses durationBetween(waitTill, currentInstant).Seconds, which again seems to be a mix of .NET and JavaScript. Ensure that durationBetween is a valid function and that currentInstant is called as a function (currentInstant()).

To pause execution in JavaScript, you typically use setTimeout function. However, in the context of Elsa workflows, you would use activities provided by Elsa to handle delays or timers.

If you are trying to create a delay within an Elsa workflow, you should use the Timer activity provided by Elsa, which allows you to specify a duration after which the workflow will resume. You would not write a while loop in JavaScript for this purpose.

Here's an example of how you might set up a Timer activity in Elsa (note that this is not JavaScript code, but rather a configuration of an Elsa activity):

builder.AddWorkflow<MyWorkflow>(workflow => workflow
    .StartWith<Timer>(timer => timer
        .Set(x => x.DurationExpression, context => TimeSpan.FromSeconds(10))
    )
    // ... other activities
);

If you are trying to write JavaScript for some custom logic within an Elsa workflow, you should ensure that you are using JavaScript correctly and not mixing it with .NET code. If you need to use .NET functionality, you should do so within C# activities or by extending Elsa with custom activities.

Please clarify if you are trying to use JavaScript within an Elsa workflow or if you are attempting to create a custom activity that involves pausing execution. If you provide more context or details about what you are trying to achieve, I can give you a more precise answer.

glime-ai[bot] avatar Feb 19 '24 06:02 glime-ai[bot]

I am using Elsa version 2.13.0 which does not have Timer activity.

I am using the Run JavaScript activity to achieve the delay and only using the functions which are shown in Elsa dashboard intellisense.

debjit93 avatar Feb 19 '24 06:02 debjit93

How did you get the "RunJavaScript" activity. I don't find it in the dashboard, and when I add using the json it fails. Looks like the whole Elsa product isn't built for complex functionality or orchestration.

Rathna314 avatar Feb 21 '24 22:02 Rathna314

How did you get the "RunJavaScript" activity. I don't find it in the dashboard, and when I add using the json it fails. Looks like the whole Elsa product isn't built for complex functionality or orchestration.

You need to add .AddJavaScriptActivities() in Elsa server for using JS activities. ` services .AddElsa(elsa => elsa .UseEntityFrameworkPersistence(ef => ef.UseSqlite(connectionString))

     .AddConsoleActivities()
     .AddJavaScriptActivities()
     .AddHttpActivities(options => options.BasePath = "/workflows")
     );`

debjit93 avatar Apr 04 '24 07:04 debjit93