5 Replies Latest reply: Jan 19, 2017 9:15 AM by Magnus Berg RSS

    SUB inside IF ... THEN statement

    Vegar Lie Arntsen

      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

        • Re: SUB inside IF ... THEN statement
          Andrey Khoronenko

          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.

            • Re: SUB inside IF ... THEN statement
              Vegar Lie Arntsen

              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.

              • Re: SUB inside IF ... THEN statement
                Vegar Lie Arntsen

                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

                  • Re: SUB inside IF ... THEN statement
                    Peter Cammaert

                    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

                    • Re: SUB inside IF ... THEN statement
                      Magnus Berg

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