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
Nandhini s 11Nandhini s 11 

Need help with triggers

Hi Guys,
I'm doing a hands-on exercise on triggers. The requirement is:
When a Billing Address is modified in accounts, get the new Postal Code. Then check which Contacts on the Account are outside that Postal Code. If 1 or more Contacts are outside of the Postal Code, mark Out_of_Zip as TRUE.

I've written the below trigger but the Out_of_Zip field is not getting updated. I'm not sure how to check if the trigger is firing or not.
Can someone tell me where to check the logs for triggers ?

trigger OutOfZipTrigger on Account (before update) {
    for(Account a: trigger.new){
        Account oldAccount = Trigger.oldMap.get(a.id);
  if(a.BillingAddress != oldAccount.BillingAddress )
    {
       List<contact> con = [select id,MailingPostalCode from contact where accountId =: a.id] ;
        for(contact c: con){
    if(a.BillingPostalCode != c.MailingPostalCode){
              a.Out_of_Zip__c = True;
               //acc.add(a);
           }
       }
    }
    }
}

 
Maharajan CMaharajan C
Hi Nandhini,

Try the below trigger:

Address is the CompounD Field try to compare address field like Below:


trigger OutOfZipTrigger on Account (before update) {
    
    Set<Id> accIds = new Set<Id>();
    List<Contact> conList = new List<Contact>();
    for(Account a: trigger.new){        
        if(a.BillingStreet != trigger.oldMap.get(a.id).BillingStreet || a.BillingCity!= trigger.oldMap.get(a.id).BillingCity || 
           a.BillingState != trigger.oldMap.get(a.id).BillingState || a.BillingPostalCode != trigger.oldMap.get(a.id).BillingPostalCode)
        {
            accIds.add(a.Id);
        }
        
        if(!accIds.isEmpty())
        {
            conList = [Select Id,AccountId,MailingPostalCode from contact where AccountId IN: accIds];
        }
        
        for(contact con : conList)
        {
            Account accrec = trigger.newMap.get(con.AccountId);
            if(con.MailingPostalCode != accrec.BillingPostalCode)
            {
                accrec.Out_of_Zip__c = True;
            }
        }
        
    }
}

Thanks,
Maharajan.C
Akshat AjmeraAkshat Ajmera
Hi Nandhini, 
Please Try the below trigger.
Though Maharajan's solution was perfect ,but the trigger was not bulkified which is not the best practice as far as apex is considered.
here is 
//This is the Handler class's method 
//this trigger will be working on the before update method.

  public static void accountOutofPostalCode(List<Account> accList, Map<ID,Account>OldMap, Map<Id,Account>NewMap){

       //We will have to take both old and new maps as we want to update the account 
      //so there should be a differentiating factor.

    Set<Id> accIds = new Set<Id>();
    
 
    for(Account accObjt: accList){        
        if(accObjt.BillingStreet != OldMap.get(accObjt.id).BillingStreet || accObjt.BillingCity!= OldMap.get(accObjt.id).BillingCity || 
           accObjt.BillingState != OldMap.get(accObjt.id).BillingState || accObjt.BillingPostalCode != OldMap.get(accObjt.id).BillingPostalCode){
            accIds.add(accObjt.Id);//Ids added to a unique set
        }

    }
         List<Contact> conList = new List<Contact>([Select Id,AccountId,MailingPostalCode from contact where AccountId IN: accIds]);
        for(Contact con:conList){
            Account accObj = NewMap.get(con.AccountId);
            if(con.MailingPostalCode != accObj.BillingPostalCode){
               accobj.Out_of_Postal__c =TRUE; 
            }
            
        }
    }
Also if the newMap does not work ..
make a new map of account and add the details in them.
Thank you 
Akshat Ajmera