我们都知道,报表有个功能为导出excel,但是有的时候客户需求往往标准的报表达不到,比如导出excel,其中本月修改的数据字段标红,如下图所示。
这就需要我们去写VF来实现此功能。
需求:将数据表记录导出成excel,其中excel内容需要本月修改的数据字段自动标红显示。
表:Goods__c,字段如下:
设计思路:如果导出excel并且需要跟踪每个字段的修改时间进行校验是否标红,则需要有一个表取跟踪这个表.有两种方式进行Track。
1.salesforce中提供了Track History功能,即当表字段小于20个情况下,可以通过设置Track History,那样系统会自动创建相关表的History表,在这个demo中系统会自动创建Goods_History表。详细Track History知识可以参看以下链接:
2.当表的字段超过20个,则通过Track History便无法满足需求了,这种情况我们需要自己创建一个sObject来和需要track的sObject进行关联,如下图所示:
页面显示均使用标准生成的页面,在Goods__c修改情况下,需要设置Trigger自动插入此条Goods__c记录对应的GoodsSign__c记录,如果某个字段有修改,则设置某个字段相对应的Date信息为System.today().
制作步骤:
1.写GoodsTrigger.Trigger代码如下所示:
trigger GoodsTrigger on Goods__c (before delete, before update) {
if(trigger.isBefore) {
//get the Schema Information of GoodsSign to check whether current user has access to operate the data
Schema.DescribeSObjectResult goodsSignDescribe = GoodsSign__c.sObjectType.getDescribe();
List<GoodsSign__c> goodsSignList = new List<GoodsSign__c>();
//第一个参数为Goods__c的ID,第二个参数为GoodsSign__c
Map<ID,GoodsSign__c> goodsIdToGoodsSignMap = new Map<ID,GoodsSign__c>();
if(trigger.isUpdate) {
Set<ID> goodsIdSet = new Set<ID>();
if(goodsSignDescribe.isCreateable() && goodsSignDescribe.isUpdateable()) {
List<Goods__c> goodsNewList = trigger.new;
List<Goods__c> goodsOldList = trigger.old;
for(Goods__c goods : goodsNewList) {
goodsIdSet.add(goods.Id);
}
String fetchGoodsSignByGoodsId = 'SELECT CreatedById, CreatedDate,' +
' IsDeleted, GoodsBrandDate__c, GoodsCostPriceDate__c,' +
' GoodsDescribeDate__c, GoodsId__c, GoodsNameDate__c,' +
' GoodsPriceDate__c, Name, LastModifiedById, LastModifiedDate,' +
' OwnerId, Id, SystemModstamp FROM GoodsSign__c ' +
' where GoodsId__c in :goodsIdSet';
List<GoodsSign__c> tempGoodsSignList = Database.query(fetchGoodsSignByGoodsId);
for(GoodsSign__c goodsSign : tempGoodsSignList) {
goodsIdToGoodsSignMap.put(goodsSign.GoodsId__c,goodsSign);
}
for(Integer i=0;i<goodsNewList.size();i++) {
Goods__c goodsNew = goodsNewList.get(i);
Goods__c goodsOld = goodsOldList.get(i);
GoodsSign__c goodsSign = new GoodsSign__c();
Id goodsId = goodsNew.Id;
if(goodsIdToGoodsSignMap.get(goodsId) != null) {
goodsSign = goodsIdToGoodsSignMap.get(goodsId);
} if(goodsNew.GoodsName__c != goodsOld.GoodsName__c) {
goodsSign.GoodsNameDate__c = System.today();
}
if(goodsNew.GoodsPrice__c != goodsOld.GoodsPrice__c) {
goodsSign.GoodsPriceDate__c = System.today();
}
if(goodsNew.GoodsCostPrice__c != goodsOld.GoodsCostPrice__c) {
goodsSign.GoodsCostPriceDate__c = System.today();
}
if(goodsNew.GoodsBrand__c != goodsOld.GoodsBrand__c) {
goodsSign.GoodsBrandDate__c = System.today();
}
if(goodsNew.GoodsDescribe__c != goodsOld.GoodsDescribe__c) {
goodsSign.GoodsDescribeDate__c = System.today();
}
if(goodsSign.GoodsId__c == null) {
goodsSign.GoodsId__c = goodsId;
}
goodsSignList.add(goodsSign);
}
if(goodsSignList.size() > 0) {
upsert goodsSignList;
}
} } else if(trigger.isDelete) {
//cascade delete
if(goodsSignDescribe.isDeletable()) {
List<Goods__c> goodsList = trigger.old;
Set<ID> goodsIdSet = new Set<ID>();
for(Goods__c currentGoods : goodsList) {
if(!goodsIdSet.contains(currentGoods.Id)) {
goodsIdSet.add(currentGoods.Id);
}
}
String fetchGoodsSignByGoodsIdSet = 'SELECT CreatedById, CreatedDate,' +
' IsDeleted, GoodsBrandDate__c, GoodsCostPriceDate__c, GoodsDescribeDate__c,' +
' GoodsId__c, GoodsNameDate__c, GoodsPriceDate__c, Name, LastModifiedById,' +
' LastModifiedDate, OwnerId, Id, SystemModstamp FROM GoodsSign__c' +
' where GoodsId__c in :goodsIdSet';
List<GoodsSign__c> goodsSignNeedDeleteList = Database.query(fetchGoodsSignByGoodsIdSet);
delete goodsSignNeedDeleteList;
}
}
}
}
此Trigger有两个功能:
1.当进行修改操作并且Goods__c记录有字段改变时,如果有相对应的GoodsSign__c进行对应,则update此记录,否则新建记录,并记录哪些字段有修改;
2.当进行删除操作时,如果有相对应的GoodsSign__c进行对应,则级联删除。
两者操作均需要当前用户有GoodsSign的操作权限。
2.新建一个类用来记录导出的字段以及导出的字段的颜色。
public with sharing class GoodsExportObject {
public String goodsName{get;set;}
public String goodsNameColor{get;set;}
public String goodsBrand{get;set;}
public String goodsBrandColor{get;set;}
public String goodsPrice{get;set;}
public String goodsPriceColor{get;set;}
public String goodsCostPrice{get;set;}
public String goodsCostPriceColor{get;set;}
public String goodsDescribe{get;set;}
public String goodsDescribeColor{get;set;}
}
3.新建Controller,此Controller用来获取显示到excel的数据。
public with sharing class ExportGoodsController { List<Goods__c> goodsList{get;set;}
List<GoodsSign__c> goodsSignList{get;set;}
public List<GoodsExportObject> exportGoodsList{get;set;}
Map<Id,GoodsSign__c> goodsSignMap = new Map<Id,GoodsSign__c>();
public ExportGoodsController() {
goodsList = new List<Goods__c>();
goodsSignList = new List<GoodsSign__c>();
exportGoodsList = new List<GoodsExportObject>();
String fetchAllGoods = 'SELECT CreatedById, CreatedDate, IsDeleted,' +
' Name, GoodsBrand__c, GoodsCostPrice__c, GoodsDescribe__c, GoodsName__c,' +
' GoodsPrice__c, LastActivityDate, LastModifiedById, LastModifiedDate,' +
' OwnerId, Id, SystemModstamp FROM Goods__c';
goodsList = Database.query(fetchAllGoods);
String fetchAllGoodsSign = 'SELECT CreatedById, CreatedDate, IsDeleted,' +
' GoodsBrandDate__c, GoodsCostPriceDate__c, GoodsDescribeDate__c,' +
' GoodsId__c, GoodsNameDate__c, GoodsPriceDate__c, Name, LastModifiedById,' +
' LastModifiedDate, OwnerId, Id, SystemModstamp FROM GoodsSign__c';
goodsSignList = Database.query(fetchAllGoodsSign);
for(GoodsSign__c goodsSign : goodsSignList) {
if(!goodsSignMap.containsKey(goodsSign.GoodsId__c)) {
goodsSignMap.put(goodsSign.GoodsId__c,goodsSign);
}
}
} public PageReference exportGoods() {
String bgColor = 'red';
Integer nowMonth = System.today().month();
for(Goods__c currentGoods : goodsList) {
GoodsSign__c goodsSign = goodsSignMap.get(currentGoods.Id);
GoodsExportObject tempGoodsExportObject = new GoodsExportObject();
tempGoodsExportObject.goodsName = currentGoods.GoodsName__c;
tempGoodsExportObject.goodsBrand = currentGoods.GoodsBrand__c;
tempGoodsExportObject.goodsPrice = String.valueOf(currentGoods.GoodsPrice__c);
tempGoodsExportObject.goodsCostPrice = String.valueOf(currentGoods.GoodsCostPrice__c);
tempGoodsExportObject.goodsDescribe = currentGoods.GoodsDescribe__c;
if(goodsSign != null) {
if(goodsSign.GoodsNameDate__c != null && goodsSign.GoodsNameDate__c.month() == nowMonth) {
tempGoodsExportObject.goodsNameColor = bgColor;
}
if(goodsSign.GoodsBrandDate__c != null && goodsSign.GoodsBrandDate__c.month() == nowMonth) {
tempGoodsExportObject.goodsBrandColor = bgColor;
}
if(goodsSign.GoodsPriceDate__c != null && goodsSign.GoodsPriceDate__c.month() == nowMonth) {
tempGoodsExportObject.goodsPriceColor = bgColor;
}
if(goodsSign.GoodsCostPriceDate__c != null && goodsSign.GoodsCostPriceDate__c.month() == nowMonth) {
tempGoodsExportObject.goodsCostPriceColor = bgColor;
}
if(goodsSign.GoodsDescribeDate__c != null && goodsSign.GoodsDescribeDate__c.month() == nowMonth) {
tempGoodsExportObject.goodsDescribeColor = bgColor;
}
}
exportGoodsList.add(tempGoodsExportObject);
} return new PageReference('/apex/ExportGoodsPage');
} }
4.新建显示的VF页面
IsExportPage.page:此VF页面用于显示一个按钮,当点击按钮后,执行Excel生成操作。
<apex:page controller="ExportGoodsController">
<apex:form >
<apex:commandButton action="{!exportGoods}" value="exportGoods"/>
</apex:form>
</apex:page>
ExportGoodsPage:生成Excel
<apex:page controller="ExportGoodsController" cache="true" contenttype="application/x-excel# GenExcel.xls" showheader="false">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
</head>
<apex:dataTable value="{!exportGoodsList}" var="exportGoods" border="1">
<apex:column style="background:{!exportGoods.goodsNameColor};">
<apex:facet name="header">Goods Name </apex:facet>
{!exportGoods.goodsName}
</apex:column>
<apex:column style="background:{!exportGoods.goodsBrandColor};">
<apex:facet name="header">Goods Brand</apex:facet>
{!exportGoods.goodsBrand}
</apex:column>
<apex:column style="background:{!exportGoods.goodsPriceColor};">
<apex:facet name="header">Goods Price</apex:facet>
{!exportGoods.goodsPrice}
</apex:column>
<apex:column style="background:{!exportGoods.goodsCostPriceColor};">
<apex:facet name="header">Goods Cost Price</apex:facet>
{!exportGoods.goodsCostPrice}
</apex:column>
<apex:column style="background:{!exportGoods.goodsDescribeColor};">
<apex:facet name="header">Goods Describe</apex:facet>
{!exportGoods.goodsDescribe}
</apex:column>
</apex:dataTable>
</apex:page>
5.配置Button,并显示到列表页面上。
结果样式显示:
点击Goods Reports按钮,跳转到导出 记录的按钮页面
点击exportGoods则可以生成Excel。以下为Excel的生成界面,其中红色为修改的记录字段。
总结:上述demo只是演示当字段Tracking超过20个需要额外创建表的情况处理,当小于20个情况下可以直接通过History的表进行查询,有兴趣的可以自己尝试,生成页面因为使用DataTable,所以对于导出的记录行数有要求,必须不大于1000条,超过则应该会报Error。篇中如果有写的错误的地方欢迎指出,如果有疑问地方欢迎留言,转载请注明出处。