[SOLVED] Inject Js Script - return value as a parameter

Hello,

I have a JS code and I want to return value from JS to UIPath. So I tried this

(function() {
var last_page_val=document.getElementById("pageCount").value;
var last_page_count=-1;
    if(last_page_val===null || last_page_val===undefined){
        last_page_count=-1;
    }
    else
    {
        last_page_count=parseInt(last_page_val);
    }
    return last_page_count;
 })();

10

And it doesn’t works. How can return value from JS?

Hi @bk52

Does this topic help?

Or other topics with examples of the Inject JS usage here.

@bk52 Just change your JavaScript as below.

function getLastPageCount() {
  var last_page_val = document.getElementById("pageCount").value;
  var last_page_count = -1;
  if (last_page_val === null || last_page_val === undefined) {
    last_page_count = -1;
  } else {
    last_page_count = parseInt(last_page_val);
  }
  return last_page_count;
}

An article about Inject JS Activity

3 Likes

Thank you for your responses.

I solved my problem with return string. I add “toString()” function and it works. My solution is below

function (e) {
var last_page_val=document.getElementById("pageCount").value;
var last_page_count=10;
    if(last_page_val===null || last_page_val===undefined){
        last_page_count=-1;
    }
    else
    {
        last_page_count=parseInt(last_page_val);
    }
    console.log("last page count -> " + last_page_count);
    return last_page_count.toString();    	
}
3 Likes

It seems the ScriptOutput variable type of your Inject JS Script activity is of type string. You don’t need to do that conversion if the variable type was integer. So if you set the type of variable last_page_count to Int32, you can remove the conversion you do in the JavaScript.

@KannanSuresh
I try it with simply like that but it didn’t work.
I created a varible “outPut” as Int32
Then I return 1 from JS.

function (e) {
	return 1;
}

If I select “ContinueOnError” property false it gives me an error “is not a valid for Int32

If I select “ContinueOnError” property true it doesn’t return 1

Only If I return this parameters as a string It works successfully.

P.S. UiPath 2019.4.2 Community Edition

It seems you are right. The documentation also says the same.

Output
ScriptOutput - String result returned from JavaScript code.

@bk52 Did you try this on a browser other than Internet Explorer? If so I think this is some issue.

I tried the codes with Internet Explorer and it works fine. But when tried with Chrome, I need to return a string from the JavaScript for it to work. This is really weird.

1 Like

I have raised a bug in the forum related to this.

1 Like

@KannanSuresh
I tried it with Chrome 74.0.3729.131

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