for (Wrapper ob : (List<Wrapper>)wResp)
{
Updated =false;
//System.debug(ob);
System.debug('------Rep Name : '+ob.Rep_Name);
System.debug('------Split Percentage : '+ob.Split_Percentage);
System.debug('------Cas_ID : '+ob.Trade_History_CAS_ID);
System.debug('------Estimated Commission : '+ob.Estimated_Commission);
for(Trade_History__c oh :
[Select
CAS_ID__c, Id,
RepCode__r.Id ,
RepCode__c,
LastModifiedDate
From Trade_History__c
Where RepCode__r.Name =:ob.Rep_Name AND CAS_ID__c =: ob.Trade_History_CAS_ID //need to match there on UAT and prod misisngh on DEV
]){
System.debug('------Found Record details '+ oh.Id);
//get rep details
Rep__c Rep = [Select Id,CAS_ID__c from Rep__c where Rep_Code__c=:oh.RepCode__c Limit 1];
System.debug('------Found Rep details '+ Rep.Id);
//ok trade history is valid (overkill check if we have an existing rep trade history i.e UPDATE vs INSERT, rep trade history has matche on casID)
for(Rep_Trade_History__c rth :
[Select
Id ,
LastModifiedDate
From Rep_Trade_History__c
Where Trade_History__c =:oh.Id
]){
System.debug('------Found EXISTING rep trade history we must Update! '+ rth.Id);
//DO stuff
//store it for now so we can combine update and save login
//update record here
//do the modifciation here and add to the list
//just do the update here
rth.Trade_History_CAS_ID__c = ob.Trade_History_CAS_ID;
if( ob.Split_Percentage != null){
rth.Split_Percentage__c = ob.Split_Percentage;
}
if(ob.Estimated_Commission != null){
rth.Split_Revenue__c = ob.Estimated_Commission; //** TO DO this might be a calculated feild percentage x comminson */
}
rth.Rep__c = Rep.id;
rth.Trade_History__c = oh.id; //link up the trade history
System.debug('------Updated Record Details for RepTradeHistory ID '+ rth.Id);
ExistingRepTradeHistories.add(rth);
Updated =true;
}
Hello ola lawal 5Here is code. It will not hit SOQL limit.List<Id> lstCASID = new List<Id>();List<String> lstRepName = new List<String>();Map<Id, Trade_History__c> mapTradeHist = new Map<Id, Trade_History__c>();Map<Id, Wrapper> mapNewTradeHistWithWrapper = new Map<Id, Wrapper>();Map<String, Rep__c> mapRepWithRepCode = new Map<String, Rep__c>();Map<Id, String> mapTradHisWithRepCode = new Map<Id, String>();List<String> lstRepCode = new List<String>(); for (Wrapper ob : (List<Wrapper>)wResp) { if(ob.Rep_Name != null && ob.Rep_Name != ''){ lstRepName.add(ob.Rep_Name); } if(ob.Trade_History_CAS_ID != null && ob.Trade_History_CAS_ID != ''){ lstCASID.add(ob.Trade_History_CAS_ID); }}for(Trade_History__c oh : [Select CAS_ID__c, Id, RepCode__r.Id ,RepCode__c,LastModifiedDate From Trade_History__c Where RepCode__r.Name IN :lstRepName AND CAS_ID__c IN :lstCASID ]){ mapTradeHist.put(oh.Id, oh); lstRepCode.add(oh.RepCode__c); mapTradHisWithRepCode.put(oh.Id, oh.RepCode__c);}for(Rep_Code__c rc : [Select Id,CAS_ID__c,Rep_Code__c from Rep__c where Rep_Code__c IN :lstRepCode]){ mapRepWithRepCode.put(rc.Rep_Code__c, rc);}for (Wrapper ob : (List<Wrapper>)wResp) { for(Trade_History__c oh : mapTradeHist.values()){ if(ob.Rep_Name == oh.RepCode__r.Name && ob.Trade_History_CAS_ID == oh.CAS_ID__c ){ mapNewTradeHistWithWrapper.put(oh.Id, ob); } }}for(Rep_Trade_History__c rth : [Select Id,LastModifiedDate,Trade_History__c From Rep_Trade_History__c Where Trade_History__c =:mapNewTradeHist.Keyset()]){ Wrapper ob = mapNewTradeHistWithWrapper.get(rth.Trade_History__c); rth.Trade_History_CAS_ID__c = ob.Trade_History_CAS_ID; if( ob.Split_Percentage != null){ rth.Split_Percentage__c = ob.Split_Percentage; } if(ob.Estimated_Commission != null){ rth.Split_Revenue__c = ob.Estimated_Commission; } rth.Rep__c = mapRepWithRepCode.get(mapTradHisWithRepCode.get(rth.Trade_History__c)).Id; rth.Trade_History__c = rth.Trade_History__c; ExistingRepTradeHistories.add(rth); }Please mark best answer if it helps you.Thanks,Arawind Mundanakar