异常处理PL SQL

时间:2015-11-01 18:34:06

标签: sql plsql

我的应用程序有以下两个处理整个事务的过程。 但是当我的嵌套过程抛出异常时,外部过程中执行的CRUD操作无法回滚。请帮我重新安排,或者我怎样才能更好地写作,以便我可以回滚我的交易。

PROCEDURE SP_SEND_CASE_NEXT_LEVEL
(
  p_wfId  IN NUMBER,
  p_assessmentId      IN NUMBER,
  p_prevLevelId       IN NUMBER,
  p_appType           IN NUMBER,
  p_sequenceNumber    IN NUMBER,
  p_subSequenceNumber IN NUMBER,
  p_userCode          IN VARCHAR2,
  p_companyCode       IN VARCHAR2,
  p_camNumber         IN VARCHAR2,
  p_reviewType        IN NUMBER,
  p_unitType          IN NUMBER,
  p_regionCode        IN NUMBER,
  p_caseType          IN VARCHAR2,
  p_isApproved        IN VARCHAR2,
  p_fromUserCode      IN VARCHAR2,
  P_ERRORDETAIL       OUT VARCHAR2
) IS

  V_Newlevelid           NUMBER(8);
  V_NewWfId              NUMBER(8);
  v_StepCompleted        NUMBER(2); 
  v_caseComplete         VARCHAR2(1);
  v_isStepComplete       VARCHAR2(1);
  v_isCompanyPicked      VARCHAR2(1);
  v_subUserInWorkflow    NUMBER(1);
  v_isPresent            NUMBER(1);
  v_inConsistentWorkFlow VARCHAR2(2);
  v_subUserCode          VARCHAR2(8);
  v_errmsg               VARCHAR2(50);
  v_userDP               NUMBER(10, 2);
  v_sql                  VARCHAR2(2000);
  v_sub_seq_no           NUMBER(8);
  v_reversalSequenceNo   NUMBER(4);
  v_reversalAppType      NUMBER(2);
  v_NewCourierId        NUmber(8);
  v_courierCount          NUMBER(1);
  v_revSequenceNo       NUMBER(4);
  v_courierComments VARCHAR2(4000);
  v_required_count  NUMBER(2);
  v_sessionPriority NUMBER(1);
  v_customRequiredCount NUMBER(1);
