APEX 3.2: Page Process If Statement - oracle-apex

I have page process that I am trying to run that will send an email if certain conditions are met. However, if the process does not pull any data, I still want the page to open like normal. I am stuck on 'ORA-01403: no data found' when the following Select statement runs:
select issue_added_to_alm into issue_added from Table_2 WHERE issue_Requester = '1234' and Issue_Added_To_ALM = '1' and EMAIL_NOTIFICATION = '0';
If I remove the initial select statement and replace the IF statement with the following:
IF :NEW.ISSUE_ADDED_TO_ALM = 1 THEN
I get: ORA-01008: not all variables bound
How do I achieve this?
declare
l_body varchar2(4000);
l_to_address varchar2(2000);
l_from varchar2(200) :='dq.issue.mgmt#jpmchase.com';
l_summary varchar2(1000);
l_description varchar2(4000);
l_ALM_ID varchar2(100);
l_subject varchar2(400);
l_added_to_alm varchar2(200);
issue_added varchar2(200);
begin
select
issue_added_to_alm
into issue_added
from Table_2
WHERE issue_Requester = '1234'
and Issue_Added_To_ALM = '1' and EMAIL_NOTIFICATION = '0';
if issue_added = 1 Then
select EMP_EMAIL_NAME
into l_to_address
from Table_1 where emp_id = '1234';
select
ISSUE_Summary,ISSUE_DESCRIPTION,ALM_ISSUE_ID,email_notification
into l_summary,l_description,l_ALM_ID,l_added_to_alm
FROM Table_2
WHERE issue_Requester = '1234' and Issue_Added_To_ALM = '1' and EMAIL_NOTIFICATION = '0';
l_subject := l_ALM_ID || 'Request has been created.';
l_body := '<style type="text/css">
p{font-family: Calibri, Arial, Helvetica, sans-serif;
font-size:12pt;
margin-left:30px;
}
</style>';
l_body := l_body || '<p>Your request has been created.</p>';
l_body := l_body || '<p>Data Quality Center received the following request:</p>';
l_body := l_body || '<p>Request ID: '|| l_ALM_ID ||'</p>';
l_body := l_body || '<p>Request Title: '|| l_summary||'</p>';
l_body := l_body || '<p>Request Description: '|| l_description||'</p>';
HTMLDB_MAIL.SEND(
P_TO => l_to_address,
P_FROM => l_from,
P_BODY => l_body,
P_BODY_HTML => l_body,
P_SUBJ => l_subject,
P_CC => l_from);
wwv_flow_mail.push_queue(
P_SMTP_HOSTNAME => 'mail.mailserver.net',
P_SMTP_PORTNO => '12'
);
UPDATE Table_2 set EMAIL_NOTIFICATION = 1 where ALM_ISSUE_ID = :l_ALM_ID;
end if;
end;
Many thanks in advance.

You just need to add an exception handler before the final end;:
...
exception
when no_data_found then
null;
end;

Related

Extract Array value without attribute name - Oracle Apex_json

DECLARE
lv_json_values apex_json.t_values;
lv_items_count NUMBER := 0;
lv_id NUMBER := 0;
ip_json CLOB := '{ "items": [{ "tableName": "id_TRACKER", "count": 3, "columnNames": ["id"], "rows": [ ["9"], ["1"], ["2"]] }] }';
BEGIN
APEX_JSON.parse (p_source => ip_json, p_values => lv_json_values);
lv_items_count := apex_json.get_number(p_path => 'items[1].count',
p_values => lv_json_values);
dbms_output.put_line ('items Count : '||lv_items_count);
FOR lr_1 IN 1 .. lv_items_count
LOOP
lv_id := apex_json.get_number(p_path => 'items[1].rows[%d].',
p0 => 1, p1 => lr_1, p_values => lv_json_values);
dbms_output.put_line ('id : '||lv_id);
END LOOP;
end;
Since values id values 9,1 and 2 are wrapped as array inside array, all attempts to get all 3 values have failed,
I can get only the 1st id; when I do loop it this way; however - can never get 2nd and 3rd value.
Any help will be appreciated very much.
FOR lr_1 IN 1 .. lv_items_count
LOOP
lv_id := apex_json.get_number(p_path => 'items[1].rows[1][%d]',
p0 => lr_1, p_values => lv_json_values);
dbms_output.put_line ('id : '||lv_id);
END LOOP;
Here is your answer;
DECLARE
lv_json_values apex_json.t_values;
lv_items_count NUMBER := 0;
lv_id NUMBER := 0;
ip_json CLOB := '{ "items": [{ "tableName": "id_TRACKER", "count": 3, "columnNames": ["id"], "rows": [ ["9"], ["1"], ["2"]] }] }';
BEGIN
APEX_JSON.parse (p_source => ip_json, p_values => lv_json_values);
lv_items_count := apex_json.get_count(p_path => 'items[1].rows',p_values => lv_json_values);
dbms_output.put_line ('items Count : '||lv_items_count);
FOR i IN 1 .. lv_items_count
LOOP
lv_id := apex_json.get_number(p_path => 'items[1].rows[%d][1]',p0 => i, p_values => lv_json_values);
dbms_output.put_line ('id : '||lv_id);
END LOOP;
end;

