salesforce 零基础学习(五十六)实现getById

本来想通过template封装DAO中的getById,结果template中无法选择$(object_name),所以此种想法打消了,直接封装成一个Helper类,方便以后项目中如果有类似需要可以使用。

 1 public virtual with sharing class ObjectDAOHelper {
 2     /*
 3     *    @param sObjectName   sobject api name
 4     *    @param id              sobject record id
 5     *    return 返回此记录ID,如果不存在返回null
 6     */
 7     public static sObject getById(String sObjectName,String id) {
 8         String queryStr = 'select ';
 9         List<Schema.DescribeSObjectResult> sObjectResults = Schema.describeSObjects(new List<String>{sObjectName});
10         if(sObjectResults == null || sObjectResults.size() == 0) {
11             return null;
12         }
13         Schema.DescribeSObjectResult sObjectResult = sObjectResults.get(0);
14         Map<String,SObjectField> maps = sObjectResult.fields.getMap();
15         Integer i = 0;
16         for(Schema.SObjectField objectField : maps.values()) {
17             Schema.DescribeFieldResult fieldResult = objectField.getDescribe();
18             if(fieldResult.isAccessible()) {
19                 queryStr += fieldResult.getName();
20                 if(i != maps.keySet().size() - 1) {
21                     queryStr += ',';
22                 }
23                 i++;
24             }
25         }
26         queryStr += ' from ' + sObjectName + ' where Id = :id';
27         system.debug('queryStr : ' + queryStr);
28         List<sObject> objects = Database.query(queryStr);
29         if(objects == null || objects.size() == 0) {
30             return null;
31         }
32         return objects.get(0);
33     }
34 }

运行代码:

 结果展示:

queryStr : select Id,OwnerId,IsDeleted,Name,RecordTypeId,CreatedDate,CreatedById,LastModifiedDate,LastModifiedById,SystemModstamp,LastViewedDate,LastReferencedDate,GoodsName__c,GoodsBrand__c,GoodsPrice__c,GoodsCostPrice__c,GoodsPicture__c,Status__c,Goods_Code_Unique__c,GoodsDescribe__c,GoodsProfit__c,No__c from Goods__c where Id = :id

{
  "attributes" : {
    "type" : "Goods__c",
    "url" : "/services/data/v38.0/sobjects/Goods__c/a052800000Ejg1vAAB"
  },
  "Id" : "a052800000Ejg1vAAB",
  "OwnerId" : "00528000002JyclAAC",
  "IsDeleted" : false,
  "Name" : "a052800000Ejg1v",
  "RecordTypeId" : "01228000000U1u0AAC",
  "CreatedDate" : "2016-12-13T07:16:03.000+0000",
  "CreatedById" : "00528000002JyclAAC",
  "LastModifiedDate" : "2016-12-13T07:16:03.000+0000",
  "LastModifiedById" : "00528000002JyclAAC",
  "SystemModstamp" : "2016-12-13T07:16:03.000+0000",
  "GoodsName__c" : "测试商品1",
  "GoodsBrand__c" : "其他",
  "GoodsPrice__c" : 200.000000,
  "GoodsCostPrice__c" : 100.000000,
  "Goods_Code_Unique__c" : "GC00001",
  "GoodsDescribe__c" : "测试描述",
  "GoodsProfit__c" : 100.00,
  "No__c" : "2016-142"
}

总结:简单的helper类以及方法,篇中有问题的欢迎指出,不懂得欢迎留言。

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏张善友的专栏

LINQ via C# 系列文章

LINQ via C# Recently I am giving a series of talk on LINQ. the name “LINQ via C...

3565
来自专栏闻道于事

js登录滑动验证,不滑动无法登陆

js的判断这里是根据滑块的位置进行判断,应该是用一个flag判断 <%@ page language="java" contentType="text/html...

1.1K8
来自专栏Ceph对象存储方案

Luminous版本PG 分布调优

Luminous版本开始新增的balancer模块在PG分布优化方面效果非常明显,操作也非常简便,强烈推荐各位在集群上线之前进行这一操作,能够极大的提升整个集群...

4505
来自专栏落花落雨不落叶

canvas画简单电路图

1.1K12
来自专栏hbbliyong

WPF Trigger for IsSelected in a DataTemplate for ListBox items

<DataTemplate DataType="{x:Type vm:HeaderSlugViewModel}"> <vw:HeaderSlug...

4394
来自专栏张善友的专栏

Miguel de Icaza 细说 Mix 07大会上的Silverlight和DLR

Mono之父Miguel de Icaza 详细报道微软Mix 07大会上的Silverlight和DLR ,上面还谈到了Mono and Silverligh...

3277
来自专栏跟着阿笨一起玩NET

c#实现打印功能

4832
来自专栏张善友的专栏

Mix 10 上的asp.net mvc 2的相关Session

Beyond File | New Company: From Cheesy Sample to Social Platform Scott Hansel...

2997
来自专栏张善友的专栏

Silverlight + Model-View-ViewModel (MVVM)

     早在2005年,John Gossman写了一篇关于Model-View-ViewModel模式的博文,这种模式被他所在的微软的项目组用来创建Expr...

3828
来自专栏转载gongluck的CSDN博客

cocos2dx 打灰机

#include "GamePlane.h" #include "PlaneSprite.h" #include "BulletNode.h" #include...

9006

扫码关注云+社区