BEGIN
  p_errorDetail          := 'Failure';
  v_inConsistentWorkFlow := 'N';
  v_reversalAppType      := 0;
  v_revSequenceNo         :=0;
  v_courierComments := '';
  v_customRequiredCount := 0;
  SAVEPOINT Start_Tran;
  BEGIN

    -- update aph_comp_leveldetails for initiator of case
    UPDATE Aph_Comp_Leveldetails
       SET Case_Complete    = 'Y',
           End_Date         = Sysdate,
           Is_Step_Complete = 'Y',
           is_approved      = p_isApproved
     WHERE User_Code = p_userCode
       AND Assessmentid = p_assessmentId
       and levelid = p_prevlevelid;

    -- update Aph_Comp_Header for case status
    UPDATE Aph_Comp_Header
       SET CASE_STATUS_DESC = 'Pending',
       COMP_REVERSE = 'N'           
     WHERE Assessmentid = p_assessmentId;

    -- update MST_TAT_CALC set case end time for previous level user
    UPDATE MST_TAT_CALC
       SET CASE_END_TIME = sysdate,
       RULE_END_TIME = sysdate,
       days = fn_get_days(RULE_START_TIME, sysdate, REASON)
     WHERE User_Code = p_userCode -- TODO : to be removed
       and LEVELID = p_prevLevelId
       AND ASSESSMENTID = p_assessmentId
       and reason <> 'C' and CASE_END_TIME is null;
    -------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------

    SELECT COUNT(CASE_COMPLETE) into v_required_count
    FROM APH_COMP_LEVELDETAILS
    WHERE ASSESSMENTID = p_assessmentid
    AND SEQ_NO         = p_sequenceNumber
    AND APP_TYPE       = p_appType
    and COMP_REVERSE = 'N'
    and COMP_WITHDRAWN = 'N'
    and COMP_REJECTED = 'N';

    SELECT COUNT(CASE_COMPLETE) into v_StepCompleted
    FROM APH_COMP_LEVELDETAILS
    WHERE ASSESSMENTID = p_assessmentid
    AND SEQ_NO         = p_sequenceNumber
    AND APP_TYPE       = p_appType
    AND CASE_COMPLETE  = 'Y'
    and COMP_REVERSE = 'N'
    and COMP_WITHDRAWN = 'N'
    and COMP_REJECTED = 'N';



                            BEGIN
                              -- aph_comp_leveldetails
                              INSERT INTO aph_comp_leveldetails
                                (WF_ID,
                                 ASSESSMENTID,
                                 LEVELID,
                                 User_Code,
                                 Case_Complete,
                                 COMP_FROMUSER,
                                 SEQ_NO,
                                 SUB_SEQ_NO,
                                 COMP_TOUSER,
                                 Start_Date,
                                 IS_STEP_COMPLETE,
                                 Is_Company_Picked,
                                 App_Type,
                                 WORK_FLOW_USER)
                              VALUES
                                (Send.Wf_Id,
                                 p_assessmentId,
                                 V_Newlevelid,
                                 USER_ON_HOLIDAY.User_code,
                                 'N',
                                 --send.user_id,
                                 P_USERCODE,
                                 Send.Sequence_No,
                                 v_sub_seq_no,
                                 USER_ON_HOLIDAY.User_code,
                                 Sysdate,
                                 'N',
                                 'N',
                                 Send.App_Type,
                                 Send.user_id);
                            EXCEPTION
                              WHEN OTHERS THEN
                                ROLLBACK;
                                v_errmsg := 'Unable to insert into aph_comp_leveldetails as previous level step not completed';
                                RETURN;
                            END;
    -------------------------------------------------------------------------------------------------
                            BEGIN
                              -- check if user is alreadily part of workflow.
                              SELECT COUNT(*)
                                INTO v_isPresent
                                FROM aph_wf_user
                               WHERE ASSESSMENTID = P_ASSESSMENTID
                                 and USER_ID = USER_ON_HOLIDAY.USER_CODE
                                 and is_wf_user = 'Y';
                            END;
    -------------------------------------------------------------------------------------------------
                            IF v_isPresent = 0 THEN
                              -- if user is not alreadily part of workflow then insert into aph_wf_user.
                              BEGIN
                                -- insert in aph_wf_user
                                INSERT INTO aph_wf_user
                                  (WF_ID,
                                   ASSESSMENTID,
                                   USER_ID,
                                   SEQUENCE_NO,
                                   APP_TYPE,
                                   IS_EDITABLE,
                                   USER_DP,
                                   UNIT_TYPE,
                                   CREATED_BY,
                                   CREATION_DATE)
                                VALUES
                                  (V_NewWfId,
                                   P_ASSESSMENTID,
                                   USER_ON_HOLIDAY.user_code,
                                   Send.Sequence_No,
                                   send.app_type,
                                   'N',
                                   v_userDP,
                                   send.unit_type,
                                   P_USERCODE,
                                   sysdate);
                              EXCEPTION
                                WHEN OTHERS THEN
                                  ROLLBACK;
                                  v_errmsg := 'Unable to insert into aph_comp_leveldetails as previous level step not completed';
                                  RETURN;
                              END;
                            END IF; -- end of  if v_isPresent = 0
    -------------------------------------------------------------------------------------------------
                            BEGIN
                              --mst_tat_calc
                              INSERT INTO mst_tat_calc
                                (ASSESSMENTID,
                                 LEVELID,
                                 COMPANY_CODE,
                                 USER_CODE,
                                 REASON,
                                 CASE_START_TIME,
                                 CASE_END_TIME,
                                 RULE_START_TIME,
                                 RULE_END_TIME,
                                 CAM_NUMBER,
                                 REVIEW_TYPE,
                                 UNIT_TYPE,
                                 REGION_CODE,
                                 LEVELID_COURIER_TO)
                              VALUES
                                (P_ASSESSMENTID,
                                 /*p_prevLevelId,*/
                                 V_NEWLEVELID, 
                                 P_COMPANYCODE,
                                 /*p_userCode,*/
                                 USER_ON_HOLIDAY.user_code,
                                 'C',--This should be value of enum CommonActionEnum.COURIER
                                 sysdate,
                                 null,
                                 sysdate,
                                 null,
                                 p_camNumber,
                                 p_reviewType,
                                 p_unitType,
                                 P_REGIONCODE,
                                 /*V_Newlevelid*/
                                 p_prevLevelId 
                                 );
    -------------------------------------------------------------------------------------------------
                              INSERT INTO mst_tat_calc
                                (ASSESSMENTID,
                                 LEVELID,
                                 COMPANY_CODE,
                                 USER_CODE,
                                 REASON,
                                 CASE_START_TIME,
                                 RULE_START_TIME,
                                 CAM_NUMBER,
                                 REVIEW_TYPE,
                                 UNIT_TYPE,
                                 REGION_CODE,
                                 DAYS)
                              VALUES
                                (P_ASSESSMENTID,
                                 V_NEWLEVELID,
                                 p_companyCode,
                                 USER_ON_HOLIDAY.user_code,
                                 'N',--This should be value of enum CommonActionEnum.NORMAL_APPROVAL
                                 sysdate,
                                 fn_get_rule_date(sysdate),
                                 p_camNumber,
                                 p_reviewType,
                                 p_unitType,
                                 p_regionCode,
                                 0);
                            EXCEPTION
                              WHEN OTHERS THEN
                                ROLLBACK;
                                v_errmsg := 'Unable to insert into mst_tat_calc as normal approver';
                                RETURN;
                            END;
    -------------------------------------------------------------------------------------------------
                            -- Check for if user has any courier
                            BEGIN
                              -- update in aph_user_Courier as courier status sent and sent date
                              SELECT COUNT(1)
                                INTO v_courierCount
                              FROM aph_user_courier
                              WHERE assessmentid = p_assessmentid
                                AND touser         = USER_ON_HOLIDAY.User_code
                                AND levelid        = p_prevlevelid;

                              if v_courierCount = 0 then
                               begin
                                 Pack_Aph_Getid.Proc_Aph_Getid('APH_USER_COURIER','COURIER_ID', v_NewCourierId); -- generate courier id.
                               end;

                               INSERT
                                INTO APH_USER_COURIER
                                  (
                                    COURIER_ID,
                                    ASSESSMENTID,
                                    LEVELID,
                                    FROMUSER,
                                    TOUSER,
                                    FROMUSER_COMMENTS,
                                    TOUSER_COMMENTS,
                                    FROMCOURIER_STATUS,
                                    TOCOURIER_STATUS,
                                    FROM_DATETIME,
                                    TO_DATETIME,
                                    DOCUMENTRECEIVED,
                                    RECEIVEDDATE
                                  )
                                  --values (v_NewCourierId, p_assessmentid, V_Newlevelid, p_usercode, Send.User_Id, null, null, 'SENT' , null , sysdate , null, null, null);
                                  VALUES
                                  (
                                    v_NewCourierId,
                                    p_assessmentid,
                                    V_Newlevelid,
                                    p_usercode,
                                    USER_ON_HOLIDAY.User_code,
                                    v_courierComments,
                                    NULL,
                                    'SENT' ,
                                    NULL ,
                                    sysdate ,
                                    NULL,
                                    NULL,
                                    NULL
                                  );
                              else
                                UPDATE aph_user_Courier
                                   SET fromcourier_status = 'SENT',
                                        tocourier_status = null,
                                       FROM_DATETIME      = SYSDATE
                                 WHERE assessmentid = p_assessmentid
                                   AND levelid = p_prevlevelid
                                   --AND touser = Send.User_Id;
                                   AND touser = USER_ON_HOLIDAY.User_code
                              end if;

                            EXCEPTION
                              WHEN OTHERS THEN
                                ROLLBACK;
                                v_errmsg := 'Unable to update aph_user_Courier';
                                RETURN;
                            END;    

               BEGIN
                               SP_COMMON_INSERTS(p_assessmentId, p_prevLevelId, V_Newlevelid, 'N', 'N',0,'0','N', p_errorDetail)
                             EXCEPTION
                              WHEN OTHERS THEN
                                p_Errordetail := 'Error in common';
                ROLLBACK;
                            END;

                            BEGIN
                              UPDATE APH_WF_USER set IS_EDITABLE = 'N' where USER_ID = Send.User_Id and ASSESSMENTID = p_assessmentId;
                            END;
                        END;
