# SIM Inventory CSV Import - Screen Flow Setup

This guide provides the Apex class and Screen Flow configuration to enable employees to import Physical SIM data via CSV file upload.

**Simplified for Physical SIM imports only - no header row expected.**

---

## Overview

The solution consists of:

1. **Apex Invocable Class** - Parses CSV and creates SIM_Inventory\_\_c records
2. **Screen Flow** - Simple UI with just file upload and results display

---

## Step 1: Deploy the Apex Classes

Copy the Apex classes from:

- `docs/integrations/salesforce/apex/SIMInventoryImporter.cls`
- `docs/integrations/salesforce/apex/SIMInventoryImporterTest.cls`

### Deploy Steps:

1. Go to **Setup → Apex Classes → New**
2. Paste the content of `SIMInventoryImporter.cls` → Save
3. Create another class, paste `SIMInventoryImporterTest.cls` → Save
4. Run tests to verify (Setup → Apex Test Execution)

---

## Step 2: Create the Screen Flow

### Flow Configuration

1. Go to **Setup → Flows → New Flow**
2. Select **Screen Flow**
3. Click **Create**

### Flow Elements

#### Element 1: Screen - File Upload

**Screen Properties:**

- Label: `Upload Physical SIM CSV`
- API Name: `Upload_SIM_CSV`

**Components on Screen:**

1. **Display Text** (Header)
   - API Name: `Header_Text`
   - Content:

   ```
   # Import Physical SIM Inventory

   Upload a CSV file containing Physical SIM data.

   **Expected format (no header row):**
   `Row,Phone_Number,PT_Number,OEM_ID,Batch_Date,,,,,`

   **Example:**
   `1,02000002470001,PT0220024700010,PASI,20251229,,,,`
   ```

2. **File Upload**
   - API Name: `CSV_File_Upload`
   - Label: `Select CSV File`
   - Allow Multiple Files: `No`
   - Accept: `.csv`
   - Required: `Yes`

#### Element 2: Action - Call Apex Importer

**Action Properties:**

- Category: `Apex`
- Action: `Import SIM Inventory from CSV`

**Input Values:**

- `contentDocumentId` → `{!CSV_File_Upload}` (the file upload component returns the ContentDocumentId)

**Store Output:**

- Create variables to store the output:
  - `ImportResult_Success` (Boolean)
  - `ImportResult_RecordsCreated` (Number)
  - `ImportResult_RecordsFailed` (Number)
  - `ImportResult_ErrorMessages` (Text)
  - `ImportResult_SummaryMessage` (Text)

#### Element 3: Screen - Results

**Screen Properties:**

- Label: `Import Results`
- API Name: `Import_Results`

**Components:**

1. **Display Text** (Success Message)
   - API Name: `Success_Message`
   - Visibility: Show when `{!ImportResult_Success} Equals true`
   - Content:

   ```
   ✅ **Import Successful**

   **Records Created:** {!ImportResult_RecordsCreated}

   {!ImportResult_SummaryMessage}
   ```

2. **Display Text** (Error Details)
   - API Name: `Error_Details`
   - Visibility: Show when `{!ImportResult_RecordsFailed} Greater than 0`
   - Content:

   ```
   ⚠️ **Some records had issues:**

   {!ImportResult_ErrorMessages}
   ```

3. **Display Text** (Failure Message)
   - API Name: `Failure_Message`
   - Visibility: Show when `{!ImportResult_Success} Equals false`
   - Content:

   ```
   ❌ **Import Failed**

   {!ImportResult_ErrorMessages}
   ```

---

## Step 3: Flow Diagram (Simplified)

```
┌─────────────────────────┐
│   Start                 │
└───────────┬─────────────┘
            │
            ▼
┌─────────────────────────┐
│  Screen: Upload CSV     │
│  - File Upload only     │
└───────────┬─────────────┘
            │
            ▼
┌─────────────────────────┐
│  Action: Import SIM     │
│  Inventory from CSV     │
│  (Apex Invocable)       │
└───────────┬─────────────┘
            │
            ▼
┌─────────────────────────┐
│  Screen: Import Results │
│  - Success/Fail Message │
│  - Records Created      │
│  - Error Details        │
└───────────┬─────────────┘
            │
            ▼
┌─────────────────────────┐
│   End                   │
└─────────────────────────┘
```

---

## Step 4: Add Flow to Lightning App

1. Go to **Setup → App Manager**
2. Edit your app (e.g., "Sales" or custom app)
3. Add the Flow to utility items or create a Tab
4. Alternatively, embed in a Lightning Page:
   - Edit any Lightning Record Page
   - Add "Flow" component
   - Select your "Import SIM Inventory" flow

---

## Alternative: Quick Action Button

Create a Quick Action to launch the flow from the SIM Inventory list view:

1. **Setup → Object Manager → SIM Inventory → Buttons, Links, and Actions**
2. Click **New Action**
3. Action Type: `Flow`
4. Flow: Select your import flow
5. Label: `Import SIMs from CSV`
6. Add to Page Layout

---

## CSV File Format Reference

Your CSV files should follow this format:

| Column | Field        | Example         | Required     |
| ------ | ------------ | --------------- | ------------ |
| 1      | Row Number   | 1               | No (ignored) |
| 2      | Phone Number | 02000002470001  | Yes          |
| 3      | PT Number    | PT0220024700010 | No           |
| 4      | OEM ID       | PASI            | No           |
| 5      | Batch Date   | 20251229        | No           |
| 6-9    | Empty        |                 | No           |

**Example CSV:**

```csv
1,02000002470001,PT0220024700010,PASI,20251229,,,,
2,02000002470002,PT0220024700020,PASI,20251229,,,,
3,02000002470003,PT0220024700030,PASI,20251229,,,,
```

---

## Troubleshooting

### Common Issues

1. **"Not enough columns" error**
   - Ensure CSV has at least 5 columns (even if some are empty)
   - Check for proper comma separators

2. **"Phone number already exists" error**
   - The phone number is already in SIM_Inventory\_\_c
   - Check existing records before importing

3. **File upload not working**
   - Ensure file is .csv format
   - Check file size (Salesforce limit: 25MB for files)

4. **Permission errors**
   - User needs Create permission on SIM_Inventory\_\_c
   - User needs access to the Flow

---

## Security Considerations

- The Apex class uses `with sharing` to respect record-level security
- Only users with appropriate permissions can run the Flow
- Consider adding a Permission Set for SIM Inventory management

---

**Last Updated:** January 2025
