Unexplained (to me) error in custom action script

I have attached a sample script for this.

I have a custom action that hit’s a generic API and creates a table from resulting data. This is similar to an actual project I’m working on. This script works, until I remove line 40, println tbl. When that’s done the resulting error occurs. I’m sure it’s something simple that myself, as a relative new user, is overlooking or just not aware of how it works.

Any ideas why having the print line allows for the script to work? My assumption is it’s causing some type of type conversion but I’m unable to explain. Thoughts or now to properly fix or correct code?

sandbox.zip (1.6 KB)

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],timeoutMs=[10000],xsi:type=[recorder:CustomScriptAction, recorder:CustomScriptAction],name=[Get Info],pollingInterval=[300],active=[true],script=[import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import groovy.json.JsonSlurper 

@CustomScriptAction(
    input = ['web_url'],
    output = 'tbl'
)
  
def customScript() {
    URL obj = new URL(String.valueOf(web_url));    
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("GET");
    int responsecode = con.getResponseCode();

    if (responsecode == HttpURLConnection.HTTP_OK) {
        BufferedReader in1 = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in1.readLine()) != null) {
            response.append(inputLine);
        }
        in1.close();

        def jsonSlurper = new JsonSlurper();
        def object = jsonSlurper.parseText(response.toString());

        object.each {
            def cntr_list = RList.of();
            cntr_list.add(it.name);
            cntr_list.add(it.email);
            tbl.addRow(cntr_list);
        }
        // comment following line to make it error
        //println tbl;
    } else {
       tbl.put(1, 1, RString.of("GET request not worked"));
    }
}],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:81)
      at com.workfusion.studio.rpa.recorder.player.PlaybackLogic.playNextAction(PlaybackLogic.java:154)
      at com.workfusion.studio.rpa.recorder.player.PlaybackLogic.run(PlaybackLogic.java:112)
      at java.lang.Thread.run(Thread.java:745)
  Caused by: java.lang.ClassCastException: Cannot cast java.util.ArrayList to com.workfusion.studio.rpa.recorder.api.types.RDataType
      at java.lang.Class.cast(Class.java:3369)
      at com.workfusion.studio.rpa.recorder.playback.shell.StackBinding.lambda$updateFromBinding$2(StackBinding.java:40)
      at java.util.Map.forEach(Map.java:630)
      at com.workfusion.studio.rpa.recorder.playback.shell.StackBinding.updateFromBinding(StackBinding.java:40)
      at com.workfusion.studio.rpa.recorder.playback.shell.GroovyShellWrapper.executeScript(GroovyShellWrapper.java:50)
      at com.workfusion.studio.rpa.recorder.playback.player.PlaybackContext.executeScript(PlaybackContext.java:108)
      at com.workfusion.studio.rpa.recorder.playback.action.template.TemplateAction.executeBehavior(TemplateAction.java:33)
      at com.workfusion.studio.rpa.recorder.playback.flow.StandardControlFlow.execute(StandardControlFlow.java:46)
      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:81)
      at com.workfusion.studio.rpa.recorder.player.PlaybackLogic.playNextAction(PlaybackLogic.java:154)
      at com.workfusion.studio.rpa.recorder.player.PlaybackLogic.run(PlaybackLogic.java:112)
      at java.lang.Thread.run(Thread.java:745)

Hi @Craigw.
Please advise which type has variable ‘tbl’ in your recording. I assume that the issue not with printing, but in the next row with type conversion.

@Lera - I’m not sure I understand exactly what your question is? Are you wanting to know what type the variable tbl is typed to in the recording? It’s type of table. Or are you referring to something else?

Hello @Craigw,

The mistake is not easy to spot, you code looks find. Could you maybe try to comment more lines to see which line actually makes the cast fail?

Also try to print the type of each objects to check if its what you are expecting.

1 Like

Yes, that I wanted to know, thank you. Can you please try to print not whole table, but rows if possible?

Hi @Craigw try adding return tbl at the very end of the script to explicitly point to the output. It should do the trick.

                import java.io.BufferedReader;
				import java.io.IOException;
				import java.io.InputStreamReader;
				import java.io.OutputStream;
				import java.net.HttpURLConnection;
				import java.net.URL;

				import groovy.json.JsonSlurper

				@CustomScriptAction(
				input = ['web_url'],
				output = 'tbl'
				)

				def customScript() {
					URL obj = new URL(String.valueOf(web_url));
					HttpURLConnection con = (HttpURLConnection) obj.openConnection();
					con.setRequestMethod("GET");
					int responsecode = con.getResponseCode();

					if (responsecode == HttpURLConnection.HTTP_OK) {
						BufferedReader in1 = new BufferedReader(new InputStreamReader(con.getInputStream()));
						String inputLine;
						StringBuffer response = new StringBuffer();
						while ((inputLine = in1.readLine()) != null) {
							response.append(inputLine);
						}
						in1.close();

						def jsonSlurper = new JsonSlurper();
						def object = jsonSlurper.parseText(response.toString());

						object.each {
							def cntr_list = RList.of();
							cntr_list.add(it.name);
							cntr_list.add(it.email);
							tbl.addRow(cntr_list);
						}
					} else {
						tbl.put(1, 1, RString.of("GET request not worked"));
					}
					return tbl
				}
1 Like

Hello All, Sorry I fell off this thread. I had moved off to other priorities and just now returned.

I just want to confirm that @ashapkina’s solution appears to work for me.

2 Likes

Thanks for the update @Craigw Glad it helps!