Friday, March 27, 2015

Oracle User Hook Registration & Deletion handy Scripts

-- To Delete OAB User Hook 

declare
    l_api_hook_call_id number;
    l_object_version_number number;
begin
    --
    select  api_hook_call_id
    ,       object_version_number
    into    l_api_hook_call_id
    ,       l_object_version_number
    from    hr_api_hook_calls
    where   call_package='XX_OAB_USER_HOOK_PKG'
    and call_procedure='CREATE_PRTT_ENRT_RESULT_A';
    --
    hr_api_hook_call_api.delete_api_hook_call(false,l_api_hook_call_id,l_object_version_number);
    --
    dbms_output.put_line('Delete User Hook '||sqlcode||'-'||sqlerrm);
    --
exception when others then
    dbms_output.put_line('Error Deleting User Hook '||sqlcode||' - '||sqlerrm);  
end;

declare
  ln_api_module_id  number;
begin
  --
  begin
      select api_module_id
      into   ln_api_module_id
      from   hr_api_hooks
      where  hook_package like '%BEN_PRTT_ENRT_RESULT_BK1%'
      and    api_hook_type='AP';
      --
      dbms_output.put_line('VALID API MODULE ID '||ln_api_module_id);
      --
  exception when others then
     ln_api_module_id := 0;
     dbms_output.put_line(' NOT A VALID API HOOK ID '||ln_api_module_id);
  end;
  if ln_api_module_id <> 0 then
      --
      -- Create all hook package body source code for one API module
      --
      hr_api_user_hooks_utility.create_hooks_one_module(ln_api_module_id);
      --
      -- Build the report text
      --
      hr_api_user_hooks_utility.write_one_errors_report(ln_api_module_id);
      --
      dbms_output.put_line('User Hook Pre-Process Registration Success');
      --  
  else
     --
     dbms_output.put_line('User Hook Pre-Process Registration Failed');
     --       
  end if; 
  --
end;

--To Register the usershook

declare
ln_api_hook_id              hr_api_hooks.api_hook_id%type;     
ln_api_hook_call_id         number;
ln_object_version_number    number;
begin
--
  begin
      select api_hook_id
      into   ln_api_hook_id
      from   hr_api_hooks
      where  hook_package like '%BEN_PRTT_ENRT_RESULT_BK1%'
      and    api_hook_type='AP';
      --
      dbms_output.put_line('VALID API HOOK ID '||ln_api_hook_id);
      --
  exception when others then
     ln_api_hook_id := 0;
     dbms_output.put_line(' NOT A VALID API HOOK ID '||ln_api_hook_id);
  end;
--
hr_api_hook_call_api.create_api_hook_call
  (p_validate                     => false,
   p_effective_date               => trunc(sysdate),
   p_api_hook_id                  => ln_api_hook_id, --1390,
   p_api_hook_call_type           => 'PP',
   p_sequence                     => 3000,
   p_enabled_flag                 => 'Y',
   p_call_package                 => 'XXS_OAB_USER_HOOK_PKG',
   p_call_procedure               => 'CREATE_PRTT_ENRT_RESULT_A',
   p_api_hook_call_id             => ln_api_hook_call_id,
   p_object_version_number        => ln_object_version_number);
 --
 dbms_output.put_line('p_api_hook_call_id '||ln_api_hook_call_id);
 dbms_output.put_line('p_object_version_number '||ln_object_version_number);
 --
 commit;
 --
exception when others then
    dbms_output.put_line('API Excepiton '||sqlcode||sqlerrm);
end;

declare
  ln_api_module_id  number;
begin
  --
  begin
      select api_module_id
      into   ln_api_module_id
      from   hr_api_hooks
      where  hook_package like '%BEN_PRTT_ENRT_RESULT_BK1%'
      and    api_hook_type='AP';
      --
      dbms_output.put_line('VALID API MODULE ID '||ln_api_module_id);
      --
  exception when others then
     ln_api_module_id := 0;
     dbms_output.put_line(' NOT A VALID API HOOK ID '||ln_api_module_id);
  end;
  if ln_api_module_id <> 0 then
      --
      -- Create all hook package body source code for one API module
      --
      hr_api_user_hooks_utility.create_hooks_one_module(ln_api_module_id);
      --
      -- Build the report text
      --
      hr_api_user_hooks_utility.write_one_errors_report(ln_api_module_id);
      --
      dbms_output.put_line('User Hook Pre-Processor Registration Success');
      --  
  else
     --
     dbms_output.put_line('User Hook Pre-Processor Registration Failed');
     --       
  end if; 
  --
