Qlik Community

Ask a Question

QlikView App Dev

Discussion Board for collaboration related to QlikView App Development.

Announcements
Join this live chat April 6, 10AM EST - QlikView to Qlik Sense REGISTER
cancel
Showing results for 
Search instead for 
Did you mean: 
qv_mypath
Contributor II
Contributor II

How to assign an output of a user defined function

Hi All,

I would like to assign an output of a function to a variable.
Say a function is just

function test
    test = 3 + 3
end function

While trying to assign to a variable, say, vTestVar, I get an error message:
Error: Error in expression: TEST ist not a valid function.

What am I doing wrong?
Thank you very much in advance.

1 Solution

Accepted Solutions
bill_markham

You need to add () after the function name

function test ()

    test = 3 + 3

end function



And set the variable in your load script thus, not forgetting the (), I also put a trace after it to display what is in the variable.


let vTestVar = test() ;

trace vTestVar: $(vTestVar) ;

Sample qvw is attached.

View solution in original post

8 Replies
its_anandrjs

Where you use this function on the load script or some where else.

Regards

Anand

qv_mypath
Contributor II
Contributor II
Author

Thank you for your response, Anand!

I define the function in the module and then try to assign its output to a variable either in UI or in the load script. Neither works.

its_anandrjs

For more details for this please check the Qlik API Guide.

Regards

Anand

bill_markham

Personally I avoid macros, the reasons are explained in this Blog Post Macros are Bad

Notwithstanding that, what are you trying to achieve with this macro ?

It may be achievable without resorting to a macro.

its_anandrjs

Thanks Bill,

I am also agree with you and this macros are time consuming also.

@qv_mypath You can do this with normal tables also with the inline data i represent a small demo to you to explain how to do this calculation with the tables.

//Calculation Done on the Variable

Let vTest = 3 + 3;

//Variable calculation taken on the table

Load * Inline

[

Addition_Result

'$(vTest)'

];

Variables.PNG

You can do any calculation on the script part.

Regards,

Anand

qv_mypath
Contributor II
Contributor II
Author

Thank you for your response, Bill!

I asked this more to learn about the general possibility (to define any function in macro module and to assign its output to any variable - preferably in UI but at least in the load script). The certain goal can surely be achieved without macro 🙂

That said, I am agree that the macros are somewhat less than perfect. But sometimes you need a user-defined function. The best example I can think of at the moment is borrowing functions for regex processing from vbs since you do not have them in the load script out of the box.

bill_markham

You need to add () after the function name

function test ()

    test = 3 + 3

end function



And set the variable in your load script thus, not forgetting the (), I also put a trace after it to display what is in the variable.


let vTestVar = test() ;

trace vTestVar: $(vTestVar) ;

Sample qvw is attached.

View solution in original post

qv_mypath
Contributor II
Contributor II
Author

Thank you very much! This did the trick for the sample function.