Skip to content

Instantly share code, notes, and snippets.

@lfreeland
lfreeland / gist:08854c27859362f42cc3de3bfb2423ad
Created May 12, 2016 12:22
Upsert List<SObject> With External Id And No "AllOrNothing" Fix
public static List<Database.UpsertResult> upsertListSObjectsWithExternalIdNoAllOrNothingErrorExampleFix() {
/* To resolve the System.TypeException: DML on generic List<SObject> only allowed for insert, update or delete"
error, dynamically instantiate the List of SObject to a list of a concrete SObject type.
*/
Account a = new Account(
Name = 'Example Company',
External_Id__c = 'ExampleCompany'
);
@lfreeland
lfreeland / gist:e9bef06713b0378d5a95ad1390863215
Created May 12, 2016 12:25
Upsert List<SObject> with External Id with "AllOrNothing" Error Example
public static List<Database.UpsertResult> upsertListSObjectsWithExternalIdWithAllOrNothingErrorExample() {
List<SObject> records = new List<SObject>();
Account a = new Account(
Name = 'Example Company',
External_Id__c = 'ExampleCompany'
);
records.add(a);
public List<Database.upsertResult> upsertWorkaround(String sobjectType, List<SObject> recordsToUpsert, Schema.SObjectField externalIdField, Boolean allOrNothing) {
if (sobjectType == 'Account') {
return Database.upsert((List<Account>) recordsToUpsert, externalIdField, allOrNothing);
}
if (sobjectType == 'Contact') {
return Database.upsert((List<Contact>) recordsToUpsert, externalIdField, allOrNothing);
}
}
public List<Database.upsertResult> upsertWorkaround(String sobjectType, List<SObject> recordsToUpsert, Schema.SObjectField externalIdField, Boolean allOrNothing) {
if (sobjectType == 'Account') {
return Database.upsert((List<Account>) recordsToUpsert, externalIdField, allOrNothing);
}
if (sobjectType == 'Contact') {
return Database.upsert((List<Contact>) recordsToUpsert, externalIdField, allOrNothing);
}
// etc
@lfreeland
lfreeland / gist:e6e92ef93926b962d9b16dc3304e5aee
Created May 22, 2016 04:09
TypeScript2 Async Await Example
async function main() {
await ping();
// Other code here is executed without a callback function.
}
main();
Select count()
from Account
where <predicate>
Select Id,
Name,
...
from Account
where <predicate>
public Batch CreateAttachmentBatch(CreateAttachmentBatchRequest request)
{
String requestTxtFileCSVContents = "Name,ParentId,Body" + Environment.NewLine;
requestTxtFileCSVContents += request.FilePath + "," + request.ParentId + ",#" + request.FilePath;
using (MemoryStream memoryStream = new MemoryStream())
{
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
byte[] requestTxtFileCSVContentsBytes = UTF8Encoding.UTF8.GetBytes(requestTxtFileCSVContents);
/// <summary>
/// Tests attaching a file to the specified "AttachmentParentId" record.
/// </summary>
[TestMethod]
public void AttachFileTest()
{
String parentId = ConfigurationManager.AppSettings["AttachmentParentId"];
Assert.IsTrue(String.IsNullOrWhiteSpace(parentId) == false, "The AttachmentParentId app setting is blank. Please specify the id of a salesforce record to attach an attachment file.");
// records populated
List<SObject> records = ...;
Set<Id> recordIds = new Set<Id>();
for (SObject record : records) {
recordIds.add(record.Id);
}