# Custom Field Creator

The Custom Field Creator is a feature in the Customs Editor tab that allows you to create Filevine custom fields directly from the browser extension sidebar.

## Overview

Instead of navigating through Filevine's UI to create custom fields, you can now use the extension's sidebar to quickly create fields programmatically. This is particularly useful for:

- Bulk field creation
- Automated field setup
- Testing custom field configurations
- Rapid prototyping of custom sections

## Location

The Custom Field Creator is located in the **Customs Editor** tab of the extension sidebar:

1. Open the extension sidebar (click the extension icon or use the sidebar panel)
2. Click the "Customs Editor" tab at the top
3. Fill out the Custom Field Creator form

## Form Fields

### Required Fields

- **Organization ID**: Your Filevine organization ID (found in your Filevine URL)
- **Custom Section ID**: The ID of the custom section where the field will be created
- **Field Name**: Display name for the field (e.g., "Test field prompt")
- **Field Selector**: Internal identifier for the field (e.g., "fieldselector")
- **Field Type**: The type of field to create (see Field Types below)
- **Row Number**: Position in the form layout (minimum: 1)

### Optional Fields

- **Required Field**: Checkbox to mark the field as required
- **Notes**: Optional notes about the field

## Field Types

The form supports all 36 Filevine field types:

### Basic Fields
- **Text - Single Line** (3) - Default
- **Text - Multiple Lines** (6)
- **Text - Multiple Lines(large)** (17)
- **Text - Single Line List** (24)

### Numeric Fields
- **Amount - Number** (1)
- **Amount - Currency like $0.00** (2)
- **Amount - Decimal like 0.00** (13)
- **Amount - Percent like 0.00%** (14)

### Date Fields
- **Date** (4)
- **Deadline (with reminders)** (15)
- **Incident Date** (18)

### Selection Fields
- **Yes/No** (5)
- **Dropdown List** (9)
- **Multi-Select List** (26)

### Reference Fields
- **Person** (7)
- **Person List** (25)
- **Project Link** (27)
- **Project Link List** (28)
- **Reference Field** (150)

### File Fields
- **File Attachment** (8)
- **Multiple File Attachments (List)** (11)

### URL Fields
- **Url** (22)
- **Url List** (23)

### Calculated Fields
- **Calculated Number** (101)
- **Calculated Currency** (102)
- **Calculated Decimal** (113)
- **Calculated Percentage** (114)

### Document Generation
- **Doc Generation** (12)
- **Doc Generation Multi-template** (29)
- **Report Fusion** (1009)
- **Report Fusion Multi-template** (1010)
- **Outlaw Template** (1100)

### Layout/UI Fields
- **Sub Section Header** (10)
- **Action Button** (20)
- **Instructions** (21)
- **Case Number** (19)

## Usage Example

1. **Get your Organization ID and Section ID**:
   - Navigate to your custom section in Filevine
   - Look at the URL: `https://product.filevineapp.com/org/990000459/section/990064683`
   - Organization ID: `990000459`
   - Section ID: `990064683`

2. **Fill out the form**:
   ```
   Organization ID: 990000459
   Custom Section ID: 990064683
   Field Name: Patient Name
   Field Selector: patientname
   Field Type: Text - Single Line
   Row Number: 1
   Required Field: ☑
   Notes: Primary patient identifier
   ```

3. **Click "Create Field"**

4. **View the result**:
   - Success message will appear with the field ID
   - Full API response is displayed in JSON format
   - Form resets automatically on success

## API Response

On successful creation, you'll receive:

```json
{
  "success": true,
  "data": {
    "id": 991972210,
    "customSectionID": 990064683,
    "name": "Patient Name",
    "internalName": "patientname990064683",
    "fieldSelector": "patientname",
    "customFieldType": 3,
    "row": 1,
    "isRequired": true,
    "notes": "Primary patient identifier"
  }
}
```

## Implementation Details

### Architecture

The Custom Field Creator uses a message-passing architecture:

1. **Frontend (sidebar.html)**: Form UI for collecting field data
2. **Sidebar Controller (sidebar.js)**: Handles form submission and displays results
3. **Background Script (index.js)**: Routes the request to the web API client
4. **Web API Client (web-api-client.js)**: Makes the actual API call to Filevine

### API Endpoint

The feature calls Filevine's custom field creation endpoint:

```
POST /api/advanced/orgs/{orgId}/customSections/{sectionId}/customFields/create
```

### Authentication

The feature uses cookie-based authentication (same as the Filevine web app). You must be logged into Filevine for this to work.

## Files Modified

### New Files
- `docs/CUSTOM_FIELD_CREATOR.md` - This documentation

### Modified Files
- `src/sidebar/sidebar.html` - Added Customs Editor tab and form UI
- `src/sidebar/sidebar.css` - Added styles for tabs and form
- `src/sidebar/sidebar.js` - Added form handler and tab switching
- `src/background/web-api-client.js` - Added `createCustomField()` method
- `src/background/index.js` - Added `create_custom_field` message handler

## Error Handling

The form includes comprehensive error handling:

- **Validation errors**: Required fields must be filled
- **Authentication errors**: Must be logged into Filevine
- **API errors**: Displayed with full error message
- **Network errors**: Caught and displayed to user

Errors are shown in the result panel with a red error indicator and the error message.

## Future Enhancements

Potential improvements for future versions:

1. **Section Browser**: Auto-populate section IDs by browsing available sections
2. **Field Templates**: Pre-configured field templates for common use cases
3. **Bulk Creation**: Create multiple fields at once from CSV/JSON
4. **Field Editor**: Edit existing fields
5. **Field Duplication**: Duplicate existing fields with modifications
6. **Validation Rules**: Add custom validation rules during creation
7. **Conditional Logic**: Set up conditional field visibility during creation

## Troubleshooting

### "Not authenticated" error
- Make sure you're logged into Filevine in the same browser
- Try the "Check Connection" button in the Documents tab
- Clear cache and re-authenticate

### "Failed to create custom field" error
- Verify your Organization ID and Section ID are correct
- Check that you have permission to create fields in that section
- Ensure the field selector doesn't conflict with existing fields

### No response/timeout
- Check your internet connection
- Verify Filevine is accessible
- Try refreshing the extension

## Related Documentation

- [Filevine API Documentation](https://support.filevine.io/en/collections/4135869-api-overview)
- [Chrome Extension Architecture](../README.md)
- [Web API Client](../src/background/web-api-client.js)
