• BettyDesk
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

Need code review on this trigger that I'm writing. I have a custom object called site_usage__c and whenever the subdomain_c field is updated, I'll need to propagate that change to the name field in account and contact objects and to the opportunity_name__c field in the opportunity object.  Is there anything wrong with my code?  Thanks!

 

// Trigger on Site_Usage__c object.

// If the subdomain field is updated on Site_Usage__c 

// then update account, lead, contact, opportunity

// to same name.

 

trigger SiteNameUpdate on Site_Usage__c (after update) {

        // Turn DB off.

        CRMfusionDBR101.DB_Globals.triggersDisabled = true;

                System.debug('########## Entering  SiteNameUpdate');    

    List<Site_Usage__c> siteList = new List<Site_Usage__c>();

    Map<double, string> subdomain = new Map<double, string>();

    

    for ( Site_Usage__c site:Trigger.new) {

        Site_Usage__c oldSite = Trigger.oldMap.get(site.ID);

        

        // subdomain was updated

        // Collect all site_id's

        if (site.Subdomain__c != oldSite.Subdomain__c) {

                subdomain.put(site.site_id__c, site.Subdomain__c);

        }

    }

    

    if ( subdomain.size() > 0 ) {

 

        // Get a list of all leads, accounts, contacts and opp's

        // that have the same site_id's

        List<Account> accList = [select ID, name, siteid__c from Account where siteid__c IN :subdomain.keySet()];

        List<Contact> contactList = [select ID, name, site_id__c from Contact where site_id__c IN :subdomain.keySet()];

        List<Opportunity> oppList = [select ID, opportunity_name__c, site_id__c from Opportunity where site_id__c IN :subdomain.keySet()];

        

        // Update the account name with the same ubdomain as the site_usage 

        for (Account acc:accList) {

            if ( subdomain.containsKey(acc.siteid__c) ) {

 

//??

                acc.Subdomain__c = subdomain.get(acc.siteid__c); 

            }

        }

        

        // Update all site names of contact with the same site name as the site_usage subdomain

        for (Contact acc:contactList) {

            if ( subdomain.containsKey(acc.site_id__c) ) {

//?

                acc.Subdomain__c = subdomain.get(acc.site_id__c); 

            }

        }

        

        // Update all matching account names in Opportunity with the same site name as the site_usage subdomain

        for (Opportunity acc:oppList) {

            if ( subdomain.containsKey(acc.site_id__c) ) {

//?

                acc.Subdomain__c = subdomain.get(acc.site_id__c); 

            }

        }

        

        update accList;

        update contactList;

        update oppList;

    } // end of "if flag"

                    System.debug('########## Exiting  SiteNameUpdate');

                            // Turn DB back on.

        CRMfusionDBR101.DB_Globals.triggersDisabled = false;        

}