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
JJJenkinsJJJenkins 

Tricky Trigger Problem

I'm having issues with not getting this to fire all of the time:

 

trigger InsertACHupdateDate on Account (before update) { 
   
    map<id,account> oldMap = new map<id,account>();
    map<id,account> newMap = new map<id,account>();
    for(Account acctOld : trigger.old)
        {                     
                oldMap.put(acctOld.id, acctOld);              
        }
    for(account acctNew : trigger.new)
        {     
                newMap.put(acctNew.id, acctNew);              
        }
        
    for(account a :trigger.new)
        {
            boolean updateCheck = false;
            account oldA = oldMap.get(a.id);
            account newA = newMap.get(a.id);            
           
            if(oldA.Routing_Number_enc__c != null && newA.Routing_Number_enc__c != null)
                if(oldA.Routing_Number_enc__c != newA.Routing_Number_enc__c)
                    updateCheck = true;
               
            if(oldA.Account_Number__c != null && newA.Account_Number__c != null)    
                if(oldA.Account_Number__c != newA.Account_Number__c)
                    updateCheck = true;
                
            if(oldA.BillingStreet!=newA.BillingStreet)
                updateCheck = true;
                
            if(oldA.BillingCity!=newA.BillingCity)
                updateCheck = true;
                
            if(oldA.BillingState!=newA.BillingState)
                updateCheck = true;
                
            if(oldA.BillingPostalCode!=newA.BillingPostalCode)
                updateCheck = true;
                
            if(oldA.BillingCountry!=newA.BillingCountry)
                updateCheck = true;
                
            if(oldA.Payable_To__c!=newA.Payable_To__c)
                updateCheck = true;
            
            if(updateCheck)
            {
                a.Last_ACH_Update__c = System.Today();
            }     
        }                                             
}

 It will fire if there is info in the field and it changes but not when it is first filled in for the Account# field and Routing field.

 

Thanks,

Rahul SharmaRahul Sharma

I think there should be OR conditions (instead of AND) for both Account Number and routing fields.

 

JJJenkinsJJJenkins

but now if the fields are blank the field gets populated even though nothing has changed.

Rahul SharmaRahul Sharma

Give it a try

 

if(oldA.Routing_Number_enc__c != null && newA.Routing_Number_enc__c != null && oldA.Routing_Number_enc__c != newA.Routing_Number_enc__c)
	if(oldA.Routing_Number_enc__c != newA.Routing_Number_enc__c)
		updateCheck = true;

if(oldA.Account_Number__c != null && newA.Account_Number__c != null && oldA.Account_Number__c != newA.Account_Number__c)    
	if(oldA.Account_Number__c != newA.Account_Number__c)
		updateCheck = true;

 

JJJenkinsJJJenkins

That is how I originally wrote it yesterday before I moved to this version.  When I debug the issue appears that if both fields are blank the newA.number returns nothing - not even a null but the oldA.number returns null if it is null so it reads as they are different and updates the field.

 

thanks!

Rahul SharmaRahul Sharma

With old and new i refer to field values.

Try this and Let me know if it does not work.

if( (old == null && new != null) || (old != null && new == null) || (old != null && new != null && old != new) )

 

JJJenkinsJJJenkins

the only time it doesn't work is if both fields are blank.

 

 

Rahul SharmaRahul Sharma

I believe you used this condition.

 

if( (oldA.Routing_Number_enc__c == null && newA.Routing_Number_enc__c != null) || (oldA.Routing_Number_enc__c != null && newA.Routing_Number_enc__c == null) || 
(oldA.Routing_Number_enc__c != null && newA.Routing_Number_enc__c != null && oldA.Routing_Number_enc__c != newA.Routing_Number_enc__c) )

 If both the fields are blank means the field is not been updated, so the field must not be populated, Right?.

Shailesh DeshpandeShailesh Deshpande

I think you dont need to use the first if conditions... these are allowing the trigger to proceed further only when the fields are not blank..if they are blank..nothing in your trigger gets executed..

 

you simply need to keep the inner if..remove the outer if statements..your trigger will work every time the record is updated..that is when thenew values and old values are different..

 

Instead of writing like this:

 

if(oldA.Routing_Number_enc__c != null && newA.Routing_Number_enc__c != null)
                if(oldA.Routing_Number_enc__c != newA.Routing_Number_enc__c)
                    updateCheck = true;

Write like this:

 

if(oldA.Routing_Number_enc__c != newA.Routing_Number_enc__c)
                    updateCheck = true;

 

 

 

JJJenkinsJJJenkins

The issue is with the map the new value doesn't return anything if it's blank.  Not even null.  It's like the new value doesn't exist.

JJJenkinsJJJenkins

here's a shot of the debug log

