Tuesday 12 March 2013


Apex Data Manipulation Language (DML) Operations
 DescriptionExample
deleteDeletes one or more records
Account[] doomedAccts =
  [select id, name
   from account where name = 'DotCom'];

try {
  delete doomedAccts;
} catch (DmlException e) {
  // Process exception here
}
insertAdds one or more records
Lead l = new Lead(
  company='ABC',
  lastname='Smith');

insert l;
mergeMerges 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
}
undeleteRestores 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
}
updateModifies 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
}
upsertCreates 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
}

Categories: ,

0 comments:

Post a Comment

    Links