fixed ps scripts

This commit is contained in:
Reynold Lariza
2025-08-28 22:30:40 +08:00
parent 833eb9448a
commit f250f81753
6 changed files with 313 additions and 447 deletions

View File

@@ -8,38 +8,38 @@
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 File
CSV or JSON file to import (alias: -fl)
.PARAMETER fl
CSV or JSON file to import
.PARAMETER SObject
Target sObject type (alias: -so)
.PARAMETER so
Target sObject type
.PARAMETER Operation
Operation: insert, update, upsert (default: insert) (alias: -op)
.PARAMETER op
Operation: insert, update, upsert (default: insert)
.PARAMETER ExternalId
External ID field for upsert/update operations (alias: -ei)
.PARAMETER ei
External ID field for upsert/update operations
.PARAMETER TargetOrg
Target org username or alias (alias: -to)
.PARAMETER to
Target org username or alias
.PARAMETER Bulk
Use bulk API for large datasets (alias: -bk)
.PARAMETER bk
Use bulk API for large datasets
.PARAMETER Wait
Wait time in minutes (default: 10) (alias: -wt)
.PARAMETER wt
Wait time in minutes (default: 10)
.PARAMETER BatchSize
Batch size for bulk operations (default: 10000) (alias: -bs)
.PARAMETER bs
Batch size for bulk operations (default: 10000)
.PARAMETER IgnoreErrors
Continue on errors (don't fail entire job) (alias: -ie)
.PARAMETER ie
Continue on errors (don't fail entire job)
.PARAMETER VerboseOutput
Enable verbose output (alias: -ve)
.PARAMETER ve
Enable verbose output
.PARAMETER Help
Show this help message (alias: -hp)
.PARAMETER hp
Show this help message
.EXAMPLE
.\sf-data-import.ps1 -fl accounts.csv -so Account
@@ -57,54 +57,22 @@
#>
param(
[Parameter(ParameterSetName="Import", Mandatory)]
[Alias("fl")]
[string]$File,
[Parameter(ParameterSetName="Import", Mandatory)]
[Alias("so")]
[string]$SObject,
[Parameter(ParameterSetName="Import")]
[string]$fl,
[string]$so,
[ValidateSet("insert", "update", "upsert")]
[Alias("op")]
[string]$Operation = "insert",
[Parameter(ParameterSetName="Import")]
[Alias("ei")]
[string]$ExternalId,
[Parameter(ParameterSetName="Import")]
[Alias("to")]
[string]$TargetOrg,
[Parameter(ParameterSetName="Import")]
[Alias("bk")]
[switch]$Bulk,
[Parameter(ParameterSetName="Import")]
[Alias("wt")]
[int]$Wait = 10,
[Parameter(ParameterSetName="Import")]
[Alias("bs")]
[int]$BatchSize = 10000,
[Parameter(ParameterSetName="Import")]
[Alias("ie")]
[switch]$IgnoreErrors,
[Parameter(ParameterSetName="Import")]
[Alias("ve")]
[switch]$VerboseOutput,
[Parameter(ParameterSetName="Help", Mandatory=$true)]
[Alias("hp")]
[switch]$Help
[string]$op = "insert",
[string]$ei,
[string]$to,
[switch]$bk,
[int]$wt = 10,
[int]$bs = 10000,
[switch]$ie,
[switch]$ve,
[switch]$hp
)
# Show help if requested
if ($Help) {
# 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
}
@@ -248,93 +216,93 @@ if (-not (Test-SalesforceCLI)) {
}
# Validate file exists
if (-not (Test-Path $File)) {
Write-Host "Error: File not found: $File" -ForegroundColor Red
if (-not (Test-Path $fl)) {
Write-Host "Error: File not found: $fl" -ForegroundColor Red
exit 1
}
# Validate external ID for upsert/update operations
if (($Operation -eq "upsert" -or $Operation -eq "update") -and -not $ExternalId) {
Write-Host "Error: External ID field is required for $Operation operations" -ForegroundColor Red
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 $File
Write-Host "Using file: $File" -ForegroundColor Green
$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 $File)) {
if (-not (Test-CSVFile $fl)) {
exit 1
}
}
"json" {
if (-not (Test-JSONFile $File)) {
if (-not (Test-JSONFile $fl)) {
exit 1
}
}
}
# Show file preview if verbose
if ($VerboseOutput) {
Show-FilePreview $File $fileFormat
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", $File, "--sobject", $SObject)
$sfArgs = @("data", "import", "bulk", "--file", $fl, "--sobject", $so)
} else {
# Use tree import for JSON files
$sfArgs = @("data", "import", "tree", "--files", $File)
$sfArgs = @("data", "import", "tree", "--files", $fl)
}
# Add optional parameters
if ($TargetOrg) {
if ($to) {
$sfArgs += "--target-org"
$sfArgs += $TargetOrg
Write-Host "Target org: $TargetOrg" -ForegroundColor Cyan
$sfArgs += $to
Write-Host "Target org: $to" -ForegroundColor Cyan
}
if ($ExternalId) {
if ($ei) {
$sfArgs += "--external-id"
$sfArgs += $ExternalId
Write-Host "External ID field: $ExternalId" -ForegroundColor Cyan
$sfArgs += $ei
Write-Host "External ID field: $ei" -ForegroundColor Cyan
}
if ($Bulk) {
if ($bk) {
$sfArgs += "--bulk"
Write-Host "Using Bulk API" -ForegroundColor Yellow
}
if ($Wait -ne 10) {
if ($wt -ne 10) {
$sfArgs += "--wait"
$sfArgs += $Wait.ToString()
$sfArgs += $wt.ToString()
}
if ($Bulk -and $BatchSize -ne 10000) {
if ($bk -and $bs -ne 10000) {
$sfArgs += "--batch-size"
$sfArgs += $BatchSize.ToString()
Write-Host "Batch size: $BatchSize" -ForegroundColor Cyan
$sfArgs += $bs.ToString()
Write-Host "Batch size: $bs" -ForegroundColor Cyan
}
if ($IgnoreErrors) {
if ($ie) {
$sfArgs += "--ignore-errors"
Write-Host "Ignoring individual record errors" -ForegroundColor Yellow
}
# Note: sf data import commands don't support --verbose flag
# VerboseOutput only affects the script's own output (file preview)
# 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: $Operation" -ForegroundColor Cyan
Write-Host "sObject: $SObject" -ForegroundColor Cyan
Write-Host "Operation: $op" -ForegroundColor Cyan
Write-Host "sObject: $so" -ForegroundColor Cyan
# Display the command being run
Write-Host ""
@@ -350,19 +318,19 @@ try {
if ($exitCode -eq 0) {
Write-Host "✅ Data import completed successfully!" -ForegroundColor Green
switch ($Operation) {
switch ($op) {
"insert" {
Write-Host "📊 Records inserted into $SObject" -ForegroundColor Cyan
Write-Host "📊 Records inserted into $so" -ForegroundColor Cyan
}
"update" {
Write-Host "📊 Records updated in $SObject" -ForegroundColor Cyan
Write-Host "📊 Records updated in $so" -ForegroundColor Cyan
}
"upsert" {
Write-Host "📊 Records upserted in $SObject (using $ExternalId as external ID)" -ForegroundColor Cyan
Write-Host "📊 Records upserted in $so (using $ei as external ID)" -ForegroundColor Cyan
}
}
if ($VerboseOutput) {
if ($ve) {
Write-Host "💡 Check the output above for detailed results and any warnings" -ForegroundColor Yellow
}
} else {