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
sooraj babusooraj babu 

how to write a class for trigger

trigger copyaddresstrigger on Contact (before insert,before update) {

    for(contact address : trigger.new)
    {
    if(address.Copy_Address__c ==True)
    {
    address.OtherStreet = address.MailingStreet ; 
    address.OtherCity   = address.MailingCity;
    address.OtherState = address.MailingState;
    address.OtherPostalCode = address.MailingPostalCode;
    address.OtherCountry = address.MailingCountry;    
    }  
        else
    { address.OtherStreet = null ; 
    address.OtherCity   = null;
    address.OtherState = null;
    address.OtherPostalCode = null;
    address.OtherCountry = null;
    }
}
}
Khushmeet KaurKhushmeet Kaur
Hi Sooraj,
In order to write an Apex class from the trigger, you need to pass the Trigger. New in the trigger class.

Please check the below post to learn about Trigger Framework.

1) http://amitsalesforce.blogspot.in/2015/06/trigger-best-practices-sample-trigger.html

Please refer to the code 

Create Trigger Like below
trigger copyaddresstrigger on Contact (before insert,before update) {
    
   if(trigger.isBefore){

    if(trigger.IsInsert || trigger.IsUpdate){

        contactHandler.copyAddressTrigger(Trigger.New);
     }
   }
}

Handler like below
public class contactHandler(
public static void copyAddressTrigger(List<Contact>contactList){

 for(contact address : contactList)
     {
       if(address.Copy_Address__c ==True)
      {
        address.OtherStreet = address.MailingStreet ; 
        address.OtherCity   = address.MailingCity;
        address.OtherState = address.MailingState;
        address.OtherPostalCode = address.MailingPostalCode;
        address.OtherCountry = address.MailingCountry;    
       }  
        else
      { 
       address.OtherStreet = null ; 
       address.OtherCity   = null;
       address.OtherState = null;
       address.OtherPostalCode = null;
       address.OtherCountry = null;
     }
}
}
}

 
sachinarorasfsachinarorasf
Hi Sooraj,

I have gone through your problem. Please try the below code.
 
Trigger: copyaddresstrigger

trigger copyaddresstrigger on Contact (before insert,before update) {
    if(Trigger.isInsert && Trigger.isBefore){
        copyaddressController.copyAddressMethod(trigger.new);
    }
    if(Trigger.isUpdate && Trigger.isBefore){
        copyaddressController.copyAddressMethod(trigger.new);
    }
}

Apex Class: copyaddressController

public class copyaddressController {
    public static void copyAddressMethod(List<Contact> contactList){
        try{
            for(Contact address : contactList){
                if(address.Copy_Address__c ==True && address.MailingStreet!= null && address.MailingCity !=null && address.MailingState != null && address.MailingPostalCode != null && address.MailingCountry != null){
                    address.OtherStreet = address.MailingStreet ; 
                    address.OtherCity   = address.MailingCity;
                    address.OtherState = address.MailingState;
                    address.OtherPostalCode = address.MailingPostalCode;
                    address.OtherCountry = address.MailingCountry; 
                }
                else{ 
                    address.OtherStreet = null ; 
                    address.OtherCity   = null;
                    address.OtherState = null;
                    address.OtherPostalCode = null;
                    address.OtherCountry = null;
                }
            }
        }catch(Exception ex){
            System.debug('Error Message'+ex.getMessage() + 'Line Number'+ ex.getLineNumber());
        }
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora