25.3.4 Starting the Lifecycle Workflow

Start a procedure lifecycle workflow when onboarding creates the patient procedure row.

The final step of the patient_onboarding workflow uses an Invoke API activity to call the COMPLETED_REGISTRATION procedure below in the FRC_ONBOARDING package. It inserts a row in FRC_PATIENT_PROCEDURES to track the patient's medical procedure to perform at the clinic. Just after that, it calls the START_WORKFLOW_FOR helper procedure to create the procedure_lifecycle workflow instance related to the newly-assigned patient procedure ID.

-- in frc_onboarding package
procedure completed_registration(p_patient_id number)  
is  
    l_patient t_patient := patient(p_patient_id);   
    l_new_procedure_id number;
begin  
    -- insert initial procedure  
    for j in (select l_patient.id as patient_id,  
                     code,  
                     'AwaitingSchedule' as status,  
                     price,  
                     duration_hours  
                from frc_medical_procedures  
               where code = l_patient.initial_procedure)
    loop
        insert into frc_patient_procedures(  
            patient_id,  
            medical_procedure,  
            status,  
            amount_due,  
            duration_hours)  
        values (
            j.patient_id,
            j.code,
            j.status,
            j.price,
            j.duration_hours)
        return id into l_new_procedure_id;
        start_workflow_for(l_new_procedure_id);
    end loop;
end;

The START_WORKFLOW_FOR helper method starts a workflow with Static ID procedure_lifecycle, passing in the medical procedure ID for the Details Primary Key value. It calls the START_WORKFLOW function in the APEX_WORKFLOW package.

-- internal in package frc_procedure_lifecycle
------------------------------------------------------------------
-- Start a procedure_lifecycle workflow instance associated with
-- the supplied patient procedure id
------------------------------------------------------------------  
procedure start_workflow_for(
    p_procedure_id in number)
is
    l_workflow_id number;
begin  
   l_workflow_id := apex_workflow.start_workflow(
                        p_application_id => frc_app.clinic_app_id,
                        p_static_id      => 'procedure_lifecycle',
                        p_detail_pk      => p_procedure_id,
                        p_initiator      => 'ADMIN');
end start_workflow_for;

Tip:

When you don't plan to use a function's result, to avoid a PL/SQL compilation error, assign it to a local variable that you then ignore. Failing to do this in the above example would yield the error:
PLS-00221: 'START_WORKFLOW' is not a procedure or is undefined