Hot answers tagged

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
  • 149k
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 ...
Keith C's user avatar
  • 134k
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
  • 476k
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
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@...
naman bhardwaj's user avatar
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 ...
Eric's user avatar
  • 53.9k
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 ...
Phil W's user avatar
  • 33.4k
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,038
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,057
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
  • 53.9k
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
  • 91.7k
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 ...
sfdcfox's user avatar
  • 476k
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
  • 149k
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.5k
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

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....
Vigneshwaran G's user avatar
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 ...
Adrian Larson's user avatar
  • 149k
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
  • 476k
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
  • 149k
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 ...
cropredy's user avatar
  • 69.1k
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 ...
BritishBoyinDC's user avatar
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 ...
sfdcfox's user avatar
  • 476k
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
  • 69.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
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 : [...
Eric's user avatar
  • 53.9k
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 ...
Eric's user avatar
  • 53.9k
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
  • 149k
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 ...
Ilya Lepesh's user avatar
  • 4,105
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
  • 69.1k

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