Skip to main content

Using the CLI and Powershell

Inserting or Updating:

Put all your data into a CSV file with the exact same column headings as the Salesforce fields, again making sure to save as ASCII and not UTF8 or any other encoding (in Excel, you want the option called "CSV (Comma delimited)" not "CSV UTF-8"). And then do this:

force bulk insert Object__c .\new_object_data.csv

To modify fields for updating, don't do the updates in Excel unless you have to - do it inline like this:

force query --format json "Select whatever from Object" | ConvertFrom-Json | %{$_.Some_Field__c = 'whatever'; $_} | select Id,Some_Field__c | Export-Csv .\updates.csv -NoTypeInformation -Encoding ASCII

Note the following:
The foreach (%) block at the end it returns the object being iterated over. This is really important otherwise it won't pipe anything through to the "select".
Speaking of the "select", you have to select only the fields you're working with, because "force query" returns an extra "attributes" field and Salesforce will get very confused if you include it.

Then just do the following:

force bulk update Object__c .\updates.csv


If you're deleting the result of a bulk export, download the result csv and run this:

get-content 'result' | convertfrom-csv | where Id -NE '' | select Id | export-csv 'delete.csv' -NoTypeInformation -Encoding ASCII
force bulk delete Object__c 'delete.csv'

Otherwise, first, write the list of records to a file (you could just save to a variable but it can come in handy to keep a list of IDs when modifying records).  Make sure to encode the file as ASCII and remove all the quotes or Salesforce will refuse to cooperate after the next step.

force query --format csv "SELECT Id FROM Object__c WHERE Field__c = 'Whatever'" | % {$_ -replace '"', ''} | Out-File .\ids_to_delete.csv -Encoding ASCII

(Big thanks to Ed over at the excellent Hey, Scripting Guy! blog for demonstrating this elegant way to remove the quotes)

Or perhaps a syntactically simpler way using JSON that avoids the need for quote replacing:

force query --format json "SELECT Id FROM Object__c WHERE Field__c = 'Whatever'" | ConvertFrom-Json | Export-Csv .\ids_to_delete.csv -Encoding ascii -NoTypeInformation

Either way, next pass that CSV to the Salesforce bulk API:

force bulk delete Object__c .\ids_to_delete.csv

Or, if you just have a medium-sized list of IDs to delete and they're not in a CSV or coming from a query (e.g. copypasta from elsewhere), just paste them right into the command line as one big string and .Split() them, like this:

"axxx000000xxxxxAAA axxx000000xxxxxAAA etc".Split(" ") | % { force record delete Object__c $_ }

Querying Metadata:

For getting a list of fields, which I need to do all the time:

(force describe -t=sobject -n=Object__c | convertfrom-json).fields | select name

Check out the output from just the part inside the parentheses to see all the other metadata you can use. It's easier to just make this a Powershell function and add it to your PS profile if you use this a lot, though.


You can query both ObjectPermissions and FieldPermissions to get the permissions of a particular profile on different things:
force query "SELECT Id, SObjectType, PermissionsRead, PermissionsCreate FROM ObjectPermissions WHERE parentid in (select id from permissionset where PermissionSet.Profile.Name = 'System Administrator')"

For FieldPermissions it's almost the same, but the fields are slightly different and you also need to add a "WHERE SobjectType = 'Custom_Object_Name__c'"

User Profiles:

Get a count of users in each profile like this:
$users = force query --format json "select, user.Email, user.FirstName, user.LastName,, user.Username, user.IsActive FROM user where user.isactive = true limit 1" | convertfrom-json
$users | select -expandproperty Profile | select -ExpandProperty Name | group

Logging into a sandbox:

Obviously replace the domain below with the domain of your sandbox:

force login -i=""


Popular posts from this blog

'A section using configSource may contain no other attributes or elements' error after installing Application Insights

After installing the Application Insights nuget package to an Umbraco solution, you'll get this error:

A section using 'configSource' may contain no other attributes or elements

<ExamineLuceneIndexSets configSource="config\ExamineIndex.config" />
     <log4net configSource="config\log4net.config">
             <level value="ALL" />
             <appender-ref ref="aiAppender" />
Source File: \project\web.config

This happens because part of the Application Insights installation process adds a <log4net> section to web.config.  Which would make sense, except Umbraco already has a <log4net> section in /config/log4net.config.  So as you can imagine, the solution is to manually move everything its added into that file. Unfortunately you can't just copy/paste the whole lot, but it's not particularly complicated:

Move <appender-ref ref="aiAppender" /> into the lo…

Handling double-quoted CSVs in Azure Data Factory Pipelines

Azure Data Factory by default uses a backslash as the escape character for CSVs, but we ran into an issue with this today processing one of the CSV files from  As with most CSVs they use quotes around values as normal and with double-quotes for empty values, but they also use double-quotes to escape quotes within non-empty values. This probably sounds confusing, so here's an example:

"column 1","column 2","","column 4 value is ""sort of"" like this"

The ADF pipeline failed because the double-quotes don't get escaped correctly:
ErrorCode=UserErrorSourceDataContainsMoreColumnsThanDefined, found more columns than expected column count.

The solution was to change the "Escape character" property on the dataset, by clicking the "Edit" button beneath it and manually entering "", since "" isn't in the list of escape characters.  I didn't think this would work at fir…

Using WiX to create an event source during install of a .NET framework project

Edit: so I guess I wasn't the only one confused with this stuff, as it's been my most popular post by far!  If I've helped you out or saved you some time, please let me know in the comments :)

In order for this to work, you have to add references to WixUtilExtension and WixNetFxExtension to your WiX project.  Once that's done, add this inside a <Component> element:

<Util:EventSourcexmlns:Util=""Name="EVENTSOURCEGOESHERE"Log="Application"EventMessageFile="[NETFRAMEWORK40FULLINSTALLROOTDIR]EventLogMessages.dll" />
Obviously replace EVENTSOURCEGOESHERE with your event source name.  NETFRAMEWORK40FULLINSTALLROOTDIR is a property set by the WixNetFxExtension which stores the path to the .NET framework v4 directory, but you can replace this with the corresponding property for the directory containing the relevant EventLogMessages.dll file.  So if you're using the .NET framewo…