345 lines
9.5 KiB
PowerShell
345 lines
9.5 KiB
PowerShell
#!/usr/bin/env pwsh
|
|
|
|
<#
|
|
.SYNOPSIS
|
|
Data import wrapper for Salesforce CLI with CSV and JSON support
|
|
|
|
.DESCRIPTION
|
|
A user-friendly wrapper around 'sf data import' that simplifies data import
|
|
to Salesforce orgs with CSV/JSON support, upsert operations, and intelligent validation.
|
|
|
|
.PARAMETER fl
|
|
CSV or JSON file to import
|
|
|
|
.PARAMETER so
|
|
Target sObject type
|
|
|
|
.PARAMETER op
|
|
Operation: insert, update, upsert (default: insert)
|
|
|
|
.PARAMETER ei
|
|
External ID field for upsert/update operations
|
|
|
|
.PARAMETER to
|
|
Target org username or alias
|
|
|
|
.PARAMETER bk
|
|
Use bulk API for large datasets
|
|
|
|
.PARAMETER wt
|
|
Wait time in minutes (default: 10)
|
|
|
|
.PARAMETER bs
|
|
Batch size for bulk operations (default: 10000)
|
|
|
|
.PARAMETER ie
|
|
Continue on errors (don't fail entire job)
|
|
|
|
.PARAMETER ve
|
|
Enable verbose output
|
|
|
|
.PARAMETER hp
|
|
Show this help message
|
|
|
|
.EXAMPLE
|
|
.\sf-data-import.ps1 -fl accounts.csv -so Account
|
|
.\sf-data-import.ps1 -fl contacts.json -so Contact -op upsert -ei Email
|
|
.\sf-data-import.ps1 -fl leads.csv -so Lead -bk -bs 5000
|
|
.\sf-data-import.ps1 -fl updates.csv -so Account -op update -ei AccountNumber
|
|
|
|
.NOTES
|
|
This script automatically checks for Salesforce CLI installation and runs
|
|
diagnostics if the CLI is not found.
|
|
|
|
Supported formats:
|
|
- CSV files with header row
|
|
- JSON files (array of objects or newline-delimited JSON)
|
|
#>
|
|
|
|
param(
|
|
[string]$fl,
|
|
[string]$so,
|
|
[ValidateSet("insert", "update", "upsert")]
|
|
[string]$op = "insert",
|
|
[string]$ei,
|
|
[string]$to,
|
|
[switch]$bk,
|
|
[int]$wt = 10,
|
|
[int]$bs = 10000,
|
|
[switch]$ie,
|
|
[switch]$ve,
|
|
[switch]$hp
|
|
)
|
|
|
|
# Show help if no parameters provided or help requested
|
|
if ($hp -or (-not $fl -and -not $so)) {
|
|
Get-Help $MyInvocation.MyCommand.Path -Detailed
|
|
exit 0
|
|
}
|
|
|
|
# Function to check if Salesforce CLI is installed
|
|
function Test-SalesforceCLI {
|
|
try {
|
|
$null = Get-Command sf -ErrorAction Stop
|
|
return $true
|
|
} catch {
|
|
return $false
|
|
}
|
|
}
|
|
|
|
# Function to run sf-check diagnostics
|
|
function Invoke-SalesforceCheck {
|
|
$checkScript = if (Test-Path "sf-check.ps1") {
|
|
".\sf-check.ps1"
|
|
} elseif (Test-Path "sf-check.sh") {
|
|
"bash sf-check.sh"
|
|
} else {
|
|
$null
|
|
}
|
|
|
|
if ($checkScript) {
|
|
Write-Host "Running Salesforce CLI diagnostics..." -ForegroundColor Yellow
|
|
Invoke-Expression $checkScript
|
|
} else {
|
|
Write-Host "Salesforce CLI not found and no diagnostic script available." -ForegroundColor Red
|
|
Write-Host "Please install the Salesforce CLI: https://developer.salesforce.com/tools/salesforcecli" -ForegroundColor Red
|
|
}
|
|
}
|
|
|
|
# Function to detect file format
|
|
function Get-FileFormat {
|
|
param([string]$FilePath)
|
|
|
|
$extension = [System.IO.Path]::GetExtension($FilePath).ToLower()
|
|
|
|
switch ($extension) {
|
|
".csv" { return "csv" }
|
|
".json" { return "json" }
|
|
default {
|
|
# Try to detect by content
|
|
$firstLine = Get-Content $FilePath -First 1
|
|
if ($firstLine -match '^\s*\{.*\}$|^\s*\[') {
|
|
return "json"
|
|
} else {
|
|
return "csv"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
# Function to validate CSV file
|
|
function Test-CSVFile {
|
|
param([string]$FilePath)
|
|
|
|
$lines = Get-Content $FilePath
|
|
if ($lines.Count -lt 2) {
|
|
Write-Host "Error: CSV file must have at least a header and one data row" -ForegroundColor Red
|
|
return $false
|
|
}
|
|
|
|
$headerFields = ($lines[0] -split ',').Count
|
|
$firstRowFields = ($lines[1] -split ',').Count
|
|
|
|
if ($headerFields -ne $firstRowFields) {
|
|
Write-Host "Warning: Header field count ($headerFields) differs from first row ($firstRowFields)" -ForegroundColor Yellow
|
|
}
|
|
|
|
return $true
|
|
}
|
|
|
|
# Function to validate JSON file
|
|
function Test-JSONFile {
|
|
param([string]$FilePath)
|
|
|
|
try {
|
|
$null = Get-Content $FilePath -Raw | ConvertFrom-Json
|
|
return $true
|
|
} catch {
|
|
Write-Host "Error: Invalid JSON format in file" -ForegroundColor Red
|
|
return $false
|
|
}
|
|
}
|
|
|
|
# Function to show file preview
|
|
function Show-FilePreview {
|
|
param([string]$FilePath, [string]$Format)
|
|
|
|
Write-Host "📄 File Preview ($Format):" -ForegroundColor Yellow
|
|
Write-Host "----------------------------------------" -ForegroundColor Gray
|
|
|
|
switch ($Format) {
|
|
"csv" {
|
|
$lines = Get-Content $FilePath
|
|
Write-Host "Header: $($lines[0])" -ForegroundColor Gray
|
|
if ($lines.Count -gt 1) {
|
|
Write-Host "Sample: $($lines[1])" -ForegroundColor Gray
|
|
}
|
|
Write-Host "Records: $($lines.Count - 1)" -ForegroundColor Gray
|
|
}
|
|
"json" {
|
|
try {
|
|
$content = Get-Content $FilePath -Raw | ConvertFrom-Json
|
|
if ($content -is [array]) {
|
|
Write-Host "Array format with $($content.Count) records" -ForegroundColor Gray
|
|
if ($content.Count -gt 0) {
|
|
$keys = ($content[0] | Get-Member -MemberType NoteProperty).Name -join ", "
|
|
Write-Host "Sample keys: $keys" -ForegroundColor Gray
|
|
}
|
|
} else {
|
|
$recordCount = (Get-Content $FilePath).Count
|
|
Write-Host "NDJSON format" -ForegroundColor Gray
|
|
Write-Host "Records: $recordCount" -ForegroundColor Gray
|
|
}
|
|
} catch {
|
|
Write-Host "Unable to parse JSON preview" -ForegroundColor Gray
|
|
}
|
|
}
|
|
}
|
|
|
|
$fileInfo = Get-Item $FilePath
|
|
$fileSize = if ($fileInfo.Length -gt 1MB) {
|
|
"{0:N1} MB" -f ($fileInfo.Length / 1MB)
|
|
} elseif ($fileInfo.Length -gt 1KB) {
|
|
"{0:N1} KB" -f ($fileInfo.Length / 1KB)
|
|
} else {
|
|
"$($fileInfo.Length) bytes"
|
|
}
|
|
|
|
Write-Host "File size: $fileSize" -ForegroundColor Gray
|
|
Write-Host "----------------------------------------" -ForegroundColor Gray
|
|
}
|
|
|
|
# Silently check for Salesforce CLI
|
|
if (-not (Test-SalesforceCLI)) {
|
|
Invoke-SalesforceCheck
|
|
exit 1
|
|
}
|
|
|
|
# Validate file exists
|
|
if (-not (Test-Path $fl)) {
|
|
Write-Host "Error: File not found: $fl" -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
|
|
# Validate external ID for upsert/update operations
|
|
if (($op -eq "upsert" -or $op -eq "update") -and -not $ei) {
|
|
Write-Host "Error: External ID field is required for $op operations" -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
|
|
# Detect and validate file format
|
|
$fileFormat = Get-FileFormat $fl
|
|
Write-Host "Using file: $fl" -ForegroundColor Green
|
|
Write-Host "Detected format: $fileFormat" -ForegroundColor Cyan
|
|
|
|
# Validate file content
|
|
switch ($fileFormat) {
|
|
"csv" {
|
|
if (-not (Test-CSVFile $fl)) {
|
|
exit 1
|
|
}
|
|
}
|
|
"json" {
|
|
if (-not (Test-JSONFile $fl)) {
|
|
exit 1
|
|
}
|
|
}
|
|
}
|
|
|
|
# Show file preview if verbose
|
|
if ($ve) {
|
|
Show-FilePreview $fl $fileFormat
|
|
}
|
|
|
|
# Build the sf command based on file format
|
|
if ($fileFormat -eq "csv") {
|
|
# Use bulk import for CSV files
|
|
$sfArgs = @("data", "import", "bulk", "--file", $fl, "--sobject", $so)
|
|
} else {
|
|
# Use tree import for JSON files
|
|
$sfArgs = @("data", "import", "tree", "--files", $fl)
|
|
}
|
|
|
|
# Add optional parameters
|
|
if ($to) {
|
|
$sfArgs += "--target-org"
|
|
$sfArgs += $to
|
|
Write-Host "Target org: $to" -ForegroundColor Cyan
|
|
}
|
|
|
|
if ($ei) {
|
|
$sfArgs += "--external-id"
|
|
$sfArgs += $ei
|
|
Write-Host "External ID field: $ei" -ForegroundColor Cyan
|
|
}
|
|
|
|
if ($bk) {
|
|
$sfArgs += "--bulk"
|
|
Write-Host "Using Bulk API" -ForegroundColor Yellow
|
|
}
|
|
|
|
if ($wt -ne 10) {
|
|
$sfArgs += "--wait"
|
|
$sfArgs += $wt.ToString()
|
|
}
|
|
|
|
if ($bk -and $bs -ne 10000) {
|
|
$sfArgs += "--batch-size"
|
|
$sfArgs += $bs.ToString()
|
|
Write-Host "Batch size: $bs" -ForegroundColor Cyan
|
|
}
|
|
|
|
if ($ie) {
|
|
$sfArgs += "--ignore-errors"
|
|
Write-Host "Ignoring individual record errors" -ForegroundColor Yellow
|
|
}
|
|
|
|
# Note: sf data import commands don't support --verbose flag
|
|
# ve only affects the script's own output (file preview)
|
|
|
|
# Display import information
|
|
Write-Host ""
|
|
Write-Host "📥 Starting Data Import" -ForegroundColor Blue
|
|
Write-Host "=======================" -ForegroundColor Blue
|
|
Write-Host "Operation: $op" -ForegroundColor Cyan
|
|
Write-Host "sObject: $so" -ForegroundColor Cyan
|
|
|
|
# Display the command being run
|
|
Write-Host ""
|
|
Write-Host "Executing: sf $($sfArgs -join ' ')" -ForegroundColor Gray
|
|
Write-Host ""
|
|
|
|
# Execute the command
|
|
try {
|
|
& sf @sfArgs
|
|
$exitCode = $LASTEXITCODE
|
|
|
|
Write-Host ""
|
|
if ($exitCode -eq 0) {
|
|
Write-Host "✅ Data import completed successfully!" -ForegroundColor Green
|
|
|
|
switch ($op) {
|
|
"insert" {
|
|
Write-Host "📊 Records inserted into $so" -ForegroundColor Cyan
|
|
}
|
|
"update" {
|
|
Write-Host "📊 Records updated in $so" -ForegroundColor Cyan
|
|
}
|
|
"upsert" {
|
|
Write-Host "📊 Records upserted in $so (using $ei as external ID)" -ForegroundColor Cyan
|
|
}
|
|
}
|
|
|
|
if ($ve) {
|
|
Write-Host "💡 Check the output above for detailed results and any warnings" -ForegroundColor Yellow
|
|
}
|
|
} else {
|
|
Write-Host "❌ Data import failed with exit code: $exitCode" -ForegroundColor Red
|
|
Write-Host "💡 Check data format, field mappings, and validation rules" -ForegroundColor Yellow
|
|
exit $exitCode
|
|
}
|
|
} catch {
|
|
Write-Host "Error executing sf command: $($_.Exception.Message)" -ForegroundColor Red
|
|
exit 1
|
|
}
|