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 ...
15
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 ...
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 ...
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
//...
12
votes
Accepted
Can a parent and child record both be inserted in single DML statement?
Yes, you need to make one field as External id on your parent object. In my case SLASerialNumber__c is external id field.
Contact con = new Contact(lastName='bhardwaj');
con.Email = 'naman.bhardwaj@...
11
votes
Accepted
How To Cause DmlException - Delete
From the docs:
The runAs method doesn’t enforce user permissions or field-level
permissions, only record sharing.
Apex code runs in system context so even though the user does not have delete ...
11
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 ...
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 ...
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(...
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....
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 ...
10
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 ...
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, ...
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 ...
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 ...
8
votes
Difference between defining these Inserts
Yes both the ways have significant different in terms of CPU utilization. Just ran the below code snippet in Execute Anonymous block in Developer Console and got the debug logs.
System.debug(Datetime....
8
votes
Accepted
DML Limits are reset when?
Nothing about the trigger context is going to reset the DML Rows limit. If updating 200 Account records consumes on average 3000 DML Rows, then on average you are going to be able to update 666 ...
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'];
...
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 ...
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 ...
7
votes
DML Limits are reset when?
In addition to @AdrianLarson's answer, let me point out a commonly-overlooked issue with execute anonymous and triggers
Many triggers are written with static Boolean variables to guard against ...
7
votes
Accepted
How to create thousands of records using batch apex
So as written, that doesn't really operate as a batch should - the batch just calls the one method and tries to create all the records in one execution...a batch is designed to run as a series of ...
7
votes
Why have database rows not been inserted when the debug log says they have?
One of your share entries is failing to insert, causing the entire DML to go through the exception handler. Odds are, you're trying to insert a share for the case owner, which isn't allowed. Here's ...
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 ...
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 ...
6
votes
Accepted
How many DML statements will be execute?
0 - Your code will error as the SOQL is not valid, when fixed:
Only 3 DML statement(s) will be used and 500 dml rows will be used
each batch of 200 will count as 1 DML statement
for(Account[] a : [...
6
votes
Accepted
Http callout test class "uncommitted work pending" error with no clear cause
Since you said in your comment that disabling workflows allows it to pass I am going to assume that some of those workflows are email alerts.
This is a know issue which I first reported back in April ...
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()....
6
votes
Accepted
DML update in @future callout not updating sObject
You are expecting from your trigger next flow:
insert account->future callout->update account
But in your code after update statement, you are executing update DML in future context, which fires ...
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[]...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
dml × 518apex × 332
trigger × 89
soql × 41
unit-test × 37
visualforce × 35
dmlexception × 34
batch × 32
exception × 27
update × 25
database × 22
callout × 18
governorlimits × 18
upsert × 15
class × 14
error × 13
insert × 13
sobject × 12
list × 11
future × 11
visual-workflow × 8
before-trigger × 8
dml-operations × 8
webservices × 7
performance × 7