Business Intelligence Specialist, Qlik
John Daniel Associates, Inc.
What is the TRACE Function?
Many Qlik developers will tell you that one of the most tedious tasks when developing an application is tracking down where and why your script has failed, especially when you are working on an application with complex scripting. Sometimes these errors are obvious, but oftentimes you can spend unfathomable amounts of time rereading your script trying to find out exactly where things went wrong. Throughout my time working with Qlik, I have personally experienced countless iterations of digging through my failed script executions until finally locating my problems after what always seems like an eternityâ¦ That was until I stumbled across the TRACE function.
The TRACE function has become one of my favorite tools in my arsenal of QlikView tips and tricks due to its ability to save time and eliminate many of the frustrations that come with debugging your script. In fact, I have found this function so useful that I have made a habit of adding it to all of my applications no matter the size or complexity. I believe that after reading this blog you too will find yourself adding this TRACE function to each and every one of your scripts!
In its simplest form, the TRACE function writes a string to the Script Execution Progress window and the script log file as shown in the images below.
As you can see in the images above, the TRACE function allows you to set markers within the Script Execution Progress window and log file so that if a script fails you can walk through your script and follow each marker, eventually pin-pointing the exact area where things went awry. These âmarkers,â or messages, are versatile and customizable, which allow you to tailor your TRACE markers to any application or situation.
One important thing to remember is that the TRACE function does not specifically spell out what failed, but rather it shows you what pieces of the script were successfully completed, thus allowing the developer to follow the success markers up until the point of script failure. The image below shows what would happen in both the Script Execution Progress window and the application log file if the script were to fail.
You can see that the Glossary Load started and finished, which tells me that everything seems to be working fine with my Glossary. I then scroll down to see that my Change Log Load starts, but I do not see any marker telling me that the load of this table was finished. Due to the missing TRACE marker in the Script Execution window and the presence of the âExecution Failedâ notification in the log file I know that something went wrong within my Change Log table load. These events and markers allow me to save time and jump right to that piece of script within my application and continue my research.
While this simple example is being used for the sake of this blog and explanation, in larger and more complex scripts being able to see what was fully completed and what was not can be a life saver. If you are running your script manually within the QlikView application you will still receive your normal script error dialog box, but the TRACE function can be invaluable when running jobs from the QMC where the specific error notifications are not as pronounced unless viewing the log file.
How is the TRACE function used?
There are a variety of ways to use the trace function but the two I find most useful are as follows:
- Designating a specific string by simply typing the desired output directly into the script as you have seen throughout this blog
Notice that there does not need to be quotes around your string unless you are adding additional components such as a variable. In that case you would need to use the format of âThis is an example â& $(vExample).
- Create a variable that feeds into your TRACE function
NOTE: If running the script manually from within the Qlik application it is VERY helpful to deselect the âClose when finishedâ checkbox at the bottom left hand side of the Script Execution Progress window. This eliminates the need to access the log file in the event of a failure. You can simply just scroll within your Script Execution Progress window to easily locate the area of your script error. This can also be configured in the User Preferences menu located under the Settings. Both methods are shown below.
*DEVELOPMENT TIP* â Because the TRACE function does not print anything to signify where a TRACE has been inserted it is sometimes hard to locate the markers that you have worked into your script. I find it extremely helpful to insert a string or characters that stand out into your TRACE function. Strings such as ââ¦â¦â¦.â (this string is used in the example image above), â********â, or â>>>>>>>â will make the TRACE much easier to read when scrolling through your Script Execution Progress window or application log file.
Hereâs a quick side-by-side look at what would happen in the Script Execution Progress window if I removed the preceding ââ¦â¦â¦.â before my TRACE string:
While the TRACE information is still present, it does not jump out at the developer the same as it would if they were to include preceding characters. For best results, use something that sticks out to you. Again, this helps to save time and increase readability.
NOTE: The TRACE function must be used before or after your script statements. For example, it CANNOT be used in the middle of a LOAD, such as after a field name.
As I have stated earlier, one limitation of the TRACE function is that it will not give you the exact record or field the script fails on. This is mainly due to the fact that the TRACE cannot be placed within a given statement. Although the TRACE function will not point out an exact record or field of error, it does have the ability to substantially narrow down the possible sources of errors. Because of this, I offer the following development tip:
*DEVELOPMENT TIP* â Use as many TRACE functions as you see fit. The more TRACE functions you use the easier it will be to debug your application.
For experienced developers it is easy to ignore certain functions or practices because they may seem useless or may take an extra few seconds to enter into your script. However, I maintain that it is these âbasicâ functions that we so often overlook that can actually be game changers when it comes crunch time. It is important to not become jaded by the development experience you obtain and always remember that going back to basics is not a sign of ignorance, but rather one of the smartest moves developers of all experience levels can make.
The TRACE function is debugging made easy. The next time you develop an application or begin to make changes to an old script give this method a tryâ¦ You wonât be disappointed!
The post How to use the TRACE function to track down problems in QlikView appeared first on John Daniel Associates, Inc..