Skip to main content
Announcements
Have questions about Qlik Connect? Join us live on April 10th, at 11 AM ET: SIGN UP NOW
cancel
Showing results for 
Search instead for 
Did you mean: 
Vegar
MVP
MVP

SUB inside IF ... THEN statement

I noticed that sub statements that are created/included inside an if statement is not available for calls outside of the IF statement.

// This IF statement will work

IF 1=1 THEN

  SUB HelloWorld

    TRACE Hello World;

  ENDSUB

  CALL HelloWorld;

ENDIF

// This IF-statement will not work and throw an error.

IF 1=1 THEN

  SUB HelloQlik

    TRACE Hello Qlik;

  ENDSUB 

ENDIF

CALL HelloQlik;

It seems that subs inside an IF acts as a private SUB. Can anyone explain and/or refer to any Qlik documentation that refers to this behavior?

Cheers

Vegar Lie Arntsen

1 Solution

Accepted Solutions
ahaahaaha
Partner - Master
Partner - Master

Sub..end sub ‒ Qlik Sense

If the operator sub defined within another operator control, such as For ... Next loop or If the design ... Then, the subroutine will be available only within the application of this operator control. In other words, the operator can sub not be defined within another operator control if a subprogram call must be made outside the control of the operator.

View solution in original post

5 Replies
ahaahaaha
Partner - Master
Partner - Master

Sub..end sub ‒ Qlik Sense

If the operator sub defined within another operator control, such as For ... Next loop or If the design ... Then, the subroutine will be available only within the application of this operator control. In other words, the operator can sub not be defined within another operator control if a subprogram call must be made outside the control of the operator.

Vegar
MVP
MVP
Author

Thanks for the explanation. It is probably a correct answer, but I can't see that the Sub..end sub ‒ Qlik Sense‌‌ documentation handles this feature.

I was aware of variables inside a sub was local(private) but I didn't know that SUB inside a FOR... NEXT or a IF THEN where local. I was expecting the code inside the SUB to run.

Vegar
MVP
MVP
Author

This becomes an issue for when using a conditional QDF initiation.

Eg. QlikView and Sense is initiated by different syntax.

IF Qlikview  Then

     $(Include=..\3.include\1.basevariable\1.init.qvs);

ELSE  //Is Qlik Sense

     SET vG.HomeContainer= 'lib://Finance';

     $(Include=$(vG.HomeContainer)\InitLink.qvs);

ENDIF

The QDF initiation contains subs that we want to use later on in the script. By capsulate the init-script into an IF we loose the QDF SUBs.

Have you run into this issue/feature before mbg‌ ?

Ping: cosmo_k

Peter_Cammaert
Partner - Champion III
Partner - Champion III

There is nothing much you can do about this (has something to do with scope and visibility of definitions, a language constant).

You can however circumvent the problem by setting the path to the include file in an IF THEN ELSE statement, and then just including whatever it has been set to. Like:

IF Qlikview Then

  SET vSubFile = ..\3.include\1.basevariable\1.init.qvs;

ELSE  //Is Qlik Sense

  SET vG.HomeContainer= 'lib://Finance';

  SET vSubFile = $(vG.HomeContainer)\InitLink.qvs;

ENDIF

$(Include=$(vSubFile)) // No semicolon needed

Best,

Peter

Magnus_Berg
Employee
Employee

Hi Vegar, yes, I know this "limitation" If you define a variable in the if that you will use in the include after the if statement, that will work. Example:

if trim(lower('$(vG.QVCDisable)'))='true' then

// This is used to disable/enable QlikView Components library (Qvc)

  set vG.QVCDisable='$(vG.$(vL.QDF.LinkShared_Folders)SubPath)'; // vG.QVCDisable = true removes Qvc.qvs filename

else

  set vG.QVCDisable='$(vG.$(vL.QDF.LinkShared_Folders)SubPath)Qvc.qvs'; // Not disabled will state the Qvc.qvs filename

endif

$(Include=$(vG.QVCDisable));