Last call to make your voice heard! Our 2022 Developer Survey closes in less than a week. Take survey.

Hot answers tagged

18 votes
Accepted

Too many if statments?

If you are just looking for a bit of code refactoring of this, creating a list of the fields you want to copy and then looping over the list and making use of methods like SObject.get and SObject.put ...
user avatar
  • 129k
18 votes
Accepted

MIXED_DML_OPERATION, DML operation on setup object is not permitted

You should use @future class to avoid this error, as described in the Apex Developer Guide.
16 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 ...
user avatar
  • 142k
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 ...
user avatar
  • 129k
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 ...
user avatar
  • 430k
13 votes
Accepted

'After' trigger's SOQL - will query fetch current value of the inserted/updated record?

Step 5 refers to a temporary database save; the rows are saved to the database, but the transaction isn't committed. When data is saved to the database, it becomes available to the current transaction....
user avatar
  • 430k
13 votes
Accepted

DML operation and async Queueable job - which executes first?

Transactions have to be fully ACID compliant. The "C" stands for consistency, and this is where transaction control comes in. In order for your Queueable to execute, it has to be guaranteed that the ...
user avatar
  • 430k
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@...
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 ...
user avatar
  • 53.1k
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 ...
user avatar
  • 2,996
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(...
user avatar
  • 4,917
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....
user avatar
  • 53.1k
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 ...
user avatar
  • 86.2k
9 votes

How do I delete records in the recycle bin?

It is possible to hard delete using DataBase.emptyRecycleBin method in the Batch class. Create a sample Batch class as mentioned below and use DataBase.emptyRecycleBin method in the Batch class. ...
user avatar
9 votes

DML operation and async Queueable job - which executes first?

That's an interesting question. With the queueable execution being asynchronous, could there be a particular timing where the queued job would start executing before the corresponding Callout__c ...
user avatar
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 ...
user avatar
  • 22.2k
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 ...
user avatar
8 votes
Accepted

Problem with Database.update()

The reason your catch block is not being executed is because of the false parameter you are passing in: Database.update(accounts, false); That parameter is called is_all_or_none meaning treat the ...
user avatar
  • 19.1k
8 votes
Accepted

Database.rollback() risks and alternative

I wish I had some very clear documentation, but to be honest, I don't. I assume you found the small amount of documentation on savepoints and rollbacks here: https://developer.salesforce.com/docs/...
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....
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 ...
user avatar
  • 142k
8 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, ...
user avatar
  • 142k
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 ...
user avatar
7 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 //...
user avatar
  • 373
7 votes
Accepted

How to know which Database.SaveResult instance corresponds to my SObject

The order of the SaveResult[] array matches the order of the inserted array. List<Thing__c> myThings = new List<Thing__c>(); //... Database.SaveResult[] saveResults = Database.insert(...
user avatar
  • 11.3k
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 ...
user avatar
  • 61.5k
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 ...
user avatar
  • 430k
7 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']; ...
user avatar
  • 430k
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 ...
user avatar
  • 61.5k
7 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 ...
user avatar
  • 142k

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