• kROOM
  • NEWBIE
  • 20 Points
  • Member since 2019
  • ASE

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 2
    Replies
TRIGGER:

trigger ContactTrigger on Contact (before insert,before update) {
    if(Trigger.isBefore){
        if(Trigger.isInsert){
         ContactTriggerHandler.onBeforeInsert(Trigger.New);
        }
        if(Trigger.isUpdate){
            ContactTriggerHandler.onBeforeUpdate(Trigger.New);
        }
    }
}

CLASS:

public class ContactTriggerHandler {
    
    public static void onBeforeInsert(List<Contact> contactList){
        setPrimaryContact(contactList);
    }
    
    public static void onBeforeUpdate(List<Contact> contactList){
        updatePrimaryContact(contactList);
    }
    
    /* Instruction Number 8
     * When an existing Non-Primary Contact is updated to become a Primary Contact,  the system should check if the Account related to the updated Contact does not yet have a Primary Contact. If the Account already has a Primary Contact, a message should be shown to the user stating: "Invalid Primary Contact. This Account has an existing Primary Contact."
     * 
    */
   
    public static void updatePrimaryContact(List<Contact> contactList){
        List<Account> accountList = [SELECT Id, (SELECT Id, Primary_Contact__c FROM Contacts) FROM Account];
        Map<Id, Account> accountMap = new Map<Id, Account>();
        
        for(Account acct : accountList)    {
            accountMap.put(acct.Id, acct);
        }
        
        for(Contact contactNonPrime : contactList){
            for(Contact cc : accountMap.get(contactNonPrime.AccountId).Contacts){
                if(contactNonPrime.Primary_Contact__c == TRUE){
                    if(cc.Primary_Contact__c == TRUE && contactNonPrime.Id != cc.Id){
                        contactNonPrime.Primary_Contact__c.addError('Invalid Primary Contact. This Account has an existing Primary Contact.');
                    }
                }
            }
        }
    }
    
  /* Instruction Number 6
   * When a new Non-Primary Contact is created and the Account related to the new Contact does not have any existing Contacts, the new Contact should automatically become the Account's Primary Contact.
   */
        
    public static void setPrimaryContact(List<Contact> contactList){
        List<Account> accountList = [SELECT Id, (SELECT Id, Primary_Contact__c FROM Contacts)
                                     FROM Account];
        Map<Id, Account> accountMap = new Map<Id, Account>();
        
        for(Account acct : accountList)    {
            accountMap.put(acct.Id, acct);
        }
            
            for(Contact contactPrime : contactList){
                if(contactPrime.Primary_Contact__c != true && contactPrime.AccountId != NULL){
                   
                    if(accountMap.get(contactPrime.AccountId).Contacts.size() <1){
                        contactPrime.Primary_Contact__c = TRUE;
                    }
                }else if(contactPrime.Primary_Contact__c == TRUE && contactPrime.AccountId != NULL) {
                    for(Contact cc : accountMap.get(contactPrime.AccountId).Contacts){
                        if(cc.Primary_contact__c == TRUE){
                              contactPrime.Primary_Contact__c.addError('Invalid Primary Contact. This Account has an existing Primary Contact.');
                        }
                  }
             } 
        }
    }
}
  • January 10, 2019
  • Like
  • 0
public class AccountTriggerHandlerEx3 {

    public static void onBeforeInsert(List<Account> accountList){
       populateOpp(accountList);
    }
       
    public static void populateOpp(List<Account> accountList){
        for(Account acc : accountList){
            if(acc.shippingAddress == Null){
                if(acc.BillingCountry == 'US' || acc.BillingCountry == 'USA' || acc.BillingCountry == 'CA'){
                           acc.ShippingStreet             = acc.BillingStreet;
                        acc.ShippingCity               = acc.BillingCity;
                        acc.ShippingState             = acc.BillingState;
                        acc.ShippingPostalCode         = acc.BillingPostalCode;
                        acc.ShippingCountry         = acc.BillingCountry;
            }
        }
            if(acc.BillingCountry == 'PH' || acc.ShippingCountry == 'PH'){
            if (acc.Phone != Null || acc.phone != ''){
                if(acc.Phone.substring(0,3) != '+63'){
                    acc.Phone.addError('Philippine telephone numbers must be prefixed with the Country Code +63');
                }
                           
            }  
            }
        
        if(acc.BillingCountry == 'PH' || acc.ShippingCountry == 'PH'){
            if (acc.Fax != Null || acc.Fax !=''){
                if(acc.Fax.substring(0,3) != '+63'){
                    acc.Fax.addError('Philippine Fax numbers must be prefixed with the Country Code +63');
                }
                           
            }  
            }}
    }


