零配件入庫 Trigger 插入觸發臺賬和零配件更新
阿新 • • 發佈:2018-04-02
isa col detail post 觸發 har hand status 型號
trigger PartsInTrigger on PartsIn__c (after insert) { PartsInHandler handler = new PartsInHandler(); if(Trigger.IsAfter){ if(Trigger.isInsert){ handler.PartsIninsert(Trigger.New); } } }
public with sharing class PartsInHandler { public void PartsIninsert(List<PartsIn__c> partsNew){ Map<String,PartsIn__c> MapSpin = new Map<String,PartsIn__c>(); //入庫map 型號,對象 Map<String,Integer> MapSpinQ = new Map<String,Integer>(); //入庫數量map 型號,數量 List<Parts__c> addparList = new List<Parts__c>(); Set<Id> partIds = new Set<Id>(); For(PartsIn__c par: partsNew){ MapSpin.put(par.Name, par); MapSpinQ.put(par.Name, Integer.valueOf(par.Quantity__c)); }//system.debug(MapSpinQ.keySet()); //查詢與入庫對應的臺賬 List<Parts__c> parList = [SELECT Id,Name,Status__c,SPPrice__c,PartsModel__c,Position__c,LotNo__c,STContractNo__c From Parts__c Where Name in: MapSpin.keySet() AND Status__c=‘缺貨‘ ORDER BY CreatedDate]; For(Parts__c plist: parList){ if(MapSpinQ.containsKey(plist.Name)){ Integer pi = MapSpinQ.get(plist.Name); //system.debug(‘pi=‘+pi); if(pi > 0){ PartsIn__c pts = MapSpin.get(plist.Name); plist.Status__c = ‘已預約‘; plist.SPPrice__c = pts.SPPrice__c; plist.NetPrice__c = pts.Net_to_YTR__c; pi -= 1; MapSpinQ.put(plist.Name, pi); partIds.add(plist.Id); } } } //查詢與臺賬對應的零配件 List<PartsAU__c> AUList = [SELECT Id,SparePartsModel__c,Status__c,WorkOrderItem__c,DetailsSpareParts__c From PartsAU__c Where DetailsSpareParts__c in: partIds AND Status__c =‘缺貨‘]; For(PartsAU__c paulist: AUList){ paulist.Status__c = ‘已預約‘; // paulist.Type__c= ‘預約‘; } for(String piq: MapSpinQ.keyset()) { if(MapSpinQ.get(piq) > 0){ for(Integer i = 0; i < MapSpinQ.get(piq); i++){ PartsIn__c pi = MapSpin.get(piq); Parts__c ps = new Parts__c(); ps.Name = pi.Name; ps.PartsModel__c = pi.SparePartsModel__c; ps.Position__c =pi.Location__c; ps.LotNo__c =pi.BatchNo__c; ps.STContractNo__c =pi.ContractNo__c; ps.InDate__c =pi.InDate__c; ps.SPPrice__c =pi.SPPrice__c; ps.Status__c = ‘可使用‘; ps.NetPrice__c = pi.Net_to_YTR__c; addparList.add(ps); } } } //system.debug(addparList); update parList; insert addparList; update AUList; } }
零配件入庫 Trigger 插入觸發臺賬和零配件更新