Note: This blog post was created with the assistance of AI.
π What Are Global ProcScripts?
Global ProcScripts are reusable pieces of code in Uniface that live in a special place called a runtime library. Think of them as shared recipes that any part of your application can use whenever needed. Instead of writing the same code over and over again, you write it once as a Global ProcScript and call it from anywhere in your application.
π Key Features
Runtime Linking: Unlike IncludeScripts that copy code during development, Global ProcScripts are linked at runtime. This means the library containing them must be deployed with your application.
Implicit Function Modules: Each Global ProcScript automatically works as a function module. You can use it as a private function or execute it using a call statement.
Context Awareness: Global ProcScripts can find out information about who is calling them using special functions like $applname
, $formfocus
, $entname
, $fieldname
, and others.
β οΈ Important Things to Know
Deployment Dependency: When you use Global ProcScripts, you create a runtime dependency. The Global ProcScript library must be available when your application runs. If it's missing, your application won't work properly.
Avoid Predefined Constants: Don't use Uniface's built-in constants in Global ProcScripts. During compilation, Global ProcScripts don't have access to the information needed to understand these constants correctly.
Prefer IncludeScripts: In most cases, it's better to use IncludeScripts instead of Global ProcScripts because they don't create runtime dependencies. Use Global ProcScripts only when you really need shared functionality across multiple applications.
π‘ Example Use Case
Imagine you have a common validation routine that needs to check if an email address is valid. Instead of copying this validation code into every component, you can create a Global ProcScript called ValidateEmail
:
; Global ProcScript: ValidateEmail
entry ValidateEmail
params string vEmailAddress
; Your validation logic here
if (vEmailAddress = "")
return -1 ; Empty email
endif
; Check for @ symbol
if ($pos("@", vEmailAddress) = 0)
return -1 ; Invalid format
endif
return 0 ; Valid email
end
Now any component in your application can call this Global ProcScript:
; In your component
string vUserEmail
numeric vResult
vUserEmail = "user@example.com"
call "ValidateEmail", vUserEmail, vResult
if (vResult = 0)
message "Email is valid! β
"
else
message "Email is invalid! β"
endif
π οΈ Alternative: Using the #function Directive
If you want to avoid the runtime dependency, you can copy a Global ProcScript into your component during compilation using the #function
precompiler directive. This treats it as a local ProcScript module, so the library doesn't need to be deployed.
π Best Practices
- β Use Global ProcScripts for truly shared functionality across multiple applications
- β Keep them simple and focused on one task
- β Use context functions to make them flexible
- β Document what they do and what parameters they need
- β Avoid using predefined Uniface constants
- β Don't forget to deploy the runtime library
- π Consider using IncludeScripts for single-application code
π― Summary
Global ProcScripts in Uniface 10.4 are powerful tools for sharing code across your application. They live in runtime libraries and are linked when your application runs. While they're useful for common functionality, remember that they create deployment dependencies. In many cases, IncludeScripts might be a better choice because they don't have this limitation.
The key is to use Global ProcScripts wiselyβwhen you need shared functionality across multiple applications and the deployment dependency is acceptable. For single-application code reuse, stick with IncludeScripts to keep things simple! π
Top comments (0)