Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toanshulverma/77cbe81eecfacd64bd46a84ec46dd095 to your computer and use it in GitHub Desktop.
Save toanshulverma/77cbe81eecfacd64bd46a84ec46dd095 to your computer and use it in GitHub Desktop.
PS - Create Salesforce Record with attachment

PRE-REQUISITES

  • Install SFDX
  • Authorize target org within SFDX with desired alias

USAGE

Open powershell and fire this command

./createstagingwithattachment.ps1 -objectName [OBJECT NAME] -instanceName [ORG NAME] -objectData [OBJECT FIELD DATA] -attachment [ATTACHMENT FILE]

  • [OBJECT NAME] = api name of the object for which record is to be created
  • [ORG NAME] = alias/username of salesforce org
  • [OBJECT FIELD DATA] = object data in space separated format (refer examples)
  • [ATTACHMENT FILE] = file path for the attachment file to be uploaded

Example 1: Create Account record and upload a test attachment

./createstagingwithattachment.ps1 -objectName Account -instanceName test -objectData "Name='testaccount2' Phone='8765434567'" -attachment "c:\test.txt"

Example 2: Create Contact record and upload a test attachment

./createstagingwithattachment.ps1 -objectName Contact -instanceName test -objectData "Lastname='testcontact' Phone='18765434567'" -attachment "c:\test.txt"

Upload files as Salesforce Files (ContentVersion/ContentDocument)

Uses SFDX plugins from Shane McLaughlin To understand installation and other options refer https://github.com/mshanemc/shane-sfdx-plugins

Pre-Requisites

  • Install Shane's SFDX plugins

sfdx plugins:install shane-sfdx-plugins

Example usage

./createstagingwithattachment2.ps1 -objectName Contact -instanceName test -objectData "Lastname='testcontact' Phone='18765434567'" -attachment "c:\test.txt"

# Insert record with attachment
param (
[string]$objectName,
[string]$instanceName,
[string]$objectData,
[string]$attachment
)
# create record
$recordData = sfdx force:data:record:create -s $objectName -v $objectData -u $instanceName --json | ConvertFrom-Json
if($recordData -eq $Null){
throw
}
$parentid = $recordData.result.id
$base64string = [Convert]::ToBase64String([IO.File]::ReadAllBytes($attachment))
$fileName = Split-Path $attachment -leaf
# upload attachment
sfdx force:data:record:create -s Attachment -v "Body='$base64string' ParentId='$parentid' Name='$fileName'" -u $instanceName
# Insert record with attachment
# Uses Shane's SFDX extensions to upload files
param (
[string]$objectName,
[string]$instanceName,
[string]$objectData,
[string]$attachment
)
# create record
$recordData = sfdx force:data:record:create -s $objectName -v $objectData -u $instanceName --json | ConvertFrom-Json
if($recordData -eq $Null){
throw
}
$parentid = $recordData.result.id
# upload attachment
sfdx shane:data:file:upload -f $attachment -p $parentid -u $instanceName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment