IBI WebFOCUS - code for Looping concepts

Using Loops with WebFOCUS Code:
In our day to day reporting world in WebFOCUS, we might come across the need to use Looping concept to run some piece of WebFOCUS code for a number of times. I have documented the Solution and codes so that it can help all of us if similar requirement is there.

Problem description:
In Launch Page we have Location selection(drop down). Display field is Location Description and Value field is Location Number. Now when we selection few locations from launch page, location numbers are passed to the report. In this case if we need to display all location selected as header, how can we proceed?

Solution:
We need to pull it from Location Dimension table which has location name and location number as available columns. We can pull location names filtering on selected location numbers and save in a save file. Now we need to read location Name again and again and append it into a variable. Let's take an example of CAR file as given below:

-* File test_tricks.fex
-SET &ECHO=ALL;
TABLE FILE CAR
PRINT CAR
ON TABLE SAVE AS SAVE_CAR
END
-RUN
-SET &FIRST_TIME='Y';
-**********************Variable to hold the appended all Car Names
-SET &HEADER_CAR = '';
-SET &NO_RECS = &RECORDS;

-*************************STARTING THE LOOP FOR NO OF RECORDS TIMES
-REPEAT CAR_HEAD &NO_RECS TIMES
-READ SAVE_CAR &CAR_NAME1.A16
-SET &CAR_NAME = TRUNCATE(&CAR_NAME1);

-TYPE &CAR_NAME
-SET &HEADER_CAR = IF &FIRST_TIME EQ 'Y' THEN &CAR_NAME ELSE &HEADER_CAR||(', '|&CAR_NAME);
-*NAME_COM/A20=LAST_TOKEN || (' '|FIRST_INIT);
-SET &FIRST_TIME='N';
-CAR_HEAD
-TYPE (Final Header Variable)..........&HEADER_CAR
-EXIT
-****************Code ends here************************************************************************************************

So after executing the above piece of code, &HEADER_CAR will have list of all Cars from CAR file. The variable can be used anywhere in the file.

While and For loop can also be used if we don't know the no of times the code should get executed.



-REPEAT label WHILE condition;
or
-REPEAT label FOR &variable [FROM fromval] [TO toval] [STEP s]

Any questions/Suggestions are welcome.

Comments

Popular posts from this blog

IBI WebFOCUS - Functions available and syntax to use

IBI WebFOCUS - Difference (Preference) between Join and Match:

IBI WebFOCUS - Testing and Debugging in WebFOCUS codes