salesforce 零基础学习(六十)Wizard样式创建数据
Posted zero.zhang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了salesforce 零基础学习(六十)Wizard样式创建数据相关的知识,希望对你有一定的参考价值。
项目中表之间关联关系特别多,比如三个表中A,B,C C作为主表,A,B作为从表,有时候C表需要创建数据时,同时需要创建A,B两个表的数据,这种情况下,使用Wizard样式会更加友好。
以Goods__c表和Goods_Vendor__c表为例,Goods__c为主表,Goods_Vendor__c为从表。新建Goods__c记录以后同时要创建其相关的数据。
表结构关系如下:
代码:
1.GoodsHelper:封装获取goods的列表方法
1 public without sharing class GoodsHelper { 2 3 public static final String BASE_GOODS_QUERY = \'SELECT CreatedById, CreatedDate, IsDeleted,\' + 4 \' Goods_Code_Unique__c, Name, GoodsPicture__c, GoodsBrand__c, GoodsCostPrice__c,\' + 5 \' GoodsDescribe__c, GoodsName__c, GoodsPrice__c, GoodsProfit__c, Is_Draft__c,\' + 6 \' LastModifiedById, LastModifiedDate, No__c, OwnerId, Id, RecordTypeId,\' + 7 \' Status__c, SystemModstamp\' + 8 \' FROM Goods__c where IsDeleted = false\'; 9 public static final String BASE_GOODS_COUNT_QUERY = \'SELECT count() from Goods__c where IsDeleted = false\'; 10 public static MyPaginationEnhancement getGoodsList(String goodsName,String goodsBrand,MyPaginationEnhancement pagination) { 11 String queryCondition= \'\'; 12 String orderBy =\'\'; 13 if(goodsName != null) { 14 queryCondition += \' and GoodsName__c like %\\\'\' + goodsName + \'%\\\'\'; 15 } 16 if(goodsBrand != null) { 17 queryCondition += \' and GoodsBrand__c = :goodsBrand\'; 18 } 19 orderBy = \' order by createddate\'; 20 21 pagination.getQueryResult(BASE_GOODS_COUNT_QUERY,BASE_GOODS_QUERY,queryCondition,null,orderBy); 22 return pagination; 23 } 24 }
2.GoodsListController:Goods列表Controller
1 public with sharing class GoodsListController { 2 public Map<String,String> parameters; 3 4 public GoodsListController() { 5 parameters=ApexPages.currentPage().getParameters(); 6 init(); 7 } 8 9 public MyPaginationEnhancement pagination = new MyPaginationEnhancement(); 10 11 public String goodsName{get;set;} 12 13 public String goodsBrand{get;set;} 14 15 16 public void init() { 17 queryByCondition(); 18 } 19 20 public void queryByCondition() { 21 GoodsHelper.getGoodsList(goodsName,goodsBrand,pagination); 22 } 23 24 public MyPaginationEnhancement resultPagination{ 25 get{ 26 if(pagination ==null){ 27 pagination =new MyPaginationEnhancement(); 28 } 29 return pagination; 30 } 31 set; 32 } 33 34 public List<Goods__c> resultList{ 35 get{ 36 if(pagination==null || pagination.resultList==null){ 37 return new List<Goods__c>(); 38 } 39 return pagination.resultList; 40 } 41 set; 42 } 43 44 public PageReference newGoods() { 45 return Page.detailGoods; 46 } 47 48 public void firstPage() { 49 pagination.first(); 50 queryByCondition(); 51 } 52 53 public void lastPage() { 54 pagination.last(); 55 queryByCondition(); 56 } 57 58 public void previousPage() { 59 pagination.previous(); 60 queryByCondition(); 61 } 62 63 public void nextPage() { 64 pagination.next(); 65 queryByCondition(); 66 } 67 }
3.GoodsListPage
1 <apex:page controller="GoodsListController"> 2 <style> 3 /*-- 分页 --*/ 4 .paginator {font:12px Arial, Helvetica, sans-serif; padding:10px 0; margin:0px;} 5 .paginator a {padding:1px 6px;border:solid 1px #ddd;background:#fff;color:#000;text-decoration:none;margin-right:2px;} 6 .paginator a:visited {padding:1px 6px;border:solid 1px #ddd;background:#fff;text-decoration:none;} 7 .paginator .current {padding:1px 6px; font-weight:bold; color:#f0ab00; font-size:12px; border:none;} 8 .paginator a:hover {color:#fff; background:#ffa501; border-color:#ffa501; text-decoration:none;} 9 10 </style> 11 12 <apex:form > 13 <apex:commandButton value="新建商品" action="{!newGoods}"/> 14 <apex:outputPanel layout="block"> 15 <apex:outputPanel layout="block"> 16 <apex:outputPanel layout="block"> 17 <apex:outputPanel layout="block" id="goodsList"> 18 <apex:dataTable align="center" value="{!resultList}" var="goods"> 19 <apex:column style="width:180px;"> 20 <apex:facet name="header">{!$ObjectType.Goods__c.fields.GoodsName__c.label}</apex:facet> 21 <apex:outputText value="{!goods.GoodsName__c}" /> 22 </apex:column> 23 <apex:column style="width:225px;"> 24 <apex:facet name="header">{!$ObjectType.Goods__c.fields.GoodsPrice__c.label}</apex:facet> 25 <apex:outputText value="{!goods.GoodsPrice__c}" /> 26 </apex:column> 27 <apex:column style="width:225px;"> 28 <apex:facet name="header">{!$ObjectType.Goods__c.fields.GoodsCostPrice__c.label}</apex:facet> 29 <apex:outputText value="{!goods.GoodsCostPrice__c}" /> 30 </apex:column> 31 <apex:column style="width:500px;"> 32 <apex:facet name="header">操作</apex:facet> 33 <apex:outputLink value="/apex/detailGoods">编辑 34 <apex:param name="goodsId" value="{!goods.Id}"/> 35 </apex:outputLink> 36 </apex:column> 37 </apex:dataTable> 38 39 <apex:outputPanel layout="block" styleClass="paginator" 40 style="padding:0px;"> 41 <apex:panelGrid columns="2" style="width:100%;" 42 styleClass="az_text_table" rowClasses="paginator,paginator"> 43 <apex:outputText rendered="{!!resultPagination.hasRecord}" 44 value="第 0 页,共 0 页,每页 {!resultPagination.pageSize} 条" /> 45 <apex:outputText rendered="{!resultPagination.hasRecord}" 46 value="第 {!resultPagination.pageNumber} 页,共 {!resultPagination.totalPage} 页,每页 {!resultPagination.pageSize} 条" /> 47 <apex:panelGroup > 48 <apex:outputPanel > 49 <apex:outputText value="首页" 50 rendered="{!(!resultPagination.hasRecord)||(!resultPagination.hasPrevious)}" 51 style="border: solid 1px #ddd;padding:1px 6px;background: #e8e8e9;margin-right:5px;"></apex:outputText> 52 <apex:commandLink action="{!firstPage}" 53 rendered="{!resultPagination.hasRecord && resultPagination.hasPrevious}" 54 immediate="true" reRender="companyList" value="首页" 55 style="margin-right:5px;" /> 56 </apex:outputPanel> 57 <apex:outputPanel > 58 <apex:outputText value="上一页" 59 rendered="{!!resultPagination.hasRecord || (!resultPagination.hasPrevious)}" 60 style="border: solid 1px #ddd;padding:1px 6px;background: #e8e8e9;margin-right:5px;"></apex:outputText> 61 <apex:commandLink action="{!previousPage}" 62 rendered="{!resultPagination.hasRecord && resultPagination.hasPrevious}" 63 immediate="true" reRender="companyList" value="上一页" 64 style="margin-right:5px;" /> 65 </apex:outputPanel> 66 <apex:outputPanel > 67 <apex:outputText value="{!resultPagination.pageNumber}" 68 styleClass="current" /> 69 </apex:outputPanel> 70 <apex:outputPanel > 71 <apex:outputText value="下一页" 72 rendered="{!!resultPagination.hasRecord || !resultPagination.hasNext}" 73 style="border: solid 1px #ddd;padding:1px 6px;background: #e8e8e9;margin-right:5px;margin-left:5px;"></apex:outputText> 74 <apex:commandLink action="{!nextPage}" 75 rendered="{!resultPagination.hasRecord && resultPagination.hasNext}" 76 immediate="true" reRender="companyList" value="下一页" 77 style="margin-right:5px;margin-left:5px;" /> 78 </apex:outputPanel> 79 <apex:outputPanel > 80 <apex:outputText value="尾页" 81 rendered="{!!resultPagination.hasRecord || !resultPagination.hasNext}" 82 style="border: solid 1px #ddd;padding:1px 6px;background: #e8e8e9;margin-right:5px;"></apex:outputText> 83 <apex:commandLink action="{!lastPage}" 84 rendered="{!resultPagination.hasRecord && resultPagination.hasNext}" 85 immediate="true" reRender="companyList" value="尾页" 86 style="margin-right:5px;" /> 87 </apex:outputPanel> 88 </apex:panelGroup> 89 </apex:panelGrid> 90 </apex:outputPanel> 91 </apex:outputPanel> 92 </apex:outputPanel> 93 </apex:outputPanel> 94 </apex:outputPanel> 95 </apex:form> 96 </apex:page>
4.GoodsDetailController:此类中封装了Wizard的相关方式,Wizard的相关跳转均为转发方式。
1 public with sharing class GoodsDetailController { 2 3 /*入口是新建还是更新:新建为false,更新为true*/ 4 public Boolean isEditView{ 5 get { 6 if(isEditView == null) { 7 isEditView = false; 8 } 9 return isEditView; 10 } 11 set; 12 } 13 14 public GoodsDetailController() { 15 init(); 16 } 17 18 public GoodsDetailController(ApexPages.StandardController controller) { 19 init(); 20 } 21 22 public void init() { 23 String goodsId = ApexPages.currentPage().getParameters().get(\'goodsId\'); 24 if(goodsId != null) { 25 isEditView = true; 26 String queryGoods = \'SELECT Goods_Code_Unique__c, Name, GoodsBrand__c,\' + 27 \' GoodsCostPrice__c, GoodsDescribe__c, GoodsName__c, GoodsPrice__c,\' + 28 \' GoodsProfit__c, No__c,Id, Status__c\' + 29 \' FROM Goods__c where Id = :goodsId\'; 30 String queryGoodsVendor = \'SELECT Goods__c, Id, Vendor_Name__c\' + 31 \' FROM Goods_Vendor__c where Goods__c = :goodsId\'; 32 List<Goods__c> goodsList = Database.query(queryGoods); 33 if(goodsList != null && goodsList.size() > 0) { 34 goods = goodsList.get(0); 35 } 36 List<Goods_Vendor__c> goodsVendorList = Database.query(queryGoodsVendor); 37 if(goodsVendorList != null && goodsVendorList.size() > 0) { 38 goodsVendor = goodsVendorList.get(0); 39 } 40 } 41 } 42 43 public Goods__c goods{ 44 get{ 45 if(goods == null) { 46 goods = new Goods__c(); 47 } 48 return goods; 49 } 50 set; 51 } 52 53 public Goods_Vendor__c goodsVendor{ 54 get{ 55 if(goodsVendor == null) { 56 goodsVendor = new Goods_Vendor__c(); 57 } 58 return goodsVendor; 59 } 60 set; 61 } 62 63 public PageReference saveFinally() { 64 Savepoint sp = Database.setSavepoint(); 65 try { 66 upsert goods; 67 goodsVendor.Goods__c = goods.Id; 68 upsert goodsVendor; 69 } catch(DMLException e) { 70 Database.rollback(sp); 71 ApexPages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.ERROR,e.getMessage())); 72 return null; 73 } 74 return redirectToGoodsList(); 75 } 76 77 78 79 public PageReference redirectToGoods() { 80 return Page.detailGoods; 81 } 82 83 public PageReference redirectToVendor() { 84 return Page.detailVendor; 85 } 86 87 public PageReference redirectToTotal() { 88 return Page.detailGoodsTotal; 89 } 90 以上是关于salesforce 零基础学习(六十)Wizard样式创建数据的主要内容,如果未能解决你的问题,请参考以下文章salesforce 零基础学习(六十一)apex:component简单使用以及图片轮转播放的实现
salesforce 零基础学习(六十二)获取sObject中类型为Picklist的field values(含record type)
salesforce 零基础开发入门学习Salesforce功能介绍,IDE配置以及资源下载