Custom Script works when run as Bot Task, but not in actual recording

I created a custom script for an API call and it works when I run it in code perspective as a bot task. Here is the code:

@CustomScriptAction(
                    input = ['input_v'],
                    output = 'output_v'
        )
		def customScript(){
		
		def url = "https://api.aylien.com/api/v1/sentiment?text=" + URLEncoder.encode(input_v, "utf-8")
		def connection = new URL(url).openConnection() as HttpURLConnection
		
		connection.setRequestProperty('X-AYLIEN-TextAPI-Application-Key', '//mykey')
		connection.setRequestProperty('X-AYLIEN-TextAPI-Application-ID', '//mykey')			 														
		
		output_v = connection.inputStream.text
		}

I created the recorder variables input_v and output_v as type string. However when I run the recording I get the following error. error.txt (4.0 KB)
It seems like the recorder string variable is not compatible with the string format of the code. (No signature of method: “static java.net.URLEncoder.encode() is applicable for argument types: (com.workfusion.studio.rpa.recorder.api.types.RString, String)”.

Function operating with string therefore doesn’t seem to work. How could I fix this?

1 Like

Hi @alexr

Try running this script. There are 2 small changes as compared to the above one (see comments).

@CustomScriptAction(
                    input = ['input_v'],
                    output = 'output_v'
        )
		def customScript(){
		
		def url = "https://api.aylien.com/api/v1/sentiment?text=" + URLEncoder.encode(input_v.toString(), "utf-8") //convert RString to Java string
		def connection = new URL(url).openConnection() as HttpURLConnection
		
		connection.setRequestProperty('X-AYLIEN-TextAPI-Application-Key', '//mykey')
		connection.setRequestProperty('X-AYLIEN-TextAPI-Application-ID', '//mykey')			 														
		
		def output = connection.inputStream.text
		output_v = RString.of(output) // convert Java string to RString
		}
1 Like

Thank you that solved it! I tried using the toString() method before but somehow still got an error.
Another question, is it possible to have the custom script action inside a for each loop?

1 Like

Glad I could help :grinning:

Yes, you can use it inside a loop.

The for each loop where I need the script is inside an open spreadsheet action already, in that case it doesn’t let me insert a script action inside the loop. If the loop is not nested in such action it works.

Yes, if the loop is inside the Open spreadsheet, then you cannot insert the script there.

As Excel actions are executed in the background, there are several actions that cannot be nested in them. You can see the list in Loops and Conditions paragraph on this page in the KB

https://kb.workfusion.com/display/RPAe/Excel

Hi @ashapkina, below is my sample script, the problem here i have is the code inside the custom action is not getting executing. Please let me know in case I am doing something incorrectly.

<?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="false">
			<capability name="SEARCH_ALL_WINDOWS" value="true" />
			<capability name="CLOSE_ALL_WINDOWS" value="false" />
			<script><![CDATA[
				import com.workfusion.studio.rpa.recorder.api.*
				import com.workfusion.studio.rpa.recorder.api.types.*
				import com.workfusion.studio.rpa.recorder.api.custom.*
				import com.workfusion.studio.rpa.recorder.api.internal.representation.*
				import com.workfusion.bot.exception.*
				
			
                println "This is test One."

				def memberno = RList.of()
				def winname = RString.of('${driver().getWindowHandle()}')
				def appid = RList.of()
				def url = RString.of('https://www.google.com')

				com.workfusion.rpa.helpers.RPA.metaClass.static.$ = { Closure c -> c.call() } // Support for Expression action. Should be implemented in RPA class in next release.
				timeouts().pageLoadTimeout(15000, java.util.concurrent.TimeUnit.MILLISECONDS)
				openChrome(UrlOperations.ensureProtocol("${url}"))
				sleep(10000)
				window().maximize()
				
				@CustomScriptAction()
				  def newScript(){
				  println "This is test2"
				  println "This is test3"
				  println "This is test4"
				 }
				 
			]]></script>
		</robot>
	</robotics-flow>
</config>
1 Like

You are missing customScripActionCall_newScript: {} after the custom script

@CustomScriptAction()
				  def newScript(){
				  println "This is test2"
				  println "This is test3"
				  println "This is test4"
				 }
				
				customScripActionCall_newScript: {}

@ashapkina Thanks much for this, I have a similar question about plug in, how the plug in can be called in script section. As in below example.

<?xml version="1.0" encoding="UTF-8"?>
<config
	xmlns="http://web-harvest.sourceforge.net/schema/1.0/config"
	scriptlang="groovy">
	
	<var-def name="employees">
	<database connection="jdbc:sqlserver://fsdfdfdfds:1433;databaseName=xxxx;user=qaddd;password=qaddd"
		        jdbcclass="com.microsoft.sqlserver.jdbc.SQLServerDriver">
				SELECT Palphaappid, dob FROM [xxx].[dbo].[Person] WHERE APPID IN (SELECT MAX(Appid) from [xxx].[dbo].[xxxson])
			  </database>
	</var-def> 
			 <loop item="emp">
			  <list> <var name="employees"/> </list> 
			   <body> <template>
	<script><![CDATA[	
			println	"Salary of ${emp.get("Palphaappid")} is ${emp.get("dob")}"
		 ]]></script>		
	      </template> </body>
	      </loop>
	

	<robotics-flow>
		<robot driver="universal" close-on-completion="true"
			start-in-private="false">
			<capability name="SEARCH_ALL_WINDOWS" value="true" />
			<capability name="CLOSE_ALL_WINDOWS" value="false" />
			
			<script><![CDATA[
				import com.workfusion.studio.rpa.recorder.api.*
				import com.workfusion.studio.rpa.recorder.api.types.*
				import com.workfusion.studio.rpa.recorder.api.custom.*
				import com.workfusion.studio.rpa.recorder.api.internal.representation.*
				import com.workfusion.bot.exception.*
				
			
                println "This is test One."
				def memberno = RList.of()
				def winname = RString.of('${driver().getWindowHandle()}')
				def appid = RList.of()
				def url = RString.of('https://www.google.com')
				com.workfusion.rpa.helpers.RPA.metaClass.static.$ = { Closure c -> c.call() } // Support for Expression action. Should be implemented in RPA class in next release.
				timeouts().pageLoadTimeout(15000, java.util.concurrent.TimeUnit.MILLISECONDS)
				openChrome(UrlOperations.ensureProtocol("${url}"))
				sleep(10000)
				window().maximize()
			]]></script>
			
			
		</robot>
	</robotics-flow>
</config>

Is there documentation available for these sort of issues.

Best–

Manoj

@ManojPuri answered to you in this topic

A post was merged into an existing topic: How to call db plugin inside or as a separate bot task??