Apex Data Manipulation Language (DML) Operations
|  | Description | Example | 
| delete | Deletes one or more records | 
Account[] doomedAccts =
  [select id, name
   from account where name = 'DotCom'];
try {
  delete doomedAccts;
} catch (DmlException e) {
  // Process exception here
} | 
| insert | Adds one or more records | 
Lead l = new Lead(
  company='ABC',
  lastname='Smith');
insert l; | 
| merge | Merges up to three records of the same type into one of the records, deleting the others, and re-parenting any related records | 
List ls =
  new List{
    new Account(name='Acme Inc.'),
    new Account(name='Acme')};
insert ls;
Account masterAcct =
  [select id, name
   from account
   where name = 'Acme Inc.'
   limit 1];
Account mergeAcct =
  [select id, name
   from account
   where name = 'Acme'
   limit 1];
   
try {
  merge masterAcct mergeAcct;
} catch (DmlException e) {
  // Process exception here
} | 
| undelete | Restores one or more records from the recycle bin | 
Account[] savedAccts =
  [select id, name
   from account
   where name = 'Trump'
   ALL ROWS];
   
try {
  undelete savedAccts;
} catch (DmlException e) {
  // Process exception here
} | 
| update | Modifies one or more existing records | 
Account a = new Account(name='Acme2');
insert(a);
Account myAcct =
  [select id, name, billingcity
   from account
   where name = 'Acme2'
   limit 1];
myAcct.billingcity = 'San Francisco';
try {
  update myAcct;
} catch (DmlException e) {
  // jump up and down
} | 
| upsert | Creates new records and updates existing records | 
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) {
  // Process exception here
}
 | 
 
0 comments:
Post a Comment