Qlik Community

QlikView Scripting

Discussion Board for collaboration on QlikView Scripting.

Announcements
Save the Date: QlikWorld Online, June 24-25, 2020. Free global virtual event for data integration and data analytic gurus. Register Today
Partner
Partner

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

Plees ekskuse my Swenglish and or Norweglish spelling misstakes
Tags (5)
1 Solution

Accepted Solutions
Highlighted
Honored Contributor

Re: SUB inside IF ... THEN statement

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
Highlighted
Honored Contributor

Re: SUB inside IF ... THEN statement

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

Highlighted
Partner
Partner

Re: SUB inside IF ... THEN statement

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.

Plees ekskuse my Swenglish and or Norweglish spelling misstakes
Highlighted
Partner
Partner

Re: SUB inside IF ... THEN statement

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

Plees ekskuse my Swenglish and or Norweglish spelling misstakes
Highlighted

Re: SUB inside IF ... THEN statement

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

Highlighted
Employee
Employee

Re: SUB inside IF ... THEN statement

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));