Skip to main content
19 votes
Accepted

Set Relationship Via Name Pointing Field?

First, I highly recommend you read Bob's post for yourself. He's a much better writer than I am. Also, Salesforce documents this behavior in Relating Records by Using an External ID and Creating ...
Adrian Larson's user avatar
  • 151k
16 votes

Values of SObject map are lost after a DML call

Using complex objects as Map keys can have unexpected consequences. In your example, the insert causes the Id of the key Account to be updated so the hashCode and equals methods of the Account ...
Keith C's user avatar
  • 136k
14 votes

upsert vs update vs insert

In general, there's no real penalty for using upsert as opposed to insert and update. One notable limitation, however, is that upsert does not support generic SObject[] lists or generic SObject ...
sfdcfox's user avatar
  • 497k
14 votes
Accepted

Any reason NOT to use FOR UPDATE?

General If you have FOR UPDATE on your query, you cause the record(s) (and potentially related records too) to be locked at the time of the query execution in the transaction against the Salesforce ...
Phil W's user avatar
  • 37k
12 votes

Any reason to skip DML on empty lists?

When I look at the results of the following coding, I would still use if (!list.isEmpty()) { DML list; } List<Account> accs = new List<Account>(); System.debug(Limits.getCpuTime()); // 1 //...
loeachim's user avatar
  • 463
12 votes

Any reason NOT to use FOR UPDATE?

A lock is always necessary to update a record, but a transaction might already have a lock on the record you want to update, so you don't necessarily need FOR UPDATE in every query. Performing a DML ...
sfdcfox's user avatar
  • 497k
10 votes
Accepted

DML "10 chunks exception" and weird pattern

When you do a DML operation Salesforce breaks down the records into chunks of 200. So if you insert or update say 2000 Accounts Salesforce actually does 10 chunks of 200. Now when you try to do a ...
dBeltowski's user avatar
  • 3,048
10 votes
Accepted

Insert multiple sObjects in single DML call