END SP_SEND_CASE_NEXT_LEVEL;

Procedure Sp_Common_Inserts
(
  --p_wfId In Number,
  p_assessmentId In Number,
  p_prevLevelId In Number,
  p_Newlevelid in number,
  p_withdraw in varchar2,
  p_rejected in varchar2,
  p_prevAssessmentId in Number,
  p_borrowerCode in varchar2,
  p_srm_lcr in varchar2,
  p_errorDetail Out Varchar2
) Is

v_wfId Number(8);
V_Errmsg Varchar2(50);
v_initiatorUserCode varchar2(50);
V_SrNo Number(8);

Begin
     -- Savepoint Start_Tran1;

      if p_withdraw = 'N' and p_rejected = 'N' then
      begin
      -- APH_ASSESSMENT_PARAMETER
      INSERT INTO APH_ASSESSMENT_PARAMETER
      (
      SR_NO,
      ASSESSMENTID,
      LEVELID,
      CAM_DATE,
      REVISION_DATE,
      SRM_EXPIRY_DATE,
      PART_PROGRAM,
      PRODUCT_PROGRAM,
      PRODUCT_PROGRM_EXPOSURE,
      PROGRM_PARAMETERS_ADHERED,
      IS_SEPARETE_APPROVER,
      CASH_BACKED_EXPOSURE,
      EXPOSURE_GURANTED,
      CLIMS_ID,
      CAM_LOGIN_DATE,
      BORROWER_RATING,
      RATING_TYPE,
      RATING_FINALISATION_DATE,
      INITIAL_LABELLING_DATE,
      CURRENT_REVIEW_DATE,
      LABEL_REVIEW_TYPE,
      LABEL,
      LAST_CLIENT_CALL_DATE,
      CALLED_ON_DATE,
      INTERFACE_DATE,
      COMP_CODE
      ) SELECT
      SR_NO,
      p_assessmentId,
      p_Newlevelid,
      CAM_DATE,
      REVISION_DATE,
      SRM_EXPIRY_DATE,
      PART_PROGRAM,
      PRODUCT_PROGRAM,
      PRODUCT_PROGRM_EXPOSURE,
      PROGRM_PARAMETERS_ADHERED,
      IS_SEPARETE_APPROVER,
      CASH_BACKED_EXPOSURE,
      EXPOSURE_GURANTED,
      CLIMS_ID,
      CAM_LOGIN_DATE,
      BORROWER_RATING,
      RATING_TYPE,
      RATING_FINALISATION_DATE,
      INITIAL_LABELLING_DATE,
      CURRENT_REVIEW_DATE,
      LABEL_REVIEW_TYPE,
      LABEL,
      LAST_CLIENT_CALL_DATE,
      CALLED_ON_DATE,
      INTERFACE_DATE,
      COMP_CODE
      From APH_ASSESSMENT_PARAMETER
      Where Levelid = p_prevlevelid;
      Exception 
      When No_Data_Found Then
      rollback;
      v_errmsg := 'Unable to insert into APH_ADDENDUM_DETAILS as no data found at previous level';
      return;
      end;

      end if;

      IF P_WITHDRAW = 'Y' or P_REJECTED = 'Y' THEN
      --APH_APPRAISAL_DETAILS
      begin
      INSERT INTO APH_APPRAISAL_DETAILS
      (
      ASSESSMENTID,
      COMPANY_CODE,
      CAM_FORMAT_ID,
      FILE_NAME,
      ORDER_ID,
      REPORTS_HTML_CONTENTS,
      SR_NO
      )SELECT
      p_assessmentId,
      COMPANY_CODE,
      CAM_FORMAT_ID,
      FILE_NAME,
      ORDER_ID,
      REPORTS_HTML_CONTENTS,
      SR_NO
      FROM APH_APPRAISAL_DETAILS
      WHERE ASSESSMENTID = P_prevAssessmentId
      AND FILE_NAME NOT IN (SELECT FILE_NAME FROM APH_FTL_DETAILS WHERE FTL_ID IN(29,30,31));
      Exception 
      When No_Data_Found Then
      rollback;
      v_errmsg := 'Unable to insert into APH_BORROWER_DETAILS as no data found at previous level';
      Return;
      End;
      END IF;

  p_Errordetail:='SUCCESS';

     Exception
       When Others Then
         Rollback;
           p_Errordetail :=v_errmsg;

