Skip to content
Christian G. Warden edited this page Sep 4, 2025 · 9 revisions

Examples

Freeze all users with specific profiles by updating UserLogin records.

$ batchforce update UserLogin -q "SELECT Id FROM UserLogin WHERE UserId IN (SELECT Id FROM User WHERE Profile.Name IN ('Marketing', 'Customer Support'))" '{Id: record.Id, IsFrozen: true}'

Create new Notes from Tasks linked to specific SObject records by upserting ContentVersion records.

batchforce upsert --external-id Migrated_From__c -b 10000 ContentVersion \
--query-all "SELECT Id, Description, CreatedDate, WhatId, CreatedBy.Name, CreatedById FROM Task WHERE Subject = 'Note' and What.Type = 'Inquiry__c' and What.RecordType.DeveloperName = 'Standard' and IsDeleted = false ORDER BY CreatedDate DESC" \
'{Title: "Note from " + record.CreatedBy.Name, FirstPublishLocationId: record.WhatId, CreatedById: record.CreatedById, CreatedDate: record.CreatedDate, PathOnClient: "Note from " + record.CreatedBy.Name + ".snote", VersionData: base64(escapeHtml(escapeUnicode(record.Description))), Migrated_From__c: record.Id}'

Get the value of a field from the most recent Opportunity for an Account and copy it up to the parent Account record.

$ batchforce update Account -q "SELECT Id, URL__c, Account.URL__c, AccountId FROM Opportunity WHERE URL__c != null AND Account.URL = null ORDER BY AccountId, CreatedDate desc" \
'incr(record.AccountId) > 1 ? nil : {Id: record.AccountId, URL__c: record.URL__c}'

Set the --context flag to anonymous apex to use org-specific data in your Expr expression, a Role Id in this example, through the apex variable.

$ batchforce update User --context "Id newRoleId = [SELECT Id FROM UserRole WHERE DeveloperName = 'New_Role'].Id;" \
  -q "SELECT Id FROM User WHERE UserRole.DeveloperName = 'Old_Role'" \
  '{Id: record.Id, UserRoleId: apex.newRoleId}'

Note that there's a limit on the number ContentVersion records that can be created per day. The maximum it can be increased to by opening a case with Salesforce is 500,000.

$ batchforce insert ContentVersion -q "SELECT ContentType, Description, Name, Id, ParentId FROM Attachment LIMIT 400000" 'let types = {
  "application/pdf" : "PDF",
  "application/msword" : "DOC",
  "application/vnd.openxmlformats-officedocument.wordprocessingml.document" : "DOCX",
  "application/vnd.ms-excel" : "XLS",
  "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" : "XLSX",
  "application/vnd.ms-powerpoint" : "PPT",
  "application/vnd.openxmlformats-officedocument.presentationml.presentation" : "PPTX",
  "text/plain" : "TXT",
  "text/csv" : "CSV",
  "text/html" : "HTML",
  "application/xml" : "XML",
  "text/xml" : "XML",
  "application/json" : "JSON",
  "image/jpeg" : "JPG",
  "image/png" : "PNG",
  "image/gif" : "GIF",
  "image/tiff" : "TIF",
  "audio/mpeg" : "MP3",
  "video/mp4" : "MP4",
  "application/zip" : "ZIP"
}; 
{
  Title: record.Name,
  Description: record.Description,
  FirstPublishLocationId: record.ParentId,
  PathOnClient: "Attachment." + types[record.ContentType] ?? "unknown",
  VersionData: base64(fetch("/services/data/v64.0/sobjects/Attachment/" + record.Id + "/Body"))
}'

Store Formatted Time Values

Format CreatedDate in custom fields for Day of Week (e.g. "01-Mon") and Hour Created using a user-agnostic Timezone.

$ batchforce update Lead -q "SELECT Id, CreatedDate, Division__r.Time_Zone__c FROM Lead WHERE Created_Day__c = null" \
 'let t = date(record.CreatedDate, "2006-01-02T15:04:05.000-0700").In(timezone(record.Division__r.Time_Zone__c));
  let day = "0" + string(int(t.Weekday()) + 1) + "-" + t.Format("Mon");
  {Id: record.Id, Created_Day__c: day, Created_Hour__c: a.Format("15")}'

Populate Field From History

Set a custom Closed_By__c field to the User who most recently updated the Stage of an Opportunity to "Closed Lost".

$ batchforce update Opportunity \
  -q "SELECT Id, (SELECT CreatedById, NewValue FROM Histories WHERE Field = 'StageName' ORDER BY CreatedDate DESC) from Opportunity WHERE StageName = 'Closed Lost'" \
  'let history = record.Histories ?? []; let update = filter(history, #.NewValue == "Closed Lost") | first(); update == nil ? nil : {Id: record.Id, Closed_By__c: update.CreatedById}'