DML的常用操作有:insert, delete, merge, undelete, update, upsert;
1、insert插入记录;
2、delete删除记录;
3、merge合并记录。合并语句将同一sObject类型的三个记录合并为一个记录,删除其他记录,并重新生成任何相关记录;
4、undelete撤销删除项;
5、update更新已存在的记录;
6、upsert==insert and/or update。Upsert帮助避免创建重复记录,并且可以节省您的时间,因为您不必首先确定哪些记录存在;
注意:
1、常对2-6的操作使用try{operation;}catch(DmlException e){}
operation后面需要使用分号,catch的{}后面不需要分号,否则报错;
2、注意在使用dml查询语句时,查询的结果是记录数组,所以存储结果对象需要是数组类型,当然如果有limit 1限制则不需要是数组类型;
eg:Sobject[] obj = [SELECT ... FROM ... WHERE ... ALL ROWS];/Sobject[] obj = [SELECT ... FROM ... WHERE ...];即使查询的数据只有一条。
或者Sobject obj = [SELECT ... FROM ... WHERE ... LIMIT 1];
3、可以使用obj.key来取值,如果是数组类型则obj[i].key来取值;
4、在使用merge语句时,需要区别哪一个是masterObj(前),哪一个是mergeObj(后),二者有区别。处理结果是保留复合前面规则的记录,删除符合后面的规则的记录。
代码片段1:目的是为Account创建两条只包含Name的记录,然后更新其中一条记录的phone,最后合并两条记录merge acct a2。
/*插入记录*/
List<Account> la = new List<Account>{
new Account(Name='Acme Inc.'),
new Account(Name='Acme')
};
insert la;
System.debug(la.get(0).Name+'==Acme Inc.');
/*更新记录*/
Account acct = [select Id,Name,Phone from Account where Name='Acme' limit 1];
acct.Phone = '13554345439';
try{update acct;}catch(DmlException e){}
System.debug('Name: '+acct.Name+', Phone: '+acct.Phone);
/*合并记录*/
Account a2 = [select Id,Name,Phone from Account Where Name='Acme Inc.'];
System.debug('Name: '+a2.Name+', Phone: '+a2.Phone);
try{merge acct a2;}catch(DmlException e){}//以acct合并a2
代码片段2:目的是为Account创建两条只包含Name的记录,然后更新其中一条记录的phone,最后合并两条记录merge a2 acct。
/*插入记录*/
List<Account> la = new List<Account>{
new Account(Name='Acme Inc.'),
new Account(Name='Acme')
};
insert la;
System.debug(la.get(0).Name+'==Acme Inc.');
/*更新记录*/
Account acct = [select Id,Name,Phone from Account where Name='Acme' limit 1];
acct.Phone = '13554345439';
try{update acct;}catch(DmlException e){}
System.debug('Name: '+acct.Name+', Phone: '+acct.Phone);
/*合并记录*/
Account a2 = [select Id,Name,Phone from Account Where Name='Acme Inc.'];
System.debug('Name: '+a2.Name+', Phone: '+a2.Phone);
try{merge a2 acct;}catch(DmlException e){}//以a2合并acct
批量操作记录代码实例:
/*
目的:插入4条Contact记录,并将Department为Finace的记录更新字段Title为Finance analyst
*/
List<Contact> conList = new List<Contact>{
new Contact(FirstName='Jay',LastName='Chou',Department='Finance'),
new Contact(FirstName='Cahno',LastName='Lee',Department='Finance'),
new Contact(FirstName='Smith',LastName='Hunter',Department='IT'),
new Contact(FirstName='Jack',LastName='Zhang',Department='Education')
};
insert conList;
List<Contact> newCon = new List<Contact>();
for(Contact con : conList){
if(con.Department=='Finance'){
con.Title='Finance analyst';
newCon.add(con);
}
}
try{update newCon;}catch(DmlException e){}
/*更新记录实例*/Contact jane = new Contact(FirstName='Jane', LastName='Smith', Email='jane.smith@example.com', Description='Contact of the day');insert jane;// 1. Upsert using an idLookup field// Create a second sObject variable.// This variable doesn’t have any ID set.Contact jane2 = new Contact(FirstName='Jane', LastName='Smith', Email='jane.smith@example.com', Description='Prefers to be contacted by email.');// Upsert the contact by using the idLookup field for matching.upsert jane2 Contact.fields.Email;// Verify that the contact has been updatedSystem.assertEquals('Prefers to be contacted by email.', [SELECT Description FROM Contact WHERE Id=:jane.Id].Description);
/*使用Database内置对象操作数据实例*/List<Contact> conList = new List<Contact>{ new Contact(FirstName='小白',LastName='江',Department='Finance'), new Contact(FirstName='小琴',LastName='毛',Department='IT'), new Contact(FirstName='小亮',LastName='胡',Department='IT')};Database.SaveResult[] results = Database.insert(conList,false);//fase表示不允许部分成功插入for(Database.SaveResult res : results){ if(res.isSuccess()){ System.debug('Successfully inserted contacts. Contact ID: '+res.getId()); }else{ for(Database.Error err : res.getErrors()){ System.debug('The following error has occurred.'); System.debug(err.getStatusCode() + ': ' + err.getMessage()); System.debug('Contact fields that affected this error: ' + err.getFields()); } }}