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
14
votes
1answer
1k 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 ...
104
votes
1answer
5k 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? ...
13
votes
2answers
11k 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 ...
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 ...
0
votes
2answers
6k views

DML Statement in ‘after trigger’ and not in ‘before trigger’?

Why we need to use DML Statement in ‘after trigger’ and not in ‘before trigger’? Thanks KS Kumar
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. ...
12
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 ...
8
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, ...
2
votes
3answers
640 views

What to use for performing operations on multiple records at once, Apex REST class or Bulk API?

I need to insert/update/delete a variable number of records at a time from a webapp (so from javascript), are there reasons to choose the Salesforce Bulk API over writing a custom Apex REST class to ...
10
votes
3answers
7k 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 ...
4
votes
4answers
16k views

Invalid conversion from runtime type LIST<ANY> to MAP<String,ANY>

I'm trying to make a webcall to pull data from an external webservice. After that call I would like to insert my data that comes in the form of JSON into my custom object called Company. However I am ...
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:...
10
votes
5answers
14k 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 @...
9
votes
1answer
816 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 ...
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 ...
3
votes
2answers
6k views

Id assignment when creating sObjects in code

I'd like to get straight exactly what is happening when I create an sObject in Force.com code but can't find any information on the specifics. The Id is null until after the DML insert statement. ...
1
vote
2answers
299 views

Does Database.SaveResult, Database.DeleteResult, etc. implement a public interface?

The various DML operations (insert, update, delete, etc.) all return results with very similar methods and properties. It is not uncommon, as a developer, that I should want to handle these results ...
-3
votes
1answer
107 views

Anyone can optimized this code for better explanation and easy to understand? [closed]

The function of this apex code is when Account has no Contacts the first contact created become the primary contact and when there is existing primary contact it cannot assign a other primary contact ...
25
votes
1answer
439 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, ...
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 ...
14
votes
5answers
15k 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 ...
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 ...
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 ...
9
votes
1answer
717 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 ...
15
votes
2answers
10k 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. ...
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 ...
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 ...
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 = '...
5
votes
3answers
3k views

Insert List of Parent and Child Records in One DML Statement

I saw an example how to insert Parent and Child Record in one DML Statement from Developer Guide and It's Working Fine no issue in that. Here In my case I would like insert List Of Both Parent and ...
4
votes
2answers
2k views

Update ConversionRates for CurrencyType from Apex

I'm working on a small application in which I want the user to be able to enter a conversion rate on a VF page. The conversion rate should be saved in the CurrencyType object. I wanted to use ...
3
votes
2answers
13k views

Problem with Database.update()

I have some accounts to update but some validation rules can failed. So to avoid this I use the Database.update() function to update accounts even if some DML exception occur. try { List<...
3
votes
1answer
1k views

Sharing Records

I have an org that was recently updated to API 32, which broke some triggers that create Sharing Records. This code worked previously to the update to API 32. public void OnAfterObjectInsert(List<...
7
votes
2answers
915 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 ...
6
votes
2answers
577 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 ...
5
votes
1answer
5k views

Can't Insert User in Test Class

I've created a Trigger on the User object and I'm trying to test creation, update, deletion and undeletion of a User. Starting with Insert DML, I've got the following code: Profile p = [SELECT Id ...
4
votes
2answers
1k views

Database Class Methods

A noobie question. Is Database.Update bulkified by itself ?. I know it is similar to "update" but just wanna confirm that this alternate version of DML is auto bulkified. For example let us assume ...
4
votes
1answer
1k views

Connection was cancelled here, Interrupting long running request

Sometimes an Apex request gets interrupted/terminated by Force.com. The underlying Apex request contains numerous DML statments (insert into around 13 custom objects) all of which marked at a ...
3
votes
2answers
133 views

Links to Documentation for Database.* DML Method? Gone from APEX Docs

So we've got the usual DML statements insert accts; and we've got the database class dml statements, e.g. Database.insert(accts, false). Had a team member ask me a question about the parameter ...
3
votes
2answers
1k views

apex:Param not working with CommandButton component

I try to pass a parameter from Visualforce to the controller via apex:param and then upsert the object in the controller. However, the parameter I passed on, "Field__c" did not get written to the ...
2
votes
1answer
612 views

How to merge multiple records outside of a for loop? [duplicate]

I have a list of master accounts that each map to a single duplicate account. I want to merge each master with its corresponding duplicate using the merge DML operation. I'm currently doing this in a ...
2
votes
2answers
127 views

Trigger NullPointerException Error Before Update

I have a simple Before Update Trigger that I want to run when anything on the Accounting_Instructions__c is updated. I am not sure why my trigger is not working. I either receive a ...
2
votes
1answer
7k views

REQUIRED_FIELD_MISSING error when Approval Process set to Manually Select

I have an Approval Process that is programatically created. When the first (and only) approval step Assigned Approver is set to a specific user, or group of users, the process works fine. However, ...
2
votes
1answer
462 views

Is there a predetermined order that Database.SaveResult is returned?

Let's pretend the scenario below: List<Account> acc = new List<Account>{...}; List<Database.SaveResult> results = Database.insert(acc, false); Will the Lists have associated ...
1
vote
2answers
4k views

How to bypass ENTITY_IS_DELETED?

I have some apex as follows 1: List<Culture_TCH__c> oldCultures = [SELECT Id FROM Culture_TCH__c WHERE Assessment_TCH__c = :oldAssessment.Id]; 2: delete oldCultures; Simple enough, but this ...
1
vote
1answer
3k views

Calling a dml statement in a constructor of a controller

I understand that we cannot have dml statement in controllers constructor. But in my case, i have a query string which is used to run our pages. So my code reads the querystring variable in the ...
1
vote
2answers
2k views

Deploying an Apex Class for a Custom Button - Too Many DML Statements Error

I am attempting to deploy an Apex Class from a sandbox environment to a production environment of Salesforce and I am having issues with the deployment. I am very new at this, so please forgive my ...
1
vote
2answers
793 views

Apex After Update delete child then create new child

I have a simple trigger where when an opportunity is inserted, it creates a child object. If the opportunity is updated, it deletes all child objects and creates a new one. However, I receive an error ...
0
votes
1answer
372 views

Governor limits involving cascaded deletes?

I'm writing a job to bulk delete records from my database. I've already seen this question: Cascade delete limits? But it does tell me how the deletion limits are affected. The object whose records ...
0
votes
0answers
1k views

Too many DML statements: 151 - no DML in a for loop (that I can see)

I'm trying to build a newsletter manager that allows users to easily update and clear newsletter campaign members based on their subscription and region without having to run multiple reports. I've ...
0
votes
1answer
115 views

Schedule Iteration of Webservice Callouts

I have an endpoint I am getting updates from, but the only arguments the end point takes is two date parameters. To keep the heap size down, I am limited to about a date range of three months. I am ...