083_SFDC Limit(二) 及良好的开发习惯
关于常见开发中遇到的limit,应如何处理:https://developer.salesforce.com/page/Apex_Code_Best_Practices
链接内容很好,就不一一做解释
总结一下:在开发的过程中要养成好的习惯
常见的用法如 List<Account>, Account[]acc = new Account[]; ,Map,Set,List,要分清什么场合,用哪种比较合适,根据实际开发来决定
1.不能再loop中有DML操作
2. 尽量简化soql,比如 account,opportunity之间的查询,尽量合并再一个语句中
3.尽量一个Trigger 对应一个Handler
4.当准备批量操作一批数据时,尽量简化soql,接收值为Set、 map、list 等
5.@future (@future static method) ,如果考虑操作数据量较大,可以考虑后台空闲时执行,每个Apex 限制最多10个调用,系统24小时内限制200个
trigger accountAsyncTrigger on Account (after insert, after update) { //By passing the @future method a set of Ids, it only needs to be //invoked once to handle all of the data. asyncApex.processAccount(Trigger.newMap.keySet()); }
6.避免Hard coding方式去取数据,比如id = ‘1*********15’,尽量用名字或更多的条件检索到具体的数据,而不是直接拿ID来用
System.debug('Total Number of SOQL Queries allowed in this Apex code context: ' + Limits.getLimitQueries()); System.debug('Total Rows of SOQL Queries allowed in this Apex code context: ' + Limits.getLimitQueryRows()); System.debug('Total Number of DML records that can be operated(insert,update,delete等无select) in this Apex code context: ' + Limits.getLimitDmlRows()); System.debug('Total Number of DML statements allowed in this Apex code context: ' + Limits.getLimitDmlStatements() ); System.debug('Total Number of CPU usage time (in ms) allowed in this Apex code context: ' + Limits.getLimitCpuTime()); List<Account> acc = [Select id ,Name,Title_AZ_CN__c from Account where Name = 'Company ABC']; System.debug('Acc Size:'+acc.size()); System.debug('1. Number of Queries used in this Apex code so far: ' + Limits.getQueries()); System.debug('2. Rows of Queries used in this Apex code so far: ' + Limits.getQueryRows()); System.debug('3. Number of DML rows queried in this Apex code so far: ' + Limits.getDmlRows()); System.debug('4. Number of DML statements used so far: ' + Limits.getDmlStatements()); System.debug('5. Amount of CPU time (in ms) used so far: ' + Limits.getCpuTime()); List<Account> updateAcc = new List<Account>(); for(Account upacc: acc){ upacc.Title_AZ_CN__c = 'lead'; updateAcc.add(upacc); } update updateAcc; System.debug('11. Number of Queries used in this Apex code so far: ' + Limits.getQueries()); System.debug('22. Rows of Queries used in this Apex code so far: ' + Limits.getQueryRows()); System.debug('33. Number of DML rows queried in this Apex code so far: ' + Limits.getDmlRows()); System.debug('44. Number of DML statements used so far: ' + Limits.getDmlStatements()); System.debug('55. Amount of CPU time (in ms) used so far: ' + Limits.getCpuTime());
代码解读:
1-5: 是系统默认的限制,所有的限制都是在Limits 封装类中可以查到
8:实际检索List.size();
9:目前只有一个query结果
10:query结果为3条
11、12:DML Row:尚没有DML操作
13:CPU time
22:目前只有一个query结果
23:这个query结果为3条
24&25:有一个DML操作(update:对系统数据做了实际的更改,查询并不改变数据,因此不作为DML),这个DML操作结果是3条数据
26:CPU time
7. 比如页面输入一个值,后台进行查询:
String query = 'SELECT Id, Name FROM Account WHERE Name = \'' + userInputString + '\''; result = Database.execute(query);
但是这种写法 容易被恶意注入 乱码等其他,比如limit 1之类的,本来name 可以查出10条,加上limit 仅仅查出1条,那么这种写法就需要重新考虑
result = [SELECT Id, Name FROM Account WHERE Name = :userInputString];
选择这种方式可以避免
8 回滚
Savepoint sp1 = Database.setSavepoint(); Account a = new Account(); // 设置 Account 对象的属性 // ... insert a; Savepoint sp2 = Database.setSavepoint(); // 更改 Account 对象的属性 // ... update a // 回滚数据到 sp1 的状态 Database.rollback(sp1);
9. System.UserInfo.getUserId();
10.
Strin gs = Datetime.newInstance(DateField,Time.newInstance(0, 0, 0, 0)).format('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
HH 代表24小时制 hh代表12小时制
11.