JavaScript - assign result of fn to variable robotakst

Discuss RoboTask here
Post Reply
felipe
Posts: 31
Joined: Thu Nov 30, 2023 6:18 pm

JavaScript - assign result of fn to variable robotakst

Post by felipe »

function fn(a, b) {
return a + " " + b;

var result = fn("hello", "world");

RoboTaskApp.AddUserVariable(RoboTaskApp.ExpandText(VariableNameResult, result);


could help format the synthase
tks
Oleg
Site Admin
Posts: 3235
Joined: Thu Jan 01, 1970 1:00 am
Contact:

Re: JavaScript - assign result of fn to variable robotakst

Post by Oleg »

Look at my example below

Code: Select all

;*****************************
;* RoboTask Task file
;* Do not edit in text editor!
;*****************************
 
[Root]
ActionAfterRun=INTEGER|0
Actions=FOLDER
Automat=INTEGER|-1
CatID=INTEGER|444033236
Comment=STRINGLIST
ContinueOnError=INTEGER|0
DisableOnError=INTEGER|0
DoNotStopWhenShutdown=INTEGER|0
ExternalName=STRING|"Task1465"
Hide=INTEGER|0
ID=INTEGER|2102349058
LogOnAsUser=INTEGER|1
Name=STRING|"JS Script..."
OnErrorTaskID=INTEGER|0
Priority=INTEGER|3
RunOnClose=INTEGER|0
RunOnStartup=INTEGER|0
StepPause=INTEGER|0
ToLog=INTEGER|3
UnicodeFormat=INTEGER|1
WriteGeneralLog=INTEGER|0

[Actions]
Action1=FOLDER
Action2=FOLDER

[Actions\Action1]
ActionID=STRING|"A_SCRIPT_JSSCRIPT"
Enabled=INTEGER|-1
Name=STRING|"JS Script"
Params=FOLDER

[Actions\Action1\Params]
expandvars=STRING|"0"
line00000000=STRING|"function myfn(a, b)"
line00000001=STRING|"  {"
line00000002=STRING|"  return a + "" "" + b;"
line00000003=STRING|"  }"
line00000005=STRING|"VariableNameResult = ""ResVar"";"
line00000006=STRING|"result = myfn(""hello"", ""world"");"
line00000008=STRING|"//logmessage(result);"
line00000009=STRING|"RoboTaskApp().SetUserVariable(VariableNameResult, result);"
linecount=STRING|"11"
savescript=STRING|"0"

[Actions\Action2]
ActionID=STRING|"A_ROBOTASK_LOG"
Enabled=INTEGER|-1
Name=STRING|"Log Message"
Params=FOLDER

[Actions\Action2\Params]
message=STRING|"{ResVar}"
type=STRING|"3"

The script is

Code: Select all

function myfn(a, b)
  {
  return a + " " + b;
  }

VariableNameResult = "ResVar";
result = myfn("hello", "world");

RoboTaskApp().SetUserVariable(VariableNameResult, result);
Oleg Yershov
felipe
Posts: 31
Joined: Thu Nov 30, 2023 6:18 pm

Re: JavaScript - assign result of fn to variable robotakst

Post by felipe »

I'd like to return to this post so I don't have to open another one, in this variable assignment context
in this context the variable is always applying globally, I'd like it to be only within the task
I've already declared it at the beginning of the task with the same name as the variable assigned in the function, the function applies globally, this interferes with another task with the same variable name
Oleg
Site Admin
Posts: 3235
Joined: Thu Jan 01, 1970 1:00 am
Contact:

Re: JavaScript - assign result of fn to variable robotakst

Post by Oleg »

Try to use variable name like this: "local@ResVar"
Also you can create local variable with necessary name

Look at my example below. I have global variable MyVar. By the script set value to local variable MyVar

Code: Select all

;*****************************
;* RoboTask Task file
;* Do not edit in text editor!
;*****************************
 
[Root]
ActionAfterRun=INTEGER|0
Actions=FOLDER
Automat=INTEGER|-1
CatID=INTEGER|307868550
Comment=STRINGLIST
ContinueOnError=INTEGER|0
DisableOnError=INTEGER|0
DoNotStopWhenShutdown=INTEGER|0
ExternalName=STRING|"Task1583"
Hide=INTEGER|0
ID=INTEGER|-1222492109
LogOnAsUser=INTEGER|1
Name=STRING|"Global and local variables"
OnErrorTaskID=INTEGER|0
Priority=INTEGER|3
RestrictRESTAPIAccess=INTEGER|0
RunOnClose=INTEGER|0
RunOnStartup=INTEGER|0
StepPause=INTEGER|0
ToLog=INTEGER|3
UnicodeFormat=INTEGER|1
WriteGeneralLog=INTEGER|0

[Actions]
Action1=FOLDER
Action2=FOLDER
Action3=FOLDER

[Actions\Action1]
ActionID=STRING|"A_VARIABLES_SET"
Enabled=INTEGER|0
Name=STRING|"Set variable ""local@myvar"" with value ""Local var"""
Params=FOLDER

[Actions\Action1\Params]
_rt_variables_produced=STRING|"local@myvar"
expand=STRING|"0"
linecount=STRING|"1"
varname=STRING|"local@myvar"
varvalue=STRING|"Local var"

[Actions\Action2]
ActionID=STRING|"A_SCRIPT_JSSCRIPT"
Enabled=INTEGER|-1
Name=STRING|"JS Script"
Params=FOLDER

[Actions\Action2\Params]
expandvars=STRING|"0"
line00000000=STRING|"function myfn(a, b)"
line00000001=STRING|"  {"
line00000002=STRING|"  return a + "" "" + b;"
line00000003=STRING|"  }"
line00000005=STRING|"VariableNameResult = ""local@MyVar"";"
line00000006=STRING|"result = myfn(""hello"", ""world"");"
line00000008=STRING|"RoboTaskApp().SetUserVariable(VariableNameResult, result);"
linecount=STRING|"9"
savescript=STRING|"0"

[Actions\Action3]
ActionID=STRING|"A_DIALOG_MESSAGE"
Enabled=INTEGER|-1
Name=STRING|"Show ""{global@myvar}"""
Params=FOLDER

[Actions\Action3\Params]
icon=STRING|"1"
msg0=STRING|"{global@myvar}"
msg1=STRING|"{local@myvar}"
msgcount=STRING|"2"
playsound=STRING|"0"
showmessage=STRING|"1"
Also read about prefixes here: https://robotask.com/help/index.html?pr ... iables.htm
Oleg Yershov
Post Reply