Getting null value from 2nd level nested json array when using apex_json :

I have tried several options, not sure what I am missing? thanks much. the filter_value in output always seems to be null. I am expecting to see something like this:
FILTER array count : 2
lov_for : Ids
v_count_2 : 3
filter_value : 1
filter_value : 2
filter_value : 3
lov_for : Amounts
v_count_2 : 4
filter_value : 20
filter_value : 30
filter_value : 50
filter_value : 60
DECLARE
v_a VARCHAR2(5000) :=
'{
"lovFilters": [
{
"lovFor": "Ids",
"values": [1,2,3]
},
{
"lovFor": "Amounts",
"values": [20,30,50,60]
}
]
}';
v_array_count NUMBER := 0;
lov_for VARCHAR2(30);
filter_value VARCHAR2(256);
v_count_2 NUMBER:= 0;
BEGIN
APEX_JSON.parse(v_a);
v_array_count := APEX_JSON.get_count(p_path => 'lovFilters');
dbms_output.put_line ('FILTER array count : '||v_array_count);
FOR lr_i IN 1 .. v_array_count
LOOP
dbms_output.put_line ('-------------------------');
lov_for := APEX_JSON.get_varchar2 (p_path => 'lovFilters[%d].lovFor', p0 => lr_i);
dbms_output.put_line ('lov_for : '||lov_for);
v_count_2 := APEX_JSON.get_count(p_path => 'lovFilters[%d].values', p0 => lr_i);
dbms_output.put_line ('v_count_2 : '||v_count_2);
dbms_output.put_line ('--------------------------');
FOR lr_n IN 1 .. v_count_2
LOOP
filter_value :=
APEX_JSON.get_varchar2(p_path => 'lovFilters[%d].values[%d]', p0 => lr_i, p1 => lr_n, p_values => l_json_values);
dbms_output.put_line ('filter_value : '||filter_value);
END LOOP;
END LOOP;
END;
Not sure what the problem was. Your code complained about l_json_values not being defined - maybe you copied some code from elsewhere ?
The following works:
DECLARE
l_json_text VARCHAR2(5000);
l_json_values apex_json.t_values;
l_array_count NUMBER := 0;
l_lov_for VARCHAR2(30);
l_filter_value VARCHAR2(256);
l_count_2 NUMBER:= 0;
BEGIN
l_json_text :=
'{
"lovFilters": [
{
"lovFor": "Ids",
"values": [1,2,3]
},
{
"lovFor": "Amounts",
"values": [20,30,50,60]
}
]
}';
apex_json.parse(
p_values => l_json_values,
p_source => l_json_text
);
l_array_count := APEX_JSON.get_count(p_path => 'lovFilters',p_values => l_json_values);
dbms_output.put_line ('FILTER array count : '||l_array_count);
FOR lr_i IN 1 .. l_array_count
LOOP
dbms_output.put_line ('-------------------------');
l_lov_for := APEX_JSON.get_varchar2 (p_path => 'lovFilters[%d].lovFor', p0 => lr_i, p_values => l_json_values);
dbms_output.put_line ('l_lov_for : '||l_lov_for);
l_count_2 := APEX_JSON.get_count(p_path => 'lovFilters[%d].values', p0 => lr_i, p_values => l_json_values);
dbms_output.put_line ('l_count_2 : '||l_count_2);
dbms_output.put_line ('--------------------------');
FOR lr_n IN 1 .. l_count_2
LOOP
l_filter_value :=
APEX_JSON.get_varchar2(p_path => 'lovFilters[%d].values[%d]', p0 => lr_i, p1 => lr_n, p_values => l_json_values);
dbms_output.put_line ('l_filter_value : '||l_filter_value);
END LOOP;
END LOOP;
END;
/

Error: SyntaxError: Unexpected end of JSON input APEX 5.1

