function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
learnSFlearnSF 

Tigger logic didnot work properly

Hi,

 

I wrote trigger ,which work fine manualy but dind't work with data loader.

 

Here is my code.

 

trigger RateChangeContract on Account (before update) { List<Contract_History_Tracker__c> contractHistory = new List<Contract_History_Tracker__c>(); Map<String,Account> newAccount= new Map<String,Account>(); for (Integer i = 0; i < Trigger.new.size(); i++) { String territory=Trigger.new[i].Selling_Territory__c; if(territory!=null && territory.length() >3){ territory=territory.substring(0,4); } //System.debug('territorys'+territory); if(Trigger.new[i].RecordTypeId=='012300000000AGr' && Trigger.new[i].Type=='Dealership' && territory =='Cars'){ if ( (Trigger.old[i].Current_Contract_Date__c == Trigger.new[i].Current_Contract_Date__c)&& (Trigger.old[i].Total_Monthly_Rate__c != Trigger.new[i].Total_Monthly_Rate__c)) { newAccount.put(Trigger.new[i].Id,Trigger.new[i]); } } } for (Contract_History_Tracker__c contractHist : [Select Current_Rate__c, CreatedDate, Account__c,Id From Contract_History_Tracker__c where Account__c IN : newAccount.KeySet() Order By CreatedDate Desc limit 1]){ Account a= newAccount.get(contractHist.Account__c); contractHist.Current_Rate__c=a.Total_Monthly_Rate__c; contractHistory.add(contractHist); } update contractHistory; }

 

This works fine for first account and after that it didnot update contract hisotry.

 

I changed my code to this now and its works fine for every account but I have some thoughts that it may with breking limits of trigger as I am writing query inside trigger.

 

Here is working code.

 

trigger RateChangeContract on Account (before update) { //List<Contract_History_Tracker__c> contractHistory = new List<Contract_History_Tracker__c>(); Map<String,Account> newAccount= new Map<String,Account>(); for (Integer i = 0; i < Trigger.new.size(); i++) { String territory=Trigger.new[i].Selling_Territory__c; if(territory!=null && territory.length() >3){ territory=territory.substring(0,4); } //System.debug('territorys'+territory); if(Trigger.new[i].RecordTypeId=='012300000000AGr' && Trigger.new[i].Type=='Dealership' && territory =='Cars'){ if ( (Trigger.old[i].Current_Contract_Date__c == Trigger.new[i].Current_Contract_Date__c)&& (Trigger.old[i].Total_Monthly_Rate__c != Trigger.new[i].Total_Monthly_Rate__c)) { System.debug(Trigger.new[i].Total_Monthly_Rate__c); Contract_History_Tracker__c contractHistory= [Select Current_Rate__c, CreatedDate, Account__c,Id From Contract_History_Tracker__c where Account__c=:Trigger.new[i].Id Order By CreatedDate Desc limit 1]; contractHistory.Current_Rate__c=Trigger.new[i].Total_Monthly_Rate__c; update contractHistory; } } } }

 

I have two question now.

 

1. How to fix first code so that it gives same functionality which second code gives.

 

2. If I am using second code,for how many account tirgger will work fine without breaking limit and giving exception like(dml exception)

 

-Thanks,

gv007gv007
check update contract hisotry table is updating data properly ,the problem is some time data is not updating as per our code in force.com platform for this some special type of handling is requried based upon the problem it may help you some how try it or wait any expert can respond on this issue.
jpwagnerjpwagner

Your map stores the Id as a string, but this could be the 15 character Id in some scenarios and the 18 character Id in others.  The map should use Id's as keys.

 

This should work:  (feel free to change the syntax, I personally dislike the [i]-look)

 

trigger RateChangeContract on Account (before update) {

      List<Contract_History_Tracker__c> contractHistory = new List<Contract_History_Tracker__c>();
      Map<Id,Account> newAccount= new Map<Id,Account>();


      for(Account a : Trigger.new){
        Account olda = System.Trigger.oldMap.get(a.Id);
     String territory=a.Selling_Territory__c;
     if(territory!=null && territory.length() >3){
       territory=territory.substring(0,4);
     }
     if(territory =='Cars' && a.RecordTypeId=='012300000000AGr' && a.Type=='Dealership' && (olda.Current_Contract_Date__c == a.Current_Contract_Date__c) && (olda.Total_Monthly_Rate__c != a.Total_Monthly_Rate__c)) {
          newAccount.put(a.Id,a);  
        }   
      }
   
      for (Contract_History_Tracker__c contractHist : [Select Id, Current_Rate__c, CreatedDate, Account__c From Contract_History_Tracker__c where Account__c in : newAccount.KeySet() Order By CreatedDate Desc limit 1]){
        Account a= newAccount.get(contractHist.Account__c);
        contractHist.Current_Rate__c = a.Total_Monthly_Rate__c;
        contractHistory.add(contractHist);
      }
 
      update contractHistory;

}

learnSFlearnSF

I changed map key with Id.

 

Still didnot help to change any of current rate for contract histories.

 

I loaded 10 account with total maonthly rate $50 with data loader an none of contract hisotry changed with current rate.

 

Any guess why this is happening?

 

Logic seems to be correct but not sure where its going wrong.

learnSFlearnSF

finaly I changed code like this.

 

for(Id ids:newAccount.KeySet()){ Contract_History_Tracker__c contractHist = [Select Current_Rate__c, CreatedDate, Account__c,Id From Contract_History_Tracker__c where Account__c =: ids Order By CreatedDate Desc limit 1]; Account a= newAccount.get(contractHist.Account__c); contractHist.Current_Rate__c=a.Total_Monthly_Rate__c; contractHistory.add(contractHist); } update contractHistory;

 

and it solved my problem.

 

In previous code inside for loop, code was taking just first value from keyset.

 

-Thanks

jpwagnerjpwagner

You'll want to move the SOQL query outside of the for loop so that you can handle a bulk data load.