      public static void onAfterUpdate(List<Account> accountSet){
       populateOppp(accountSet);
    }

    private static void populateOppp(List<Account> accountSet){
        List<Id> accountIds = new List<Id>();
        for(Account acc : accountSet){
          
        Account old = (Account)trigger.oldMap.get(acc.Id);
        if (acc.billingstreet != old.billingstreet|| acc.billingcity !=old.billingcity || acc.billingstate !=old.billingstate || acc.billingcountry != old.billingcountry) {
            accountIds.add(acc.Id);
        }
            
        }  
        if (accountIds.size() > 0) {
        contact[] updates = [select Id, accountId from contact where accountId in :accountIds];
        for (contact c : updates) {
            Account a = (account)trigger.newmap.get(c.accountId);
            c.mailingStreet = a.billingStreet;
            c.mailingcity = a.billingcity;
            c.mailingstate = a.billingstate;
            c.mailingpostalcode = a.billingpostalcode;
            c.mailingcountry = a.billingcountry;
                    }
        update updates;
    }                                                   
    
    
    }
    

}
  • January 10, 2019
  • Like
  • 0
public class OpportunityTriggerHandler {
    
    public static void onBeforeInsert(List<Opportunity> opportunityList){
        populateOpp(opportunityList);
    }
    public static void onBeforeUpdate(List<Opportunity> opportunityList, Map<Id,Opportunity> opportunityId){
        populateOppDescription(opportunityList, opportunityId);
    }
    
    public static void populateOpp(List<Opportunity> opportunityList){
        for(Opportunity opp : opportunityList){
            if(opp.StageName == 'Closed Won'){
                opp.Description = 'This Opportunity is Closed Won';
            }
        }
    }
    
    public static void populateOppDescription(List<Opportunity> opportunityList, Map<Id,Opportunity> opportunityId){
        for(Opportunity opp : opportunityList){
            Opportunity oldOpp = opportunityId.get(opp.Id);
            Boolean oldStage = oldOpp.StageName.equals('Closed Won');
            Boolean newStage = opp.StageName.equals('Closed Won');
            
            if(!oldStage && newStage){
                opp.Description = 'This Opportunity is Closed Won';
            }
            
        }
    }
    
    
}
  • January 10, 2019
  • Like
  • 0
TRIGGER:

trigger ContactTrigger on Contact (before insert,before update) {
    if(Trigger.isBefore){
        if(Trigger.isInsert){
         ContactTriggerHandler.onBeforeInsert(Trigger.New);
        }
        if(Trigger.isUpdate){
            ContactTriggerHandler.onBeforeUpdate(Trigger.New);
        }
    }
}

CLASS:

public class ContactTriggerHandler {
    
    public static void onBeforeInsert(List<Contact> contactList){
        setPrimaryContact(contactList);
    }
    
    public static void onBeforeUpdate(List<Contact> contactList){
        updatePrimaryContact(contactList);
    }
    
    /* Instruction Number 8
     * When an existing Non-Primary Contact is updated to become a Primary Contact,  the system should check if the Account related to the updated Contact does not yet have a Primary Contact. If the Account already has a Primary Contact, a message should be shown to the user stating: "Invalid Primary Contact. This Account has an existing Primary Contact."
     * 
    */
   
    public static void updatePrimaryContact(List<Contact> contactList){
        List<Account> accountList = [SELECT Id, (SELECT Id, Primary_Contact__c FROM Contacts) FROM Account];
        Map<Id, Account> accountMap = new Map<Id, Account>();
        
        for(Account acct : accountList)    {
            accountMap.put(acct.Id, acct);
        }
        
        for(Contact contactNonPrime : contactList){
            for(Contact cc : accountMap.get(contactNonPrime.AccountId).Contacts){
                if(contactNonPrime.Primary_Contact__c == TRUE){
                    if(cc.Primary_Contact__c == TRUE && contactNonPrime.Id != cc.Id){
                        contactNonPrime.Primary_Contact__c.addError('Invalid Primary Contact. This Account has an existing Primary Contact.');
                    }
                }
            }
        }
    }
    
