Activity › Forums › Salesforce® Discussions › How can I manipulate records with DML in Salesforce?
-
How can I manipulate records with DML in Salesforce?
Posted by Abhishek on May 23, 2018 at 10:23 AMHow can I manipulate records with DML in Salesforce?
shariq replied 7 years, 7 months ago 4 Members · 3 Replies -
3 Replies
-
Hi Abhishek,
Create and modify records in Salesforce by using the Data Manipulation Language, abbreviated as DML. DML provides a straightforward way to manage records by providing simple statements to insert, update, merge, delete, and restore records.For Example:
// Create the account sObject
Account acct = new Account(Name=’Acme’, Phone='(415)555-1212′, NumberOfEmployees=100);
// Insert the account by using DML
insert acct;You can practise it on Salesforce Trailhead under Manipulate Records with DML topic for better understanding.
Hope this helps you.
- [adinserter block='9']
-
DML operations allow you to modify records one at a time or in batches. DML are the actions which are performed in order to perform insert, update, delete, upsert, delete & undelete. You can perform DML operations either on a single sObject, or in bulk on a list of sObjects.
Followings are DML actions
• insert: Use this statement to create the records of sObject.
• update: Use this statement to update the existing records of sObject.
• upsert: Use this statement to create the new record or update the existing records on the basis of Id or external field.
• delete : Use this statement to delete the existing record
• undelete : Use this statement to restore the records from Organization recycle bin.
• merge : Use this statement to merge the records of same sObject type by deleting other and re-parenting the related records. You can use up to 3 records to merge.
Sample Code
Account[] acctsList = [SELECT Id, Name, BillingCity
FROM Account WHERE BillingCity = ‘Bombay’];
for (Account a : acctsList) {
a.BillingCity = ‘Mumbai’;
}
Account newAcct = new Account(Name = ‘Acme’, BillingCity = ‘San Francisco’);
acctsList.add(newAcct);
try {
upsert acctsList;
} catch (DmlException e) {
System.debug(e.ErrorMessage());
// Process exception here
}
-
Hi,
Manipulate Records with DML
Use DML to insert, update, and delete records.
Perform DML statements in bulk.
Use upsert to either insert or update a record.
Catch a DML Exception.
Use a Database method to insert new records with the partial success option and process the results.
Know when to use DML statements and when to use Database methods.Hope this helps.
Log In to reply.