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; } }