Chapter 7: Contract Condition Management
Now that we've mastered the ALV Grid Display Framework for presenting data beautifully, let's explore the Contract Condition Management system - the digital contract administrator that handles all the business rules and fine print of your rental agreements!
What Problem Does This Solve?
Imagine you're managing 100 rental contracts, and each one has specific conditions like:
- Special pricing rules (10% discount for long-term tenants)
- Service charge rates (different rates for residential vs. commercial)
- Payment terms (monthly vs. quarterly billing)
- Adjustment conditions (annual rent increases based on inflation)
Without proper management, you might end up with:
- Outdated conditions still being applied to billing
- Inconsistent rates across similar properties
- Manual errors when updating contract terms
- Billing disputes due to incorrect conditions
The Contract Condition Management system acts like a meticulous contract administrator who keeps track of every detail, ensures changes are applied correctly, and maintains perfect synchronization with your billing system.
A Real-World Example
Let's say you need to remove outdated service charge conditions from contracts that were created in 2024 but are no longer valid:
Input: 50 contracts with old S001, S002, S003 condition types from 2024 Processing: System finds these conditions, validates they haven't been used in billing, and safely deletes them Output: Clean contracts with only current, valid conditions, plus a detailed log showing what was deleted
It's like spring cleaning for your contracts - removing the clutter while keeping everything organized!
Key Components of Contract Condition Management
The system consists of four main parts:
1. Condition Selection and Filtering
This finds the contract conditions you want to manage:
SELECT * FROM v_vicdcond_cn_ds
INTO TABLE lt_cond
WHERE bukrs = p_bukrs
AND recnnr IN s_contr
AND condtype IN s_cond.
This code searches for contract conditions based on your criteria - company code, contract numbers, and condition types. Think of it as filtering through filing cabinets to find specific contract clauses.
2. Safety Validation Checks
This ensures conditions can be safely modified or deleted:
SELECT DISTINCT condtype condguid
FROM zv_viradoccfcn
INTO TABLE lt_posted
WHERE condtype = lt_cond-condtype.
This code checks if conditions have been used in billing - you can't delete conditions that are already part of posted invoices. It's like checking if a contract clause is still being referenced before removing it.
3. BAPI-Based Updates
This uses SAP's standard interfaces to make changes:
CALL FUNCTION 'BAPI_RE_CN_CHANGE'
EXPORTING compcode = lv_bukrs
contractnumber = lv_recnnr
TABLES condition = lt_bapi_cond
return = lt_return.
This code updates contract conditions through official SAP channels, ensuring all business rules and validations are properly applied.
4. Results Logging and Display
This shows you exactly what happened:
MOVE-CORRESPONDING <line> TO ls_log.
ls_log-type = 'S'.
ls_log-message = 'Successfully Deleted'.
APPEND ls_log TO lt_log.
This code creates a detailed audit trail of every change made, so you have complete visibility into what was processed.
How to Use Contract Condition Management
Here's how you would clean up old contract conditions step by step:
Step 1: Specify Your Selection Criteria
PARAMETERS: p_bukrs TYPE vicncn-bukrs.
SELECT-OPTIONS: s_contr FOR vicncn-recnnr,
s_cond FOR v_viradoccfcn_ds-condtype.
You tell the system which company and contracts to work with, and which condition types you want to manage.
Step 2: Choose Test or Update Mode
PARAMETERS: p_test AS CHECKBOX DEFAULT abap_true.
Test mode shows you what would happen without making real changes. Update mode actually performs the modifications. Always test first!
Step 3: Let the System Process
The system automatically:
- Finds matching conditions
- Validates they can be safely changed
- Makes the updates through proper channels
- Creates a detailed log of results
Step 4: Review the Results
The system displays a comprehensive report using the ALV Grid Display Framework showing exactly what was processed and the outcome of each operation.
What Happens Under the Hood?
Let's trace through what happens when you delete old contract conditions:
Here's what happens step by step:
Step 1: Condition Discovery
SELECT * FROM v_vicdcond_cn_ds INTO TABLE lt_cond
WHERE bukrs = p_bukrs
AND condtype IN s_cond
AND conddelete = ''.
The system finds all active conditions that match your criteria. It automatically excludes conditions that are already marked for deletion.
Step 2: Usage Validation
SELECT DISTINCT condtype condguid FROM zv_viradoccfcn
INTO TABLE lt_posted
WHERE condtype = lt_cond-condtype.
The system checks if conditions have been used in billing. Any conditions that appear in posted invoices are automatically excluded from deletion to maintain data integrity.
Step 3: Contract Processing
LOOP AT lt_cond GROUP BY ( key = <ls_cond>-recnnr ).
CALL FUNCTION 'BAPI_RE_CN_GET_DETAIL'
EXPORTING compcode = <ls_cond>-bukrs
contractnumber = <ls_cond>-recnnr.
ENDLOOP.
For each contract, the system retrieves current condition details to ensure it has the latest information before making changes.
Step 4: Safe Deletion Processing
ls_bapi_cond-change_indicator = 'D'. " Delete
APPEND ls_bapi_cond TO lt_bapi_cond.
The system marks conditions for deletion using SAP's standard change indicators, ensuring proper workflow and validation.
Step 5: Transactional Updates
CALL FUNCTION 'BAPI_RE_CN_CHANGE'
TABLES condition = lt_bapi_cond
return = lt_return.
IF sy-subrc = 0 AND p_test IS INITIAL.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
ENDIF.
The system processes all changes in a transaction - either everything succeeds, or nothing changes. This prevents partial updates that could leave contracts in an inconsistent state.
The System's Smart Safety Features
1. Automatic Safety Checks
DELETE lt_cond WHERE condtype = ls_posted-condtype
AND condguid = ls_posted-condguid.
The system automatically excludes dangerous deletions - any condition that's been used in billing is protected from accidental removal.
2. Test Mode Protection
IF p_test IS NOT INITIAL.
lo_display->set_list_header( |Log of deleted entries(Test mode)| ).
ELSE.
lo_display->set_list_header( |Log of deleted entries| ).
ENDIF.
The system clearly shows when you're in test mode, preventing confusion about whether changes are real or simulated.
3. Comprehensive Error Handling
LOOP AT lt_return INTO DATA(ls_ret) WHERE type CA 'AXE'.
ENDLOOP.
IF sy-subrc <> 0 AND p_test IS INITIAL.
" Only commit if no errors found
ENDIF.
The system checks for any errors before committing changes. If anything goes wrong, the entire transaction is rolled back.
4. Detailed Audit Trail
MOVE-CORRESPONDING <line> TO ls_log.
IF ls_ret-type <> 'E'.
<fs_log>-type = 'S'.
<fs_log>-message = 'Successfully Deleted'.
ELSE.
<fs_log>-type = ls_ret-type.
<fs_log>-message = ls_ret-message.
ENDIF.
Every operation is logged with detailed status information, creating a complete audit trail for compliance and troubleshooting.
Real-World Management Example
Let's walk through cleaning up old service charge conditions:
Scenario: Remove outdated S001-S003 conditions from 2024 contracts
Step 1: User selects company code and condition types S001-S003 Step 2: System finds 25 contracts with these conditions Step 3: System checks billing usage - finds 5 were used in invoices Step 4: System safely deletes 20 unused conditions from remaining contracts Step 5: User sees detailed report:
- 20 conditions successfully deleted
- 5 conditions skipped (already used in billing)
- Full audit trail with contract numbers and timestamps
Result: Clean contracts with only current conditions, complete transparency of what changed
Advanced Management Features
1. Duplicate Prevention
SORT lt_cond BY condtype condguid.
DELETE ADJACENT DUPLICATES FROM lt_cond COMPARING condtype condguid.
The system automatically removes duplicates from processing lists, ensuring each condition is only processed once.
2. Batch Processing by Contract
LOOP AT lt_cond GROUP BY ( key = <ls_cond>-recnnr ).
" Process all conditions for one contract together
ENDLOOP.
The system groups conditions by contract for efficient processing, reducing database calls and improving performance.
3. Flexible Date Handling
CALL FUNCTION 'HR_PSF_GET_BEGIN_END_OF_YEAR'
EXPORTING imp_begda = sy-datum
IMPORTING exp_begda = lv_begda
exp_endda = lv_endda.
The system automatically sets up date ranges for current year processing, making it easy to work with time-based conditions.
4. Integration with Other Components
The Contract Condition Management works closely with:
- Service Charge Calculation Engine: Ensures rate changes are applied to calculations
- Invoice Creation and Posting System: Validates conditions before invoicing
- Data Selection and Validation Engine: Provides clean, validated contract data
Professional Results Display
The system uses the ALV Grid Display Framework to present results professionally:
TRY.
cl_salv_table=>factory(
IMPORTING r_salv_table = DATA(lo_alv)
CHANGING t_table = lt_log ).
DATA(lo_display) = lo_alv->get_display_settings( ).
lo_display->set_list_header( 'Log of deleted entries' ).
lo_display->set_striped_pattern( abap_true ).
lo_alv->display( ).
ENDTRY.
Users get a professional, sortable, filterable report showing exactly what was processed, with clear status indicators and detailed messages.
Conclusion
The Contract Condition Management system serves as your digital contract administrator, ensuring that all business rules and contract terms are properly maintained, validated, and synchronized across your rental property system. Like a skilled legal assistant who keeps track of every contract detail, it prevents errors, maintains consistency, and provides complete audit trails for all changes.
Key benefits:
- Safe, validated updates prevent data corruption and billing errors
- Comprehensive audit trails provide complete visibility into all changes
- Automatic safety checks protect against accidental deletion of active conditions
- Test mode capabilities let you verify changes before applying them
- Professional reporting shows exactly what was processed and why
The system ensures that your contract conditions remain accurate, current, and properly integrated with your billing processes, giving you confidence that every charge is based on the correct contract terms.
In our next chapter, we'll explore the Background Job Processing Framework to understand how the system handles large-scale operations efficiently without impacting your daily work.