Questions tagged [dml]

Use this tag for questions about DML, the data manipulation language used in apex to perform operations as insert, update, delete, and restore data in the database.

Filter by
Sorted by
Tagged with
91
votes
3answers
4k views

Any reason to skip DML on empty lists?

In parts of my code I have DML statements with an if around to only do the DML if the list is not empty: if( !recordsToUpdate.isEmpty() ) { update recordsToUpdate; } Couldn't I just write? ...
28
votes
2answers
486 views

How to write the superduper (=proper and stable) Custom Save method

Custom Save actions in Apex are tricky! Here are my requirements which I was unable to fulfill without harming the stability of my code: Save for New and Edit (works with record with and without Id) ...
25
votes
1answer
338 views

Why am I double charged on DML limits in a process builder?

This question is a follow-up from another question where the accepted answer states that for each Update Records action in a process, following limits are consumed: As you can therefore conclude, ...
20
votes
1answer
352 views

Email value is saved without parentheses (label) when using DML

When an email contains a label enclosed in parentheses, the value can be saved as typed in via SF UI. (Note: the email value "as is" is only seen when in Edit mode.) However, when the record is saved ...
18
votes
1answer
19k views

Database.SaveResult - how to get the Id of the record where Update failed?

I am using Database.SaveResult and Database.update(..., false) for the very first time and ask myself how to get the Id of the record where a dml has failed. I expected Database.SaveResult.getId()` ...
15
votes
2answers
9k views

How do I delete records in the recycle bin?

I understand that when I perform a Database.delete() operation, records will end up in my org's recycle bin. And their presence in the recycle bin is reflected by the IsDeleted field on the object. ...
14
votes
1answer
4k views

Proper method to get an Sobject instance from an Id

What is the "proper" way of getting an object from an Id? The method I use at the moment is SOQL, and that feels wrong..... Example: Account account = [SELECT Id FROM Account WHERE Id=:inserted....
14
votes
5answers
14k views

Triggering a DML Exception for Test Coverage

I have an Apex Class, where 55% of the code is inside a catch, requiring a DML exception to be executed. Since the DML update request is using data from an SOQL request inside this class, I have no ...
14
votes
1answer
9k views

DML not allowed in page action

So I understand DML Operations aren't allowed in getters/setters,constructors but it was my understanding that they were allowed in methods that were invoked by page-actions like so: page: <apex:...
14
votes
1answer
846 views

Set Relationship Via Name Pointing Field?

One of the most startling things I ever learned from @BobBuzzard is that you can update a lookup by using the name pointing field. At least, I thought that's what I learned, until a recent debate cast ...
13
votes
2answers
10k views

Updating multi currency rates from Apex

I just looked in the Salesforce.com documentation for a way to update currency rates using Apex, but I have a vague feeling that this is not possible using standard features. I noticed that a couple ...
13
votes
1answer
9k views

Is there a way to make the insert not fail if one record fails?

I am inserting multiple records using Apex DML; e.g. List<Account> accounts = new List<Account>(); // Populate the accounts list insert accounts; If there is any kind of error ...
13
votes
2answers
1k views

DML operation and async Queueable job - which executes first?

I have a trigger after insert on some arbitrary entity: trigger OpportunityTrigger on Opportunity (after insert) { // not bulkified for the sake of simplicity ID jobID = System.enqueueJob(...
13
votes
1answer
5k views

What is DML? How different is it from SOQL Actions?

This question is designated as a community wiki, please add your inputs, guides (please try to ignore oustside links, and bring the important skimmed data in here. What is DML? What is Dynamic-DML? ...
12
votes
2answers
2k views

Fields missing using getContent call for Visualforce rendered as PDF

I am attempting to get the content of a visualforce page that is rendered as a PDF. I make some changes to the object that the visualforce page is displaying data from, update that object, make the ...
11
votes
5answers
13k views

System.CalloutException: You have uncommitted work pending. Please commit or rollback

I am sending only a single HTTP Request in Batch but still facing error System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out My test code is @...
11
votes
2answers
9k views

Generic SObject update pattern

Question I know that it is possible to update a generic of List<SObject>, But what is the best approach to generate that list without knowing the SObjectType, only the ids ? Sample: List<...
11
votes
2answers
7k views

Rolling back DML operation in Apex method

What is the best way to rollback some changes to the database inside an Apex method? For example, in an Apex method, I insert multiple records (Opportunity / Quote / QuoteLineItems). When I execute ...
11
votes
1answer
2k views

Is DML not allowed in a Visualforce getter?

Summary I am trying to simplify some code for a page that makes a callout, then logs the request and response. I stumbled across a scenario in which the log record appears to create successfully, but ...
10
votes
1answer
8k views

Setting new user passwords in Apex

I have a method that creates a new user and sets a custom password, everything works fine to the point where I try to log in with the new user and get an error that the login details are incorrect. ...
10
votes
1answer
2k views

How To Cause DmlException - Delete

I thought I would be able to take a User who does not have delete privileges on an object, delete records from that object, and get a DmlException. No dice. Below are the Case permissions for the ...
10
votes
2answers
1k views

Order of execution for records on DML statements

A list of objects should be inserted in the order it is populated, so lower indexes will be inserted first. This is important when dealing with a List<sObject> where you rely on the order within ...
10
votes
3answers
6k views

Rollback whole Batch on error

I am using an APEX batch to process and insert large amounts of data. Essentially I would like it to process every row to catch any errors with the data and then rollback the entire batch if a single ...
9
votes
4answers
12k views

upsert vs update vs insert

I'm trying to understand when to use insert/update/upsert. As I understand, upsert is a combination of insert and update. I tend to use upsert most often as I do not have to check whether an object is ...
9
votes
1answer
2k views

Submit records for approval in bulk without hitting DML Statement limit

I've got code to create and submit for approval a custom object. I'm running into the DML statement limit. As far as I can see each record I submit for approval takes up a DML statement, but there's ...
9
votes
1answer
782 views

Avoiding Mixed DML (setup/non-setup) Approach

I need an approach to allow delegated admins (i.e., non-Administators) to add members to Public Groups and Collaboration Groups (the GroupMember and CollaborationGroupMember objects). I added a ...
9
votes
1answer
710 views

Is upsert properly ACID compliant?

Given an object Table_A with one unique field Unique and two rows: row Unique --- ------ 1 A 2 B And the following class: public class KeySwapDML { public void ...
8
votes
2answers
10k views

MIXED_DML_OPERATION, DML operation on setup object is not permitted [duplicate]

I'm writing a Trigger that After Insert of a new User, I need to create a Contact with that same new User information. I'm getting this error: MIXED_DML_OPERATION DML operation on setup ...
8
votes
1answer
10k views

Creating a user with DML

I have the following Apex code: User mike = new User(); mike.Username= 'mike.white@company.com'; mike.Email = 'mike.white@company.com'; mike.Lastname = 'mike'; mike.Firstname = 'white'; mike.Alias = '...
8
votes
1answer
784 views

Too many if statments?

So the Apex docs say you have to set the value of the master record if you want to retain the discarded account field values before the merge operation. Is there a better way to do validation for a ...
8
votes
1answer
3k views

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

I want to get data from children objects and populate field at parent object. I used after insert & update trigger to get the information. If I run a query on inserted/ updated object in a ...
7
votes
4answers
20k views

How to avoid DML Insert/Update Limit of 10,000 rows when running anonymous apex?

Is there any way to bulkify an insert/update operation performed from anonymous apex? For instance, if I have a script that builds a list of 15,000 records that need to be updated, is there any way ...
7
votes
2answers
7k views

where do I use Database. Methods?

I m trying to understand where we use following Database.Methods. I know if we want to insert a record to an object we use DML Statement. What i want to know is why/where we use Database. methods. It ...
7
votes
3answers
2k views

Can you perform DML operations from visualforce email templates?

Visualforce email templates allow you to include visualforce components with allowDML="true" but there doesn't appear to be any way to actually use DML. From the docs: You can't use data ...
7
votes
1answer
373 views

DML “10 chunks exception” and weird pattern

I am aware of System.TypeException: Cannot have more than 10 chunks in a single operation exception and it seems to be a known bug. I am also aware, that there are some other ways one can encounter ...
7
votes
1answer
318 views

Database.DMLOptions - How to prevent Chatter Group Auto-Follow trigger from generating email

Currently I have a trigger which auto-follows specific groups in our organization when a new user is brought onboard. The auto-follow trigger feature works great! However, we have quite a few groups ...
7
votes
2answers
2k views

Can we have a DML not roll back when i use an adderror method?

We have a scenario where in the before insert trigger of object Asset, which looks for a duplicate and if a duplicate is found a record is inserted in the junction table and after the dml is complete, ...
7
votes
1answer
2k views

Do triggers run asynchronously when triggered by DML in apex?

The question is fairly self-explanatory, but to add a little background, here's the scenario that has made me ask. First a (very basic) SSCCE: public void createAttachments(List<Account> ...
7
votes
1answer
168 views

Why doesn't SFDC allow MIXED_DML_OPERATIONs anymore?

Okay, I know very well by now that SFDC does not want and often does not allow us simply insert and/or update Setup and/or "Non-setup" sObjects in the same context anymore, something I used to do with ...
7
votes
1answer
102 views

Combining DML, Callouts and Triggers gives CalloutException

When running unit tests, I'm currently getting: System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out The issue is that I have a trigger that ...
7
votes
1answer
65 views

From inside a Salesforce packaging org, how do you query the list of created versions for a managed package?

I have, a Salesforce packaging org, it contains a managed package with a namespace. I need to be able to query the list of created versions for that managed package. I am aware that a user can go ...
7
votes
2answers
901 views

DML not allowed on ApexClass

I am trying to move around 80 classes and visualforce pages from sandbox to production by using change set but i got an error One or more components failed Version Compatibility Check. This ...
7
votes
1answer
195 views

Why is DML not allowed on AdditionalNumber?

The following test method will not compile or save from my IDE. It give an error of: "(OIGForceTest) DML not allowed on AdditionalNumber " static testMethod void testGetDirectoryNumbers() { // ...
6
votes
2answers
411 views

Values of SObject map are lost after a DML call

Can anyone explain the comments in this snippet? Map<Account, Contact> m = new Map<Account, Contact>(); m.put(new Account(Name='Test'), new Contact(Lastname='Test')); SObject[] records = ...
6
votes
3answers
4k views

No such column 'lastvieweddate' on entity 'Quote__c

This is a bit odd. Sandbox org created under V27.0 Sandbox org now upgraded to V28.0 a few weeks back VF page at V28.0 that interacts with a controller at V27.0 that updates custom object Quote__c ...
6
votes
2answers
541 views

DML Limits are reset when?

I have a scenario where I need to update 500,000 Accounts in production to change their Record Type, but I want to avoid writing a Batch class for one-time use. In addition, using DataLoader would ...
6
votes
3answers
718 views

Randomizing User class username

I have the following method: public static User initUser(){ /** * Create a User * Insert User into DB * Return User back into testPage in order to use information **/ User ...
6
votes
4answers
609 views

Alternatives to using page action for user preferences

Sometimes you have to load user preferences stored in a Hierarchy custom setting when a Visualforce page loads. If the preferences exist, then retrieve them, otherwise create them and insert them. ...
6
votes
1answer
2k views

Can a parent and child record both be inserted in single DML statement?

I have a requirement to insert both a parent and a child record, in a single DML statement. Is this possible to do?
6
votes
2answers
568 views

Why have database rows not been inserted when the debug log says they have?

I have code that creates CaseShare records for the case based on some criteria from another class. The code that creates the CaseShare is called both on update and creation of the case. It works ...