end;

Oracle SSHR - Introduction


SSHR Personalizations


Absence Request in Oracle SSHR


Saturday, March 21, 2015

If you want to print a string vertically then use the following SQL Query


SELECT SUBSTR ('&&String', ROWNUM, 1)
FROM all_tables
WHERE ROWNUM <= LENGTH (TRIM ('&STRING'));

How To Send An SMS Using PL/SQL Code/Script



Sending An SMS Using A PL/SQL Code/Script:


Follow the simple steps below to send an sms using your cell phone through a pl/sql code.


1. Compile the below procedure on sql*plus


CREATE OR REPLACE PROCEDURE send_sms (
p_sender IN VARCHAR2,
p_recipient IN VARCHAR2,
p_message IN VARCHAR2
)
AS
mailcon UTL_SMTP.connection;
BEGIN
mailcon := UTL_SMTP.open_connection ('localhost');
UTL_SMTP.helo (mailcon, 'localhost');
UTL_SMTP.mail (mailcon, p_sender);
UTL_SMTP.rcpt (mailcon, p_recipient);
UTL_SMTP.DATA (mailcon,
'From: '
|| p_sender
|| UTL_TCP.crlf
|| 'To: '
|| p_recipient
|| UTL_TCP.crlf
|| 'Subject: SMS From Database'
|| UTL_TCP.crlf
|| p_message
);
UTL_SMTP.quit (mailcon);
END;


2. We need to execute the above procedure so as to send SMS. The systax is as follows:


exec SEND_SMS('nisheeth.exe@gmail.com', '9886012345@ideacellular.net','This is my first SMS');


Note: In place of the '9886012345@ideacellular.net' you need to provide your mobile number if your operator is Idea. You can refer the following list of mobile operator and the area of the service to use them for sending messages.


Andhra Pradesh AirTel-----------Mobile No@airtelap.com
Andhra Pradesh Idea Cellular-----Mobile No@ideacellular.net
Chennai Skycell/Airtel------------Mobile No@airtelchennai.com
Chennai RPG Cellular-------------Mobile No@rpgmail.net
Delhi Airtel-----------------------Mobile No@airtelmail.com
Delhi Hutch-----------------------Mobile No@delhi.hutch.co.in
Gujarat Airtel---------------------Mobile No@airtelmail.com
Gujarat Idea Cellular--------------Mobile No@ideacellular.net
Gujarat Celforce/Fascel-----------Mobile No@celforce.com
Goa Airtel-------------------------Mobile No@airtelmail.com
Goa BPL Mobile-------------------Mobile No@bplmobile.com
Goa Idea Cellular------------------Mobile No@ideacellular.net
Haryana Airtel--------------------Mobile No@airtelmail.com
Haryana Escotel-------------------Mobile No@escotelmobile.com
Himachal Pradesh Airtel-----------Mobile No@airtelmail.com
Karnataka Airtel-------------------Mobile No@airtelkk.com
Kerala Airtel-----------------------Mobile No@airtelkerala.com
Kerala Escotel---------------------Mobile No@escotelmobile.com
Kerala BPL Mobile-----------------Mobile No@bplmobile.com
Kolkata Airtel----------------------Mobile No @airtelkol.com
Madhya Pradesh Airtel-------------Mobile No@airtelmail.com
Maharashtra Airtel-----------------Mobile No@airtelmail.com
Maharashtra BPL Mobile-----------Mobile No@bplmobile.com
Maharashtra Idea Cellular----------Mobile No@ideacellular.net
Mumbai Airtel----------------------Mobile No@airtelmail.com
Mumbai BPL Mobile----------------Mobile No@bplmobile.com
Punjab Airtel-----------------------Mobile No@airtelmail.com
Pondicherry BPL Mobile------------Mobile No @bplmobile.com
Tamil Nadu Airtel-------------------Mobile No@airtelmail.com
Tamil Nadu BPL Mobile-------------Mobile No@bplmobile.com
Tamil Nadu Aircel-------------------Mobile No@airsms.com
UP (West) Escotel-------------------Mobile No@escotelmobile.com


Code to send E-mail from PLSQL by reading the file content

Business Case:
 This is a email program to sends email to the receiver/user by reading the text file content