I have this problem:
I have this JavaScript code in a Dynamic Action:
var vMe = $(this.triggeringElement);
var vRow = $(vMe).parents(".meAllRow");
var vSeqID = $(vRow).find("[headers=SEQ_ID]").html();
var vEstado = $(vRow).find("[name=f01]").val();
apex.server.process("ajx_Cambia_estado",{x01:vSeqID,x02:vEstado});
and this is the PL-SQL CODE
DECLARE
vEstado VARCHAR2(1);
vSeq NUMBER := to_number(APEX_APPLICATION.g_x01);
BEGIN
IF (APEX_APPLICATION.g_x02 = 'A') THEN
vEstado := 'I';
ELSE
vEstado := 'A';
END IF;
APEX_COLLECTION.UPDATE_MEMBER (
p_collection_name => 'DINAMIC_LIST',
p_seq => vSeq,
p_c002 => vEstado);
END;
When I execute the dynamic action, it throws this error:
SyntaxError: Unexpected end of JSON
but when I put a return in the PL-SQL like:
htp.p('"process":"finish"');
the error disappears. But I don't need to send a response message, in Apex 4.2 I don't have this problem.
Try:
DECLARE
vEstado VARCHAR2(1);
vSeq NUMBER := to_number(APEX_APPLICATION.g_x01);
BEGIN
IF (APEX_APPLICATION.g_x02 = 'A') THEN
vEstado := 'I';
ELSE
vEstado := 'A';
END IF;
APEX_COLLECTION.UPDATE_MEMBER (
p_collection_name => 'DINAMIC_LIST',
p_seq => vSeq,
p_c002 => vEstado);
apex_json.open_object;
apex_json.write('success', true);
apex_json.close_object;
END;

Postgres function not returning the out parameters as expected

I am writing a function in postgres sql to check if eamil is in the table or not. if exists then check status.
CREATE OR REPLACE FUNCTION check_email_status_from_master_list(IN email_to_check character varying, OUT already_exists boolean, OUT dosend boolean)
AS
$BODY$
DECLARE
already_exists boolean := False;
doSend boolean := False;
status character varying(255);
BEGIN
IF EXISTS(SELECT true FROM sendmessage_master_list_emails WHERE email=email_to_check) THEN
-- do something
already_exists := True;
SELECT email_status INTO status FROM sendmessage_master_list_emails WHERE email=email_to_check;
--SELECT sendmessage_master_list_emails into status FROM sendmessage_master_list_emails WHERE email=email_to_check;
if(status='Bounce') then
doSend := False;
elsif(status='Invalid Email') then
doSend := False;
elsif(status='Delivered') then
doSend :=True;
else
doSend := False;
end if;
ELSE
already_exists := False;
doSend := False;
END IF;
END;
$BODY$
LANGUAGE plpgsql
I called this function from django by passing three emails. where first two emails are not in the database and the last one is in the database and status is Bounce.
users = {1 : { "name" : "M user", "email" : "abc.official#gmail.com" , "phone" : "123456456"},
2 : { "name" : "M user", "email" : "abc.official#gmail.com" , "phone" : "1234564569" },
3 : { "name" : "M user", "email" : "abc.xyz#gmail.com" , "phone" : "1234564565" }}
# Check First if the email is invalid & spam from the master list
for keys in users:
c = connection.cursor()
try:
c.execute("BEGIN")
c.callproc("check_email_status_from_master_list", [users[keys]["email"]])
resultss = c.fetchall()
c.execute("COMMIT")
except Exception as e:
print e
finally:
for item in resultss:
print item
c.close()
the function returns the output like this instead of false or true.
(None, None)
(None, None)
(None, None)
The reason OUT isn't working as expected is because you are redeclaring the two variables in the DECLARE section.
The two lines highlighted below, should be in the BEGIN/END block and not in the DECLARE block. When in the DECLARE block, the parser considers this as two local variables (just with the same name as the OUT variables). This is the reason why, when returning the OUT variables are NULLs and therefore you get an empty output.
Here is the sample Function with the said change:
CREATE OR REPLACE FUNCTION check_email_status_from_master_list(IN email_to_check character varying, OUT already_exists boolean, OUT dosend boolean)
AS
$BODY$
DECLARE
status character varying(255);
BEGIN
already_exists := False; -- MOVE THESE LINES FROM DECLARE ... to BEGIN/END block
doSend := False; -- MOVE THESE LINES FROM DECLARE ... to BEGIN/END block
IF EXISTS(SELECT true FROM sendmessage_master_list_emails WHERE email=email_to_check) THEN
-- do something
already_exists := True;
SELECT email_status INTO status FROM sendmessage_master_list_emails WHERE email=email_to_check;
--SELECT sendmessage_master_list_emails into status FROM sendmessage_master_list_emails WHERE email=email_to_check;
if(status='Bounce') then
doSend := False;
elsif(status='Invalid Email') then
doSend := False;
elsif(status='Delivered') then
doSend :=True;
else
doSend := False;
end if;
ELSE
already_exists := False;
doSend := False;
END IF;
END;
$BODY$
LANGUAGE plpgsql;

APEX- APEX_WEB_SERVICES.MAKE_REST_REQUEST for POST - attachment of files

