global class UpdateQuoteOtcodeBacth implements Database.Batchable { //询价id String tempOppId =''; //参数 报价id String tempQuoId =''; //参数 otcode String otcode =''; //参数 产品id String proId =''; //价格手册id String tempPriceBookEntryId; public List activities; public List tmpactivities; public QELine active_activity; global UpdateQuoteOtcodeBacth() { } global UpdateQuoteOtcodeBacth(String tempQuoId,String otcode,String proId) { this.tempQuoId = tempQuoId; this.otcode = otcode; this.proId = proId; } global Database.QueryLocator start(Database.BatchableContext bc) { //查询 招投标标识 字段 为 true 的询价 String query = 'SELECT Id,OpportunityId from quote '; if(String.isNotBlank(this.tempQuoId)){ query += ' where Id = :tempQuoId'; } query += ' limit 200 '; return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, List quoteList) { if(quoteList.size()>0){ tempOppId = quoteList[0].OpportunityId; } List priceBookList = [SELECT Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry WHERE Product2Id=:proId and CurrencyIsoCode ='CNY']; if(priceBookList.size()>0){ tempPriceBookEntryId = priceBookList[0].Id; } //clone报价行 List QuoteLineItemCloneList = new List(); //clone询价行 List OpportunityLineItemCloneList = new List(); if(otcode!=null &&proId!=null){ //先检索报价行 //develop console、batch不能执行 //List QuoteLineItemList= [select fields(all) from QuoteLineItem where Quoteid =:tempQuoId limit 200]; List QuoteLineItemList = [Select id,Asset_Model_No__c,Product2Id, SFDA_Status__c, Name__c, BSS_Category__c,Qty_Unit__c, Cost__c, UnitPrice__c,UnitPrice, ListPrice__c, Quantity, PricebookEntryId, UnitPrice_Page__c,AgencyUnitPrice__c, AgencySubtotal__c, Present__c, multiYearWarranty__c , If_Cancel_Guarantee__c , GuaranteePeriod__c, ServicePrice__c , GuranteePrice__c, ProductEntend_gurantee_period_all__c,ProductGuranteePrice__c, GuranteeType__c,warrantyType__c, productServicePrice__c, NoDiscountTotal__c, provistonPeriod__c, Maintenance_Price_Year__c,Quoteid From QuoteLineItem where Quoteid =:tempQuoId Order by Item_Order__c, Id]; // //由于ot code是公式文本,所以实际需要的是替换产品id // QuoteLineItemCloneList = QuoteLineItemList.clone(); activities =new List(); if(QuoteLineItemList.size()>0){ for(QuoteLineItem quote :QuoteLineItemList){ QELine c = new QELine(quote,otcode,proId,tempPriceBookEntryId); System.debug('C::'+c); activities.add(c); } } //报价行先删后增 start if(QuoteLineItemList.size()>0){ delete QuoteLineItemList; } //报价行先删后增 end if(activities.size()>0){ for(QELine qe:activities){ //new 行项目 QuoteLineItem quo = new QuoteLineItem(); //复制报价行 quo = qe.pageObject; //addlist QuoteLineItemCloneList.add(quo); } } system.debug('QuoteLineItemCloneList:'+QuoteLineItemCloneList); //报价行先删后增 start if(QuoteLineItemCloneList.size()>0){ insert QuoteLineItemCloneList; } //报价行先删后增 end //先检索报价行 //develop console 、batch不能执行 //List OpportunityLineItemList= [select fields(all) from OpportunityLineItem where Opportunityid =:tempOppId limit 200]; List OpportunityLineItemList=[Select id,OpportunityId,Product2Id, Asset_Model_No__c, SFDA_Status__c, Name__c, ProductCode__c,Qty_Unit__c, Cost__c, ListPrice__c, Quantity, BSS_Category__c, TotalPrice,Product_Cost__c, Product_ListPrice__c,PricebookEntryId,UnitPrice__c, TotalPrice__c, AgencyUnitPrice__c, AgencySubtotal__c, Present__c, multiYearWarranty__c , If_Cancel_Guarantee__c , GuaranteePeriod__c,ServicePrice__c , GuranteePrice__c,ProductEntend_gurantee_period_all__c,ProductGuranteePrice__c, GuranteeType__c,warrantyType__c, productServicePrice__c,NoDiscountTotal__c, provistonPeriod__c, Maintenance_Price_Year__c From OpportunityLineItem Where OpportunityId =:tempOppId Order by Item_Order__c, Id]; // //由于ot code是公式文本,所以实际需要的是替换产品id // OpportunityLineItemCloneList = OpportunityLineItemList.clone(); //清空 // activities.clear(); activities =new List(); System.debug('最开始的:OpportunityLineItemList:'+OpportunityLineItemList); System.debug('最开始的数量:'+OpportunityLineItemList.size()); if(OpportunityLineItemList.size()>0){ for(OpportunityLineItem opp :OpportunityLineItemList){ QELine c = new QELine(opp,otcode,proId,tempPriceBookEntryId); System.debug('C::'+c); activities.add(c); } } //报价行先删后增 start if(OpportunityLineItemList.size()>0){ delete OpportunityLineItemList; } system.debug('询价行的行数:'+activities.size()); //报价行先删后增 end if(activities.size()>0){ for(QELine ql:activities){ //new 行项目 OpportunityLineItem oppli = new OpportunityLineItem(); //复制报价行 oppli = ql.pageOppObject; //addlist OpportunityLineItemCloneList.add(oppli); } } system.debug('OpportunityLineItemCloneList:'+OpportunityLineItemCloneList); //报价行先删后增 start if(OpportunityLineItemCloneList.size()>0){ insert OpportunityLineItemCloneList; } } } global void finish(Database.BatchableContext BC) { } public class QELine { public String Id; public String Asset_Model_No; public String Product2Id; public QuoteLineItem pageObject; public OpportunityLineItem pageOppObject; public String SFDA_Status; public String Name; public String BSS_Category; public String Qty_Unit; public Decimal Cost; public Decimal UnitPrice; public Decimal ListPrice; public Decimal Quantity; public String PricebookEntryId; public Decimal UnitPrice_Page; public Decimal AgencyUnitPrice; public Decimal AgencySubtotal; public Boolean Present; public Boolean multiYearWarranty; public Boolean If_Cancel_Guarantee; public Decimal GuaranteePeriod; public Decimal ServicePrice; public Decimal GuranteePrice; public Decimal ProductEntend_gurantee_period_all; public Decimal ProductGuranteePrice; public String GuranteeType; public String warrantyType; public Decimal productServicePrice; public Decimal NoDiscountTotal; public Decimal provistonPeriod; public Decimal Maintenance_Price_Year; public String Quoteid; //报价行遍历 public QELine(QuoteLineItem qli,String otcode,String proId,String tempPriceBookEntryId){ this.pageObject = qli.clone(); //insert 报价不需要id // this.Id = qli.Id; this.Asset_Model_No = qli.Asset_Model_No__c; //替换产品和价格手册 if(qli.Asset_Model_No__c.equals(otcode)){ this.pageObject.Product2Id=proId ; this.pageObject.PricebookEntryId=tempPriceBookEntryId ; } // else{ // this.Product2Id=qli.Product2Id ; // this.PricebookEntryId=qli.PricebookEntryId ; // } // this.Product2Id = qli.Product2Id; // this.SFDA_Status = qli.SFDA_Status__c; // this.Name = qli.Name__c; // this.BSS_Category = qli.BSS_Category__c; // this.Qty_Unit = qli.Qty_Unit__c; // this.Cost = qli.Cost__c; // this.UnitPrice = qli.UnitPrice__c; // this.UnitPrice = qli.UnitPrice; // this.ListPrice = qli.ListPrice__c; // this.Quantity = qli.Quantity; // this.PricebookEntryId = qli.PricebookEntryId; // this.UnitPrice_Page = qli.UnitPrice_Page__c; // this.AgencyUnitPrice = qli.AgencyUnitPrice__c; // this.AgencySubtotal = qli.AgencySubtotal__c; // this.Present = qli.Present__c; // this.multiYearWarranty = qli.multiYearWarranty__c; // this.If_Cancel_Guarantee = qli.If_Cancel_Guarantee__c; // this.GuaranteePeriod = qli.GuaranteePeriod__c; // this.ServicePrice = qli.ServicePrice__c; // this.GuranteePrice = qli.GuranteePrice__c; // this.ProductEntend_gurantee_period_all = qli.ProductEntend_gurantee_period_all__c; // this.ProductGuranteePrice = qli.ProductGuranteePrice__c; // this.GuranteeType = qli.GuranteeType__c; // this.warrantyType = qli.warrantyType__c; // this.productServicePrice = qli.productServicePrice__c; // this.NoDiscountTotal = qli.NoDiscountTotal__c; // this.provistonPeriod = qli.provistonPeriod__c; // this.Maintenance_Price_Year = qli.Maintenance_Price_Year__c; // this.Quoteid = qli.Quoteid; } //询价行遍历 public QELine(OpportunityLineItem oli,String otcode,String proId,String tempPriceBookEntryId){ this.pageOppObject = oli.clone(); //insert 报价不需要id // this.Id = qli.Id; this.Asset_Model_No = oli.Asset_Model_No__c; //替换产品和价格手册 if(oli.Asset_Model_No__c.equals(otcode)){ this.pageOppObject.Product2Id=proId ; this.pageOppObject.PricebookEntryId=tempPriceBookEntryId ; } } } }