Error when using macros in Internal Library

I was developing a library to be used by all applications . And this library was using the macro “sc_lookup(” in the internal libary and it gave error Fatal error: Uncaught Error: Call to a member function Execute()
So this is about such errors. I am aware that this is mentioned by other users in this forum. But I am trying to make such issues under a separate head with detailed explanation for all. I am not trying to steal others inputs though.

The Issue: Macro Compatibility and “Null” Errors

A common cause of Fatal error: Call to a member function ... on null is calling an Internal Library function that contains database macros (like sc_lookup or sc_sql_injection) from an incompatible event. This happens because:

  • Execution Scope: Macros inside a library function often lose access to the application’s internal database connection object.
  • Event Restrictions: Every Scriptcase macro has a specific list of events where it is allowed to run. If a macro is prohibited in an event (like onApplicationInit), it will also fail if called via a library within that same event.

Step 1: Verify Macro Compatibility

Before placing a library call, check the Scriptcase Macro Documentation for every macro used inside your library function.

  • Check the Event List: Ensure the macro you are using is explicitly listed as supported for the event calling the library.
  • The onApplicationInit Trap: Many database macros are not fully supported or initialized in onApplicationInit. Moving these calls to onScriptInit is often the solution, as it is the first event where the database connection is guaranteed to be ready.

Step 2: The “Object-Passing” Pattern

To ensure a library function has reliable access to the database regardless of macro parsing issues:

  • Pass the Application Object: Define your library function to accept the application context as a reference (e.g., function my_func(&$app_obj)).
  • Call with $this: When calling the function from an event, pass the current object: my_func($this).
  • Use Direct Methods: Inside the library, instead of using macros, use the internal methods of the passed object (e.g., $app_obj->Db->Execute()). This bypasses the macro parser entirely and prevents “null” pointer errors.

Summary Checklist

  1. Consult the Documentation: Verify the macro is allowed in the specific calling event.
  2. Use onScriptInit: Use this event for any logic that requires database interaction to ensure the connection is live.
  3. Use sc_include_library: Always use the formal macro to include your project or system libraries to maintain proper file paths.

Hi
I’m using sc macros in internal libraries at project level with no issues.

regards.

thanks for your feed back. Can you please share what macros are used by you and where are you using it in the application (eg application_init, script init and so on )

I have several internal libraries for sharing code. Some are utilities that do not use sc macros, but others do, for example, sc_lookup.
image
That function is used in onValidate event in an application

Other sc macros in other libraries: sc_select


This function is call from a button in an app.

Just add the libraries in your applications and use them.

HAPPY NEW YEAR !!
Thanks for sparing you time and taking efforts in making detailed screenshots with explanation. This will definitely help
Just to clarify (feel free to correct me if wrong)
I encountered this issue when using sc_lookup in the onApplicationInit.
You have mentioned the use of the library in onValidate and button events.
My issue was with onApplicationInit. The same function was working well in the onScriptInit.
That made me conclude that those macros which are qualified for a set of events will definitely work in the library.
The documentation for sc_lookup does not show onApplicationInit for a form application. But the MenuApplications (all types) support the usage in onApplicationInit.
This prompted me to make this post.
If you have used a function with sc_lookup in the onApplicationInit, please let me know. Not only will i correct/ remove this post, will also find where my code is wrong. Strange though the same code works in onScriptInit
Thanks once again

Hi,
You cannot use database macros in onApplicationInit; use onScriptInit instead.
See the macros in red.

Exactly the point . Check my first post as below

The Issue: Macro Compatibility and “Null” Errors

A common cause of Fatal error: Call to a member function ... on null is calling an Internal Library function that contains database macros (like sc_lookup or sc_sql_injection) from an incompatible event. This happens because:

  • Execution Scope: Macros inside a library function often lose access to the application’s internal database connection object.
  • Event Restrictions: Every Scriptcase macro has a specific list of events where it is allowed to run. If a macro is prohibited in an event (like onApplicationInit), it will also fail if called via a library within that same event.

Step 1: Verify Macro Compatibility

Before placing a library call, check the Scriptcase Macro Documentation for every macro used inside your library function.

  • Check the Event List: Ensure the macro you are using is explicitly listed as supported for the event calling the library.
  • The onApplicationInit Trap: Many database macros are not fully supported or initialized in onApplicationInit. Moving these calls to onScriptInit is often the solution, as it is the first event where the database connection is guaranteed to be ready.