9:35:41.178 (178138000)|CODE_UNIT_STARTED|[EXTERNAL]|01qM000000003rr|InsertACHupdateDate on Account trigger event BeforeUpdate for [001C000000p8oti]
09:35:41.178 (178428000)|SYSTEM_METHOD_ENTRY|[7]|MAP.put(ANY, ANY)
09:35:41.178 (178552000)|SYSTEM_METHOD_EXIT|[7]|MAP.put(ANY, ANY)
09:35:41.178 (178624000)|SYSTEM_METHOD_ENTRY|[11]|MAP.put(ANY, ANY)
09:35:41.178 (178691000)|SYSTEM_METHOD_EXIT|[11]|MAP.put(ANY, ANY)
09:35:41.178 (178775000)|SYSTEM_METHOD_ENTRY|[17]|MAP.get(ANY)
09:35:41.178 (178823000)|SYSTEM_METHOD_EXIT|[17]|MAP.get(ANY)
09:35:41.178 (178873000)|SYSTEM_METHOD_ENTRY|[18]|MAP.get(ANY)
09:35:41.178 (178914000)|SYSTEM_METHOD_EXIT|[18]|MAP.get(ANY)
09:35:41.179 (179053000)|SYSTEM_METHOD_ENTRY|[23]|System.debug(ANY)
09:35:41.179 (179107000)|USER_DEBUG|[23]|DEBUG|null ----------------------------this is oldA.Routing
09:35:41.179 (179129000)|SYSTEM_METHOD_EXIT|[23]|System.debug(ANY)
09:35:41.179 (179162000)|SYSTEM_METHOD_ENTRY|[24]|System.debug(ANY)
09:35:41.179 (179197000)|USER_DEBUG|[24]|DEBUG| --------------------------------this is newA.Routing
09:35:41.179 (179220000)|SYSTEM_METHOD_EXIT|[24]|System.debug(ANY)
09:35:41.179 (179325000)|SYSTEM_METHOD_ENTRY|[53]|System.today()
09:35:41.179 (179393000)|SYSTEM_METHOD_EXIT|[53]|System.today()

 This was ran when both fields were blank.

Rahul SharmaRahul Sharma

Whats datatype of Account Number and Routing? 

JJJenkinsJJJenkins

encrypted text

Rahul SharmaRahul Sharma

Can you post the modified code where you had put debug.

JJJenkinsJJJenkins
  if(oldA.Routing_Number_enc__c != null && newA.Routing_Number_enc__c != null)
               if (oldA.Routing_Number_enc__c != newA.Routing_Number_enc__c)
                   system.debug(oldA.Routing_Number_enc__c);
                   system.debug(newA.Routing_Number_enc__c);
               {
                   updateCheck = true;
               }

 

Rahul SharmaRahul Sharma

Please post output of this:

 

if(oldA.Routing_Number_enc__c != null && newA.Routing_Number_enc__c != null)
if(oldA.Routing_Number_enc__c != newA.Routing_Number_enc__c)
{
system.debug('==Oldvalue=='+oldA.Routing_Number_enc__c);
system.debug('==Newvalue=='+newA.Routing_Number_enc__c);
updateCheck = true;
}

 

JJJenkinsJJJenkins

Nothing showed?

10:35:42.931 (931863000)|CODE_UNIT_STARTED|[EXTERNAL]|01qM000000003rr|InsertACHupdateDate on Account trigger event BeforeUpdate for [001C000000p8oti]
10:35:42.932 (932285000)|SYSTEM_METHOD_ENTRY|[7]|MAP.put(ANY, ANY)
10:35:42.932 (932506000)|SYSTEM_METHOD_EXIT|[7]|MAP.put(ANY, ANY)
10:35:42.932 (932630000)|SYSTEM_METHOD_ENTRY|[11]|MAP.put(ANY, ANY)
10:35:42.932 (932751000)|SYSTEM_METHOD_EXIT|[11]|MAP.put(ANY, ANY)
10:35:42.932 (932907000)|SYSTEM_METHOD_ENTRY|[17]|MAP.get(ANY)
10:35:42.933 (933001000)|SYSTEM_METHOD_EXIT|[17]|MAP.get(ANY)
10:35:42.933 (933107000)|SYSTEM_METHOD_ENTRY|[18]|MAP.get(ANY)
10:35:42.933 (933190000)|SYSTEM_METHOD_EXIT|[18]|MAP.get(ANY)
10:35:42.035 (933433000)|CUMULATIVE_LIMIT_USAGE
10:35:42.035|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Number of script statements: 10 out of 200000
  Maximum heap size: 0 out of 3000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100 

 

VPrakashVPrakash

Try this,

 

trigger InsertACHupdateDate on Account (before update) { 
   
	boolean updateCheck = false;        
    for(account newA :trigger.new){    
            account oldA = trigger.oldMap.get(a.id);           
            if(oldA.Routing_Number_enc__c != null && oldA.Routing_Number_enc__c != newA.Routing_Number_enc__c)                
				updateCheck = true;               
            if(oldA.Account_Number__c != null && oldA.Account_Number__c != newA.Account_Number__c)                   
				updateCheck = true; 
				
            if(oldA.BillingStreet!=newA.BillingStreet)
                updateCheck = true;
                
            if(oldA.BillingCity!=newA.BillingCity)
                updateCheck = true;
                
            if(oldA.BillingState!=newA.BillingState)
                updateCheck = true;
                
            if(oldA.BillingPostalCode!=newA.BillingPostalCode)
                updateCheck = true;
                
            if(oldA.BillingCountry!=newA.BillingCountry)
                updateCheck = true;
                
            if(oldA.Payable_To__c!=newA.Payable_To__c)
                updateCheck = true;
            
            if(updateCheck){        
                a.Last_ACH_Update__c = System.now();
            }     
        }                                             
}