  /* Instruction Number 6
   * When a new Non-Primary Contact is created and the Account related to the new Contact does not have any existing Contacts, the new Contact should automatically become the Account's Primary Contact.
   */
        
    public static void setPrimaryContact(List<Contact> contactList){
        List<Account> accountList = [SELECT Id, (SELECT Id, Primary_Contact__c FROM Contacts)
                                     FROM Account];
        Map<Id, Account> accountMap = new Map<Id, Account>();
        
        for(Account acct : accountList)    {
            accountMap.put(acct.Id, acct);
        }
            
            for(Contact contactPrime : contactList){
                if(contactPrime.Primary_Contact__c != true && contactPrime.AccountId != NULL){
                   
                    if(accountMap.get(contactPrime.AccountId).Contacts.size() <1){
                        contactPrime.Primary_Contact__c = TRUE;
                    }
                }else if(contactPrime.Primary_Contact__c == TRUE && contactPrime.AccountId != NULL) {
                    for(Contact cc : accountMap.get(contactPrime.AccountId).Contacts){
                        if(cc.Primary_contact__c == TRUE){
                              contactPrime.Primary_Contact__c.addError('Invalid Primary Contact. This Account has an existing Primary Contact.');
                        }
                  }
             } 
        }
    }
}
  • January 10, 2019
  • Like
  • 0
public class AccountTriggerHandlerEx3 {

    public static void onBeforeInsert(List<Account> accountList){
       populateOpp(accountList);
    }
       
    public static void populateOpp(List<Account> accountList){
        for(Account acc : accountList){
            if(acc.shippingAddress == Null){
                if(acc.BillingCountry == 'US' || acc.BillingCountry == 'USA' || acc.BillingCountry == 'CA'){
                           acc.ShippingStreet             = acc.BillingStreet;
                        acc.ShippingCity               = acc.BillingCity;
                        acc.ShippingState             = acc.BillingState;
                        acc.ShippingPostalCode         = acc.BillingPostalCode;
                        acc.ShippingCountry         = acc.BillingCountry;
            }
        }
            if(acc.BillingCountry == 'PH' || acc.ShippingCountry == 'PH'){
            if (acc.Phone != Null || acc.phone != ''){
                if(acc.Phone.substring(0,3) != '+63'){
                    acc.Phone.addError('Philippine telephone numbers must be prefixed with the Country Code +63');
                }
                           
            }  
            }
        
        if(acc.BillingCountry == 'PH' || acc.ShippingCountry == 'PH'){
            if (acc.Fax != Null || acc.Fax !=''){
                if(acc.Fax.substring(0,3) != '+63'){
                    acc.Fax.addError('Philippine Fax numbers must be prefixed with the Country Code +63');
                }
                           
            }  
            }}
    }


      public static void onAfterUpdate(List<Account> accountSet){
       populateOppp(accountSet);
    }

    private static void populateOppp(List<Account> accountSet){
        List<Id> accountIds = new List<Id>();
        for(Account acc : accountSet){
          
        Account old = (Account)trigger.oldMap.get(acc.Id);
        if (acc.billingstreet != old.billingstreet|| acc.billingcity !=old.billingcity || acc.billingstate !=old.billingstate || acc.billingcountry != old.billingcountry) {
            accountIds.add(acc.Id);
        }
            
        }  
        if (accountIds.size() > 0) {
        contact[] updates = [select Id, accountId from contact where accountId in :accountIds];
        for (contact c : updates) {
            Account a = (account)trigger.newmap.get(c.accountId);
            c.mailingStreet = a.billingStreet;
            c.mailingcity = a.billingcity;
            c.mailingstate = a.billingstate;
            c.mailingpostalcode = a.billingpostalcode;
            c.mailingcountry = a.billingcountry;
                    }
        update updates;
    }                                                   
    
    
    }
    

}
  • January 10, 2019
  • Like
  • 0