Change the following things when you run this program:
 1. v_email_server parameter value. Put your mail server details.
 2. v_port parameter value. Generally, it is 25 only.
 3. v_dirpath parameter value. Directory path of the file to read
 4. v_filename parameter value. This is your text file name. (I put 10 lines in the text file for testing)


DECLARE

   v_file_handle    UTL_FILE.FILE_TYPE;

   v_conn           UTL_SMTP.CONNECTION;

   v_reply          UTL_SMTP.REPLY;

   v_email_server   VARCHAR2 (100):= 'lax02.lax.corp.int.gect.com';

   v_port           NUMBER        := 25;

 

   v_dirpath        VARCHAR2 (50) := '/usr/tmp';

   v_filename       VARCHAR2 (50) := 'testing123.txt';

   v_sender         VARCHAR2 (50) := 'name@mydomain.com';

   v_recpnt         VARCHAR2 (255):= 'name@senderdomain.com';

   v_msg            VARCHAR2 (32767);

   v_line           VARCHAR2 (1000);

   v_message        VARCHAR2 (1000);

   CRLF             VARCHAR2 (2)  := CHR (13) || CHR (10);




BEGIN




   /* Check if the file exists */

   BEGIN

      v_file_handle := UTL_FILE.FOPEN (v_dirpath, v_filename, 'R');

   EXCEPTION

      WHEN UTL_FILE.INVALID_PATH THEN

         RETURN;

      WHEN OTHERS THEN

         RETURN;

   END;




   /* Try connecting smtp server  and do handshake*/

   v_conn := UTL_SMTP.OPEN_CONNECTION (v_email_server, v_port);

   v_reply := UTL_SMTP.HELO (v_conn, v_email_server);

 

   IF v_reply.code != 250 THEN

       RETURN;

   END IF;




   /*UTL_SMTP.MAIL initiates a mail transaction with the server. The destination is a mailbox.*/

   v_reply := UTL_SMTP.MAIL (v_conn, v_sender);




   IF v_reply.code != 250 THEN

      RETURN;

   END IF;




   /* UTL_SMTP.RCPT specifies the recipient of an e-mail message. */

   v_reply := UTL_SMTP.RCPT (v_conn, v_recpnt);




   IF v_reply.code != 250 THEN

      RETURN;

   END IF;




   /*UTL_SMTP.OPEN_DATA sends the DATA command after which you can use WRITE_DATA and WRITE_RAW_DATA to write a portion of the e-mail message.*/

   UTL_SMTP.OPEN_DATA (v_conn);

   v_message := 'This is an auto generated mail. Please do not reply to this mail.';

   v_msg     := 'Date: ' || TO_CHAR (SYSDATE, 'Mon DD yyyy hh24:mi:ss') || CRLF ||

                'From: ' || v_sender || CRLF ||

                'Subject: ' || 'Sample file' || CRLF ||

                'To: ' || v_recpnt || CRLF

                || v_message  || CRLF || CRLF;

                 

   /*UTL_SMTP.WRITE_DATA Writes a portion of the e-mail message*/

   UTL_SMTP.WRITE_DATA (v_conn, v_msg);




   /*Read each line of the mail and put it in the mail*/

   LOOP

      BEGIN

         UTL_FILE.GET_LINE (v_file_handle, v_line);

      EXCEPTION

         WHEN NO_DATA_FOUND THEN

            EXIT;

      END;




      v_msg := '*** truncated ***' || CRLF;

      v_msg := v_line || CRLF;




      UTL_SMTP.WRITE_DATA (v_conn, v_msg);

   END LOOP;




   UTL_FILE.FCLOSE (v_file_handle);




   /*UTL_SMTP.CLOSE_DATA call ends the e-mail message*/

   UTL_SMTP.CLOSE_DATA (v_conn);




   /*UTL_SMTP.QUIT terminates an SMTP session and disconnects from the server*/

   UTL_SMTP.QUIT (v_conn);

EXCEPTION

 when others then

   raise_application_error(-20000, SQLERRM);

END;


Note:

In a typical Oracle Apps environment

1. The file that has to be read has to kept in DB Node. Meaning database server. NOT on Appl Tier. Meaning NOT on Middle tier.

2. To find out the email server, The navigation is

Sysadmin Login

Workflow (Oracle Applications Manager) > Under Configuration 'Service Components' > select 'Workflow Notification Mailer' > Edit > Inbound EMail Account (IMAP) > Note Server Name