In this blog post, we will be working through a case study on how to use APEX_PLUGIN_UTIL.IS_COMPONENT_USED in Oracle APEX. This amazing function can save you a lot of work because it can enforce build options, authorization schemes, conditions, etc… that you have already defined within Shared Components.
If you are unfamiliar with IS_COMPONENT_USED, I suggest you also take a look at a wonderful article on this topic written by Anton Nielsen. That was my initial starting point, and this post will be expanding upon his ideas.
The specific case study I’m going to focus on is when you need to use a list, but cannot use a list region. The long road would be to essentially add the same conditions within a query so you can ensure users only see what they’re meant to see. As a developer, this adds a future break point because now you need to maintain two sets of the same logic. Read on to learn more about the easier way.
1. Let’s start by creating a list (in Shared Components / Lists) called My Awesome List:
Always Show
- No Conditions
Never Show
- A Condition Type of Never
Conditionally Show
- A Condition Type of Expression
- Language: PL/SQL
- Expression 1: :APP_USER = ‘ANDREW_ADMIN’
2. Now we can add a list region to a page.
3. Run the page and verify everything is working as expected.
4. Let’s add a Classic Report Region that queries this list.
Here’s the code for the query:
select entry_text from APEX_APPLICATION_LIST_ENTRIES
where application_id = :APP_ID
and list_name = ‘My Awesome List’
5. Run the page again and observe that our Classic Report returns all items in our list, but does not enforce our conditions.
6. In order to get our list to enforce the conditions applied, we need to use APEX_PLUGIN_UTIL.IS_COMPONENT_USED. There is just one minor problem — it returns a Boolean.
7. To make this easier to use, we can follow Anton Nielsen’s suggestion and create a wrapper function (in SQL Workshop / SQL Commands) to Return a ‘Y’ or ‘N’ value.
create or replace function is_component_used_yn
p_build_option_id IN NUMBER DEFAULT NULL,
p_authorization_scheme_id IN VARCHAR2,
p_condition_type IN VARCHAR2,
p_condition_expression1 IN VARCHAR2,
p_condition_expression2 IN VARCHAR2,
p_component IN VARCHAR2 DEFAULT NULL )
return varchar2 is
begin
return case when
apex_plugin_util.is_component_used (
p_build_option_id => p_build_option_id,
p_authorization_scheme_id => p_authorization_scheme_id,
p_condition_type => p_condition_type,
p_condition_expression1 => p_condition_expression1,
p_condition_expression2 => p_condition_expression2,
p_component => p_component)
then 'Y'
else 'N'
end;
end is_component_used_yn;
8. If we apply the code below, it will evaluate and enforce the conditions we have already applied to the List within Shared Components.
is_component_used_yn(p_build_option_id => BUILD_OPTION
,p_authorization_scheme_id => AUTHORIZATION_SCHEME_ID
,p_condition_type => CONDITION_TYPE_CODE
,p_condition_expression1 => CONDITION_EXPRESSION1
,p_condition_expression2 => CONDITION_EXPRESSION2) = 'Y'
9. Now we can rewrite our query for our Classic Report Region.
Query Code
select entry_text
from APEX_APPLICATION_LIST_ENTRIES
where application_id = :APP_ID
and list_name = 'My Awesome List'
and is_component_used_yn(p_build_option_id => BUILD_OPTION
,p_authorization_scheme_id => AUTHORIZATION_SCHEME_ID
,p_condition_type => CONDITION_TYPE_CODE
,p_condition_expression1 => CONDITION_EXPRESSION1
,p_condition_expression2 => CONDITION_EXPRESSION2) = 'Y'
10. Run the page and see the results:
11. Voilà! Our conditions in the Classic Report are now being evaluated and enforced in the same way they are in the List region. In my example, I only added expressions, but the great thing about this is that it will also work with build options and authorization schemes!
Looking to deepen your APEX expertise? Explore Traust’s other Oracle APEX tutorials and articles, where we dive into advanced APEX topics, tips, and best practices to help you unlock the full potential of your applications. Start enhancing your APEX skills with insights from our experts!
References
- https://docs.oracle.com/en/database/oracle/apex/24.1/aeapi/IS_COMPONENT_USED-Function.html#GUID-7BE7EA7C-DCBA-4940-8FC8-8F6EB849E3AE
- https://apexdebug.com/apexpluginutiliscomponentused
Requirements
- APEX 22x or higher
Disclaimer
We do not take responsibility for any unintended or unwanted consequences in your instance of Oracle, Oracle APEX, or related products as a result of reading our blogs or following our guides. Though the information is fully tested and generally safe to use, our lawyers really have a thing against admitting potential wrongdoing. If it makes you feel any better, they once had to defend a guy accused of stealing his own house. On its surface, that doesn’t make any sense, but that guy had multiple personalities, and they all didn’t get along.
The post The Amazing API: IS_COMPONENT_USED in Oracle APEX appeared first on Traust.