# DeleteAdHocFieldDefinitionClaimType - Advanced Documentation ## Overview Deletes a claim type ad hoc field definition and all associated field values, form widgets, and intake designer references. Automatically regenerates claim forms for all claim types that used this field. This is a long-running operation that reports progress through a ProcessID status channel. ## Business Rules - The ad hoc field definition must belong to the current company (verified via `vwAdHocFieldDefinitionsClaimTypes`) - All claim types that reference this field are identified upfront and their forms are regenerated after deletion - Field values are deleted in batches (looped stored procedure calls) to avoid SQL timeouts on large datasets - Widget field references and intake designer field references are also deleted in batches - The Aptify `AdHocFieldDefinitionsClaimTypes` entity record is hard-deleted - Sort orders for remaining ad hoc fields are recalculated after deletion (`spSaveAdHocSortOrderClaimType`) - Generated claim forms are regenerated for every claim type that used the deleted field - Intake designer cache is cleared after the operation completes - Ad hoc field cache for the company is cleared in the `finally` block (always runs) ## Permissions & Security - Caller must be logged in (`curPerson.ID >= 1`) - Caller must have a company selected (`curCompany.ID >= 1`) - Caller must be a member of the current company (`IsPartOfCompany` check) - Caller must have the **ManageFirm** permission - The field definition must belong to the current company (cross-company access is blocked) ## Data Flow 1. Validate authentication, company membership, and ManageFirm permission 2. Set process status to "Validating Security Access" 3. Verify the ad hoc field definition belongs to the current company via `vwAdHocFieldDefinitionsClaimTypes` 4. Execute `spGetClaimTypesThatUseAdHocField` to identify all claim types referencing this field 5. Loop `spDeleteAdHocValuesClaimType` until no more field values remain (batched, no transaction) 6. Begin transaction 7. Loop `spDeleteAdHocClaimTypeMenuWidgetFields` to remove form widget references (batched) 8. Loop `spDeleteAdHocCompanyClaimTypeIntakeWidgetFields` to remove intake designer references (batched) 9. Delete the `AdHocFieldDefinitionsClaimTypes` entity record via Aptify 10. Execute `spSaveAdHocSortOrderClaimType` to recalculate sort orders 11. For each affected claim type, call `ClaimDesigner.UpdateClaimTypeGeneratedForms` to regenerate forms 12. Commit transaction 13. Clear intake setup cache 14. Clear ad hoc field cache (in `finally` block) 15. Set final process status to "This field has been deleted" ## Side Effects - **Database deletes**: All ad hoc field values for claims using this definition are permanently deleted - **Database deletes**: All form widget field references and intake designer field references are removed - **Database delete**: The ad hoc field definition record itself is removed - **Database update**: Sort orders are recalculated for remaining fields - **Database updates**: Claim type generated forms (HTML) are regenerated for all affected claim types - **Cache invalidation**: Intake setup cache is cleared via `ClearIntakeSetupCache()` - **Cache invalidation**: Ad hoc field cache for claim type fields is cleared via `ClearAdHocCache(AdHocFieldType.Claim, companyID)` ## Error Conditions - `RadoloException("You must be logged in to perform this action.")` - caller not authenticated - `RadoloException("You must have a company selected to perform this action.")` - no company context - `RadoloException("You can only perform this action for a company you are a member of.")` - cross-company attempt - `RadoloException("You do not have permission to delete {claimLabel} type adhocs...")` - missing ManageFirm permission - `RadoloException("The field you are trying to delete is not part of the company you are logged into [{ID}].")` - field belongs to a different company - `RadoloException("There was a problem deleting this ad hoc field : {error}")` - Aptify entity deletion failed ## Usage Notes - This is a long-running operation wrapped in `ProcessStatus.Run`. Poll the ProcessID for progress updates. - Field values are deleted outside the transaction (intentionally) to avoid timeouts on large datasets. The transaction only covers the structural changes (widget references, intake references, definition record, sort order, form regeneration). - If the process fails partway through, field values may already be deleted while the definition still exists. The field would be non-functional in this state. - The `finally` block always clears the ad hoc cache regardless of success or failure, and sets the process status message. - Form regeneration can be slow if many claim types reference the field.