You need to use a related field Contact__r to associate the Contact to AccountContact__c. Try below code, List<sObject> records = new List<sObject>(); records.add(new Account(...
Devendra's user avatar
  • 5,075
10 votes
Accepted

Is DML not allowed in a Visualforce getter?

Here ya go. According to Considerations for Creating Custom Controllers and Controller Extensions You can't use data manipulation language (DML) operations in a “getxxx” method in a controller....
Eric's user avatar
  • 54.2k
10 votes
Accepted

Delete an SObject by knowing only Id

Yes, you can delete an object knowing only its Id. If you know the object's type at development time, you can initialize an sObject instance with the Id and supply it to the delete DML operator, as ...
David Reed's user avatar
  • 93.2k
9 votes

Insert multiple sObjects in single DML call

You need to use the name pointing reference. Here, that would be Contact__r. It might be more clear if you look at how it works if you set the value dynamically: you can either call set with the Id, ...
Adrian Larson's user avatar
  • 151k
9 votes

upsert vs update vs insert

INSERT Always use to create new records Support generic SObject type insert Note: Must NOT have id in the Sobject instance UPDATE: Always use to update the record Support generic Sobject type ...
Ashwani's user avatar
  • 22.6k
9 votes
Accepted

How this actually update where there is no change in record?

The short answer is: It forces an update on all records (accounts) in the database. The query returns every single account from the database and the update DML statement is obvious. Regardless of ...
Boris Bachovski's user avatar
8 votes
Accepted

Remove Id from sObject

You have two choices: assign null to Id, or SObject.clone. Assign Null To Id myObject__c customObj = [Select Name,Account__c from myObject__c where id='a06R0000002fMqY']; ...
sfdcfox's user avatar
  • 497k
8 votes

DML Merge Fails INVALID_FIELD_FOR_INSERT_UPDATE

I'm not sure why, but the merge statement appears to be sensitive to the fields that are in memory for an sObject when passed to the merge statement. The operation fails if any of the fields are not ...
Ralph Callaway's user avatar
8 votes
Accepted

DML requires SObject or SObject list type: List<List<Commission_Line__c>>

When you have a Map<T1, T2> and call values, you get a List<T2>. So when you have a Map<T, List<T>>, you get a List<List<T>>. Basically, you're going to have to ...
Adrian Larson's user avatar
  • 151k
7 votes
Accepted

Operations That Don't Invoke Triggers - Cascading delete operations

In your use case, you are explicitly calling a Delete DML operation on the child Object2 and that will execute the before/after delete triggers on Object2 But, imagine that the trigger of Object1 did ...
cropredy's user avatar
  • 72.1k
7 votes
Accepted

Exceptions and DML

The transaction will only be rolled back automatically if the thrown exception isn't caught. Your example showed a call stack like: Class A calls Class B Class B calls Class C Class C attempts a ...
Daniel Ballinger's user avatar
6 votes

Insert List of Parent and Child Records in One DML Statement

In the doc, the example given is a list of two sobjects: Database.SaveResult[] results = Database.insert(new SObject[] {parentAccount, newOpportunity }); If you want to do a list of lists: SObject[]...
cropredy's user avatar
  • 72.1k
6 votes
Accepted

Is it possible to create the SkillUser object from apex

You can use REST to create these records as well. I find it a much simpler approach. public static HttpResponse postSkillUser(SkillUser record) { String endpoint = URL.getSalesforceBaseUrl()....
Adrian Larson's user avatar
  • 151k
6 votes
Accepted

System.runas throwing insert failed error in test class

The Username field is unique across all orgs. Try using a value less likely to collide. Something like [email protected]. You can randomize it more by adding static counters, ...
Adrian Larson's user avatar
  • 151k
6 votes
Accepted

Insert Parent and Child Objects of One Type within One DML statement

This approach only works with different types. Take a look at Creating Parent and Child Records in a Single Statement Using Foreign Keys (emphasis mine): You can use external ID fields as foreign ...
Adrian Larson's user avatar
  • 151k
6 votes

upsert vs update vs insert

If you are sure that, you are migrating data into a fresh organization there use insert operation. If you are not sure that, whether data exists in that environment then go for upsert. In upsert ...
Santanu Boral's user avatar
6 votes
Accepted

Trigger NullPointerException Error Before Update

You can't set cross object fields on trigger context records. When you specify Case__r on a record in your trigger.new collection, it is null. Hence trying to set its Status results in an attempt to ...
Adrian Larson's user avatar
  • 151k
6 votes

DML Statement vs. Database method with true parameter?

There are three main points, all of which are trivial. Inline DML Statements ... ... Require Less Typing insert records; // or... Database.insert(records, true); ... Cannot Be Shadowed public ...
sfdcfox's user avatar
  • 497k
6 votes

Does an update called in an @future method count as being in the same transaction as the method that called it?

Executing a @future method counts as DML for the purposes of mixing callouts and DML. You can demonstrate it for yourself with a simple Minimum Viable Reproduction (MVR). public with sharing class ...
Adrian Larson's user avatar
  • 151k
6 votes

Insert 2 objects (parent and child) at a time (over 200 records)

Your Case.Id values are null until your perform the insert with insert newCases. You should be performing two DML operations, as you do here, and only a single SOQL query. Right now, you're ...
David Reed's user avatar
  • 93.2k
6 votes
Accepted

Is it possible to grab the external Ids of the records that failed in database.insert

The order of the Database.SaveResult items will be the same order as the original DML list. As such, you can figure out the external Id values simply by looping over the list: List<SObject> ...
sfdcfox's user avatar
  • 497k
6 votes
Accepted

What does it mean when an external ID field follows a DML Statement?

This is valid only for the upsert function. Its purpose is to provide an "External ID" field to create/update records against. This is often useful when you don't know the Salesforce ID of a record, ...
sfdcfox's user avatar
  • 497k
6 votes
Accepted

Calling Apex method (to insert a record) imperatively from LWC JS

You are using a getter - it will be invoked only when the tracked property which is being used in template also changes. You can invoke it in connectedCallback to invoke on initialization of component....
salesforce-sas's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible