Proposed Rule Name: OperationWithHighCostInLoop
Proposed Category: Performance
Description:
Methods Schema.getGlobalDescribe() or Schema.describeSObjects should be called once per an apex transaction and should never be called inside a loop.
Code Sample: This should include code, that should be flagged by the rule. If possible, the "correct" code
according to this new rule should also be demonstrated.
//to avoid Set<String> fieldNameSet = new Set<String> {'Id'}; for (String fieldNameOrDefaultValue : fieldNameOrDefaultValueList) { if (Schema.getGlobalDescribe().get(objectName).getDescribe().fields.getMap().containsKey(fieldNameOrDefaultValue.trim() )) { fieldNameSet.add(fieldNameOrDefaultValue); } } //correct Set<String> fieldNameSet = new Set<String> {'Id'}; Map<String, Schema.SObjectField> fieldMap = Schema.getGlobalDescribe().get(objectName).getDescribe().fields.getMap(); for (String fieldNameOrDefaultValue : fieldNameOrDefaultValueList) { if (fieldMap.containsKey(fieldNameOrDefaultValue.trim())) { fieldNameSet.add(fieldNameOrDefaultValue); } }
Additional Information:
See also: Why is Schema.describeSObjects(types) Slower Than Schema.getGlobalDescribe()?
Possible Properties:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4