End Sp_Common_Inserts;

如果嵌套过程sp_common_insert抛出异常,请告诉我如何处理上述两个过程中的异常。

1 个答案:

答案 0 :(得分:1)

而不是使用RETURN,而是使用RAISE。这样,子块中遇到的错误也会触发设置主块中p_Errordetail值的异常。另一件事是在SP_SEND_CASE_NEXT_LEVEL过程的主块中没有EXCEPTION块。这是您的代码的修订版:

  PROCEDURE SP_SEND_CASE_NEXT_LEVEL
  (
    p_wfId  IN NUMBER,
    p_assessmentId      IN NUMBER,
    p_prevLevelId       IN NUMBER,
    p_appType           IN NUMBER,
    p_sequenceNumber    IN NUMBER,
    p_subSequenceNumber IN NUMBER,
    p_userCode          IN VARCHAR2,
    p_companyCode       IN VARCHAR2,
    p_camNumber         IN VARCHAR2,
    p_reviewType        IN NUMBER,
    p_unitType          IN NUMBER,
    p_regionCode        IN NUMBER,
    p_caseType          IN VARCHAR2,
    p_isApproved        IN VARCHAR2,
    p_fromUserCode      IN VARCHAR2,
    P_ERRORDETAIL       OUT VARCHAR2
  ) IS

    V_Newlevelid           NUMBER(8);
    V_NewWfId              NUMBER(8);
    v_StepCompleted        NUMBER(2); 
    v_caseComplete         VARCHAR2(1);
    v_isStepComplete       VARCHAR2(1);
    v_isCompanyPicked      VARCHAR2(1);
    v_subUserInWorkflow    NUMBER(1);
    v_isPresent            NUMBER(1);
    v_inConsistentWorkFlow VARCHAR2(2);
    v_subUserCode          VARCHAR2(8);
    v_errmsg               VARCHAR2(50);
    v_userDP               NUMBER(10, 2);
    v_sql                  VARCHAR2(2000);
    v_sub_seq_no           NUMBER(8);
    v_reversalSequenceNo   NUMBER(4);
    v_reversalAppType      NUMBER(2);
    v_NewCourierId        NUmber(8);
    v_courierCount          NUMBER(1);
    v_revSequenceNo       NUMBER(4);
    v_courierComments VARCHAR2(4000);
    v_required_count  NUMBER(2);
    v_sessionPriority NUMBER(1);
    v_customRequiredCount NUMBER(1);
  BEGIN
    p_errorDetail          := 'Failure';
    v_inConsistentWorkFlow := 'N';
    v_reversalAppType      := 0;
    v_revSequenceNo         :=0;
    v_courierComments := '';
    v_customRequiredCount := 0;
    SAVEPOINT Start_Tran;
    BEGIN

      -- update aph_comp_leveldetails for initiator of case
      UPDATE Aph_Comp_Leveldetails
         SET Case_Complete    = 'Y',
             End_Date         = Sysdate,
             Is_Step_Complete = 'Y',
             is_approved      = p_isApproved
       WHERE User_Code = p_userCode
         AND Assessmentid = p_assessmentId
         and levelid = p_prevlevelid;

      -- update Aph_Comp_Header for case status
      UPDATE Aph_Comp_Header
         SET CASE_STATUS_DESC = 'Pending',
         COMP_REVERSE = 'N'           
       WHERE Assessmentid = p_assessmentId;

      -- update MST_TAT_CALC set case end time for previous level user
      UPDATE MST_TAT_CALC
         SET CASE_END_TIME = sysdate,
         RULE_END_TIME = sysdate,
         days = fn_get_days(RULE_START_TIME, sysdate, REASON)
       WHERE User_Code = p_userCode -- TODO : to be removed
         and LEVELID = p_prevLevelId
         AND ASSESSMENTID = p_assessmentId
         and reason <> 'C' and CASE_END_TIME is null;
      -------------------------------------------------------------------------------------------------
      -------------------------------------------------------------------------------------------------

      SELECT COUNT(CASE_COMPLETE) into v_required_count
      FROM APH_COMP_LEVELDETAILS
      WHERE ASSESSMENTID = p_assessmentid
      AND SEQ_NO         = p_sequenceNumber
      AND APP_TYPE       = p_appType
      and COMP_REVERSE = 'N'
      and COMP_WITHDRAWN = 'N'
      and COMP_REJECTED = 'N';

      SELECT COUNT(CASE_COMPLETE) into v_StepCompleted
      FROM APH_COMP_LEVELDETAILS
      WHERE ASSESSMENTID = p_assessmentid
      AND SEQ_NO         = p_sequenceNumber
      AND APP_TYPE       = p_appType
      AND CASE_COMPLETE  = 'Y'
      and COMP_REVERSE = 'N'
      and COMP_WITHDRAWN = 'N'
      and COMP_REJECTED = 'N';



                              BEGIN
                                -- aph_comp_leveldetails
                                INSERT INTO aph_comp_leveldetails
                                  (WF_ID,
                                   ASSESSMENTID,
                                   LEVELID,
                                   User_Code,
                                   Case_Complete,
                                   COMP_FROMUSER,
                                   SEQ_NO,
                                   SUB_SEQ_NO,
                                   COMP_TOUSER,
                                   Start_Date,
                                   IS_STEP_COMPLETE,
                                   Is_Company_Picked,
                                   App_Type,
                                   WORK_FLOW_USER)
                                VALUES
                                  (Send.Wf_Id,
                                   p_assessmentId,
                                   V_Newlevelid,
                                   USER_ON_HOLIDAY.User_code,
                                   'N',
                                   --send.user_id,
                                   P_USERCODE,
                                   Send.Sequence_No,
                                   v_sub_seq_no,
                                   USER_ON_HOLIDAY.User_code,
                                   Sysdate,
                                   'N',
                                   'N',
                                   Send.App_Type,
                                   Send.user_id);
                              EXCEPTION
                                WHEN OTHERS THEN
                                  ROLLBACK;
                                  v_errmsg := 'Unable to insert into aph_comp_leveldetails as previous level step not completed';
                                  RAISE;
                              END;
      -------------------------------------------------------------------------------------------------
                              BEGIN
                                -- check if user is alreadily part of workflow.
                                SELECT COUNT(*)
                                  INTO v_isPresent
                                  FROM aph_wf_user
                                 WHERE ASSESSMENTID = P_ASSESSMENTID
                                   and USER_ID = USER_ON_HOLIDAY.USER_CODE
                                   and is_wf_user = 'Y';
                              END;
      -------------------------------------------------------------------------------------------------
                              IF v_isPresent = 0 THEN
                                -- if user is not alreadily part of workflow then insert into aph_wf_user.
                                BEGIN
                                  -- insert in aph_wf_user
                                  INSERT INTO aph_wf_user
                                    (WF_ID,
                                     ASSESSMENTID,
                                     USER_ID,
                                     SEQUENCE_NO,
                                     APP_TYPE,
                                     IS_EDITABLE,
                                     USER_DP,
                                     UNIT_TYPE,
                                     CREATED_BY,
                                     CREATION_DATE)
                                  VALUES
                                    (V_NewWfId,
                                     P_ASSESSMENTID,
                                     USER_ON_HOLIDAY.user_code,
                                     Send.Sequence_No,
                                     send.app_type,
                                     'N',
                                     v_userDP,
                                     send.unit_type,
                                     P_USERCODE,
                                     sysdate);
                                EXCEPTION
                                  WHEN OTHERS THEN
                                    ROLLBACK;
                                    v_errmsg := 'Unable to insert into aph_comp_leveldetails as previous level step not completed';
                                    RAISE;
                                END;
                              END IF; -- end of  if v_isPresent = 0
      -------------------------------------------------------------------------------------------------
                              BEGIN
                                --mst_tat_calc
                                INSERT INTO mst_tat_calc
                                  (ASSESSMENTID,
                                   LEVELID,
                                   COMPANY_CODE,
                                   USER_CODE,
                                   REASON,
                                   CASE_START_TIME,
                                   CASE_END_TIME,
                                   RULE_START_TIME,
                                   RULE_END_TIME,
                                   CAM_NUMBER,
                                   REVIEW_TYPE,
                                   UNIT_TYPE,
                                   REGION_CODE,
                                   LEVELID_COURIER_TO)
                                VALUES
                                  (P_ASSESSMENTID,
                                   /*p_prevLevelId,*/
                                   V_NEWLEVELID, 
                                   P_COMPANYCODE,
                                   /*p_userCode,*/
                                   USER_ON_HOLIDAY.user_code,
                                   'C',--This should be value of enum CommonActionEnum.COURIER
                                   sysdate,
                                   null,
                                   sysdate,
                                   null,
                                   p_camNumber,
                                   p_reviewType,
                                   p_unitType,
                                   P_REGIONCODE,
                                   /*V_Newlevelid*/
                                   p_prevLevelId 
                                   );
      -------------------------------------------------------------------------------------------------
                                INSERT INTO mst_tat_calc
                                  (ASSESSMENTID,
                                   LEVELID,
                                   COMPANY_CODE,
                                   USER_CODE,
                                   REASON,
                                   CASE_START_TIME,
                                   RULE_START_TIME,
                                   CAM_NUMBER,
                                   REVIEW_TYPE,
                                   UNIT_TYPE,
                                   REGION_CODE,
                                   DAYS)
                                VALUES
                                  (P_ASSESSMENTID,
                                   V_NEWLEVELID,
                                   p_companyCode,
                                   USER_ON_HOLIDAY.user_code,
                                   'N',--This should be value of enum CommonActionEnum.NORMAL_APPROVAL
                                   sysdate,
                                   fn_get_rule_date(sysdate),
                                   p_camNumber,
                                   p_reviewType,
                                   p_unitType,
                                   p_regionCode,
                                   0);
                              EXCEPTION
                                WHEN OTHERS THEN
                                  ROLLBACK;
                                  v_errmsg := 'Unable to insert into mst_tat_calc as normal approver';
                                  RAISE;
                              END;
      -------------------------------------------------------------------------------------------------
                              -- Check for if user has any courier
                              BEGIN
                                -- update in aph_user_Courier as courier status sent and sent date
                                SELECT COUNT(1)
                                  INTO v_courierCount
                                FROM aph_user_courier
                                WHERE assessmentid = p_assessmentid
                                  AND touser         = USER_ON_HOLIDAY.User_code
                                  AND levelid        = p_prevlevelid;

                                if v_courierCount = 0 then
                                 begin
                                   Pack_Aph_Getid.Proc_Aph_Getid('APH_USER_COURIER','COURIER_ID', v_NewCourierId); -- generate courier id.
                                 end;

                                 INSERT
                                  INTO APH_USER_COURIER
                                    (
                                      COURIER_ID,
                                      ASSESSMENTID,
                                      LEVELID,
                                      FROMUSER,
                                      TOUSER,
                                      FROMUSER_COMMENTS,
                                      TOUSER_COMMENTS,
                                      FROMCOURIER_STATUS,
                                      TOCOURIER_STATUS,
                                      FROM_DATETIME,
                                      TO_DATETIME,
                                      DOCUMENTRECEIVED,
                                      RECEIVEDDATE
                                    )
                                    --values (v_NewCourierId, p_assessmentid, V_Newlevelid, p_usercode, Send.User_Id, null, null, 'SENT' , null , sysdate , null, null, null);
                                    VALUES
                                    (
                                      v_NewCourierId,
                                      p_assessmentid,
                                      V_Newlevelid,
                                      p_usercode,
                                      USER_ON_HOLIDAY.User_code,
                                      v_courierComments,
                                      NULL,
                                      'SENT' ,
                                      NULL ,
                                      sysdate ,
                                      NULL,
                                      NULL,
                                      NULL
                                    );
                                else
                                  UPDATE aph_user_Courier
                                     SET fromcourier_status = 'SENT',
                                          tocourier_status = null,
                                         FROM_DATETIME      = SYSDATE
                                   WHERE assessmentid = p_assessmentid
                                     AND levelid = p_prevlevelid
                                     --AND touser = Send.User_Id;
                                     AND touser = USER_ON_HOLIDAY.User_code
                                end if;

                              EXCEPTION
                                WHEN OTHERS THEN
                                  ROLLBACK;
                                  v_errmsg := 'Unable to update aph_user_Courier';
                                  RAISE;
                              END;    

                 BEGIN
                                 SP_COMMON_INSERTS(p_assessmentId, p_prevLevelId, V_Newlevelid, 'N', 'N',0,'0','N', p_errorDetail)
                               EXCEPTION
                                WHEN OTHERS THEN
                                  p_Errordetail := 'Error in common';
                  ROLLBACK;
                              END;

                              BEGIN
                                UPDATE APH_WF_USER set IS_EDITABLE = 'N' where USER_ID = Send.User_Id and ASSESSMENTID = p_assessmentId;
                              END;
                          END;
  Exception
         When Others Then
           Rollback;
             p_Errordetail :=v_errmsg;
  END SP_SEND_CASE_NEXT_LEVEL;








  Procedure Sp_Common_Inserts
  (
    --p_wfId In Number,
    p_assessmentId In Number,
    p_prevLevelId In Number,
    p_Newlevelid in number,
    p_withdraw in varchar2,
    p_rejected in varchar2,
    p_prevAssessmentId in Number,
    p_borrowerCode in varchar2,
    p_srm_lcr in varchar2,
    p_errorDetail Out Varchar2
  ) Is

  v_wfId Number(8);
  V_Errmsg Varchar2(50);
  v_initiatorUserCode varchar2(50);
  V_SrNo Number(8);

  Begin
       -- Savepoint Start_Tran1;

        if p_withdraw = 'N' and p_rejected = 'N' then
        begin
        -- APH_ASSESSMENT_PARAMETER
        INSERT INTO APH_ASSESSMENT_PARAMETER
        (
        SR_NO,
        ASSESSMENTID,
        LEVELID,
        CAM_DATE,
        REVISION_DATE,
        SRM_EXPIRY_DATE,
        PART_PROGRAM,
        PRODUCT_PROGRAM,
        PRODUCT_PROGRM_EXPOSURE,
        PROGRM_PARAMETERS_ADHERED,
        IS_SEPARETE_APPROVER,
        CASH_BACKED_EXPOSURE,
        EXPOSURE_GURANTED,
        CLIMS_ID,
        CAM_LOGIN_DATE,
        BORROWER_RATING,
        RATING_TYPE,
        RATING_FINALISATION_DATE,
        INITIAL_LABELLING_DATE,
        CURRENT_REVIEW_DATE,
        LABEL_REVIEW_TYPE,
        LABEL,
        LAST_CLIENT_CALL_DATE,
        CALLED_ON_DATE,
        INTERFACE_DATE,
        COMP_CODE
        ) SELECT
        SR_NO,
        p_assessmentId,
        p_Newlevelid,
        CAM_DATE,
        REVISION_DATE,
        SRM_EXPIRY_DATE,
        PART_PROGRAM,
        PRODUCT_PROGRAM,
        PRODUCT_PROGRM_EXPOSURE,
        PROGRM_PARAMETERS_ADHERED,
        IS_SEPARETE_APPROVER,
        CASH_BACKED_EXPOSURE,
        EXPOSURE_GURANTED,
        CLIMS_ID,
        CAM_LOGIN_DATE,
        BORROWER_RATING,
        RATING_TYPE,
        RATING_FINALISATION_DATE,
        INITIAL_LABELLING_DATE,
        CURRENT_REVIEW_DATE,
        LABEL_REVIEW_TYPE,
        LABEL,
        LAST_CLIENT_CALL_DATE,
        CALLED_ON_DATE,
        INTERFACE_DATE,
        COMP_CODE
        From APH_ASSESSMENT_PARAMETER
        Where Levelid = p_prevlevelid;
        Exception 
        When No_Data_Found Then
        rollback;
        v_errmsg := 'Unable to insert into APH_ADDENDUM_DETAILS as no data found at previous level';
        RAISE;
        end;

        end if;

        IF P_WITHDRAW = 'Y' or P_REJECTED = 'Y' THEN
        --APH_APPRAISAL_DETAILS
        begin
        INSERT INTO APH_APPRAISAL_DETAILS
        (
        ASSESSMENTID,
        COMPANY_CODE,
        CAM_FORMAT_ID,
        FILE_NAME,
        ORDER_ID,
        REPORTS_HTML_CONTENTS,
        SR_NO
        )SELECT
        p_assessmentId,
        COMPANY_CODE,
        CAM_FORMAT_ID,
        FILE_NAME,
        ORDER_ID,
        REPORTS_HTML_CONTENTS,
        SR_NO
        FROM APH_APPRAISAL_DETAILS
        WHERE ASSESSMENTID = P_prevAssessmentId
        AND FILE_NAME NOT IN (SELECT FILE_NAME FROM APH_FTL_DETAILS WHERE FTL_ID IN(29,30,31));
        Exception 
        When No_Data_Found Then
        rollback;
        v_errmsg := 'Unable to insert into APH_BORROWER_DETAILS as no data found at previous level';
        RAISE;
        End;
        END IF;

    p_Errordetail:='SUCCESS';

       Exception
         When Others Then
           Rollback;
             p_Errordetail :=v_errmsg;

  End Sp_Common_Inserts;