Get Record(s) by Ids
来源:互联网 发布:java的数据类型 编辑:程序博客网 时间:2024/06/04 19:50
Use this to get record or records by id.
/** * Created by mengt on 2017/4/18. */public class sobjectDaoHelper { public static Sobject getById(Id recordId){ String queryStr = 'select '; String sObjectName = sobjectDaoHelper.findObjectNameFromRecordIdPrefix(recordId); if(sObjectName == ''|| sObjectName == null){ return null;} List<Schema.DescribeSObjectResult> sObjectResults = Schema.describeSObjects(new List<String>{sObjectName}); if(sObjectResults == null || sObjectResults.size() == 0) { return null; } Schema.DescribeSObjectResult sObjectResult = sObjectResults.get(0); Map<String,SObjectField> maps = sObjectResult.fields.getMap(); for(Schema.SObjectField objectField : maps.values()) { Schema.DescribeFieldResult fieldResult = objectField.getDescribe(); if(fieldResult.isAccessible()) { queryStr += fieldResult.getName() + ','; } } queryStr = queryStr.substring(0,queryStr.length()-1); queryStr += ' from ' + sObjectName + ' where Id = :recordId'; List<sObject> objects = Database.query(queryStr); if(objects == null || objects.size() == 0) { return null; } return objects.get(0); } /* 可优化方案:循环查找perfixkey,可用map替换,在数据操作过大的时候计算时间会有所提高 List<Id>限制 50000 List对象限制 100 */ public static String findObjectNameFromRecordIdPrefix(String recordIdOrPrefix){ String objectName = ''; try{ //Get prefix from record ID //This assumes that you have passed at least 3 characters String myIdPrefix = String.valueOf(recordIdOrPrefix).substring(0,3); //Get schema information Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); //Loop through all the sObject types returned by Schema for(Schema.SObjectType stype : gd.values()){ Schema.DescribeSObjectResult r = stype.getDescribe(); String prefix = r.getKeyPrefix(); //Check if the prefix matches with requested prefix if(prefix!=null && prefix.equals(myIdPrefix)){ objectName = r.getName(); break; } } }catch(Exception e){ System.debug(e); } return objectName; } public static Map<String,List<SObject>> getByIds(List<Id> recordIds){ if(recordIds == null || recordIds.size()>50000){ return null; } Map<String,List<Id>> mapIds = new Map<String,List<Id>>(); for (Id thisid : recordIds) { String objectType = findObjectNameFromRecordIdPrefix(thisid); if(objectType == '' || objectType == null){ continue; } if(!mapIds.containsKey(objectType)){ mapIds.put(objectType,new List<Id>()); } mapIds.get(objectType).add(thisid); } if(mapIds.keySet().size() > 100){ return null; } Map<String,List<SObject>> returnMap = new Map<String,List<SObject>>(); List<String> objectNames = new List<String>(); objectNames.addAll(mapIds.keySet()); List<Schema.DescribeSObjectResult> sObjectResults = Schema.describeSObjects(objectNames); for (DescribeSObjectResult sObjectResult : sObjectResults) { String queryStr = 'select '; Map<String,SObjectField> maps = sObjectResult.fields.getMap(); for(Schema.SObjectField objectField : maps.values()) { Schema.DescribeFieldResult fieldResult = objectField.getDescribe(); if(fieldResult.isAccessible()) { queryStr += fieldResult.getName() + ','; } } List<Id> queryIds = mapIds.get(sObjectResult.getName()); queryStr = queryStr.substring(0,queryStr.length()-1); queryStr += ' from ' + sObjectResult.getName() + ' where Id = :queryIds'; returnMap.put(sObjectResult.getName(),Database.query(queryStr)); } return returnMap; }}
0 0
- Get Record(s) by Ids
- How to Get Table’s Generated Ids
- How to get new record's id
- Record change by another
- Record wireshark lua's bugs found by myself and some suggestion
- S-Record格式详解
- MOTOROLA S-RECORD文件格式
- MOTOROLA S-RECORD文件格式
- S-Record格式详解
- S-Record格式详解
- MOTOROLA S-RECORD文件格式 .
- S-Record格式详解
- Get process's navigation path by using process name
- Get process's navigation path by using process name
- Locale IDs Assigned by Microsoft (zz)
- S-Record格式分析代码
- S-Record 文件解析器
- S-Record格式分析代码
- java 如果一个变量是double或者float,如何初始化为0?
- Grid图片加载
- YV12格式介绍
- Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400
- ROS环境变量的设置
- Get Record(s) by Ids
- HDU 1242 Rescue
- 快速排序详解
- margin布局居中显示
- android中的broadcastReceiver
- ubuntu 下 vim安装和配置
- asdfghj
- 内存管理浅谈
- fio 磁盘I/O测试