I am facing problem in uploading a file/ file attachments using RESTful API's. I am aware of APEX_WEB_SERVICES.MAKE_REST_REQUEST, but its giving me errors. If any1 has earlier used it, plz post an example or explain how it works.
My approach code:
declare
l_clob varchar2(32767);
l_in clob;
l_blob blob;
secure varchar2(100);
BEGIN
/*----------Setting Headers----------------------------------------*/
apex_web_service.g_request_headers(1).name := 'Accept';
apex_web_service.g_request_headers(1).Value := 'multipart/form-data';
apex_web_service.g_request_headers(1).name := 'Content-Type';
apex_web_service.g_request_headers(1).value := 'multipart/form-data';
/*-----------------------------------------------------------------*/
/*------------Calling Authentication Rest service------------------*/
l_clob := apex_web_service.make_rest_request(
p_url => 'My url to authenticate'
p_http_method => 'POST',
p_username => 'XXXXXX',
p_password => '######',
p_wallet_path => 'file:C:\Users\',
p_wallet_pwd => '5162);
/*------------- Retrieving Cookies---------------------------------*/
apex_collection.create_or_truncate_collection('P31_RESP_COOKIES');
for i in 1.. apex_web_service.g_response_cookies.count loop
IF (apex_web_service.g_response_cookies(i).secure) THEN
secure := 'Y';
ELSE
secure := 'N';
END IF;
apex_collection.add_member(p_collection_name => 'P31_RESP_COOKIES',
p_c001 => apex_web_service.g_response_cookies(i).name,
p_c002 => apex_web_service.g_response_cookies(i).value,
p_c003 => apex_web_service.g_response_cookies(i).domain,
p_c004 => apex_web_service.g_response_cookies(i).expire,
p_c005 => apex_web_service.g_response_cookies(i).path,
p_c006 => secure,
p_c007 => apex_web_service.g_response_cookies(i).version );
end loop;
/*------------------------------------------------------------------*/
/*------------- Setting Cookies-------------------------------------*/
for c1 in (select seq_id, c001, c002, c003, c004, c005, c006, c007
from apex_collections
where collection_name = 'P31_RESP_COOKIES' ) loop
apex_web_service.g_request_cookies(c1.seq_id).name := c1.c001;
apex_web_service.g_request_cookies(c1.seq_id).value := c1.c002;
apex_web_service.g_request_cookies(c1.seq_id).domain := c1.c003;
apex_web_service.g_request_cookies(c1.seq_id).expire := c1.c004;
apex_web_service.g_request_cookies(c1.seq_id).path := c1.c005;
if c1.c006 = 'Y' then
apex_web_service.g_request_cookies(c1.seq_id).secure := true;
else
apex_web_service.g_request_cookies(c1.seq_id).secure := false;
end if;
apex_web_service.g_request_cookies(c1.seq_id).version := c1.c007;
end loop;
select test_data into l_blob from my_table; -- gets blob datatype file
l_in := '<Entity Type="attachments">
<Fields>
<Field Name="filename">
<Value>sample case</Value>
</Field>
<Field Name="description">
<Value></Value>
</Field>
<Field Name="override-existing-attachment">
<Value>n<value/>
</Field>
<Field Name="ref-subtype">
<Value>0</Value>
</Field>
<Field Name="data">
<Value>';
l_in := l_in||apex_web_service.blob2clobbase64(l_blob);
dbms_output.put_line(l_in);
l_in := l_in||'</Value>
</Field>
</Fields>
</Entity>';
/*------------------------------------------------------------------*/
apex_web_service.g_request_headers(1).name := 'Accept';
apex_web_service.g_request_headers(1).Value := 'multipart/form-data';
apex_web_service.g_request_headers(1).name := 'Content-Type';
apex_web_service.g_request_headers(1).value := 'multipart/form-data';
/*-- not sure with the following code--*/
l_clob := apex_web_service.make_rest_request(p_url => 'myurl/tests/13142/attachments',
p_http_method => 'POST',
p_body = l_in,
p_body_blob => l_blob,
p_wallet_path => 'same as above',
p_wallet_pwd => 'same as above');
end;
Achieved it as:
/*change dis part in above code*/
select test_data into l_blob from h_attachments;
/*------------------------------------------------------------------*/
apex_web_service.g_request_headers(1).name := 'Accept';
apex_web_service.g_request_headers(1).Value := 'application/xml';
apex_web_service.g_request_headers(1).name := 'Content-Type';
apex_web_service.g_request_headers(1).value := 'application/octet-stream';
apex_web_service.g_request_headers(1).name := 'Slug';
apex_web_service.g_request_headers(1).value := 'filename.xls';
l_clob := apex_web_service.make_rest_request(p_url => 'your url path',
p_http_method => 'POST',
p_body_blob => l_blob,
p_wallet_path => 'file:C:\Users\ur path',
p_wallet_pwd => 'passwd');