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
Betty_forceBetty_force 

Apex trigger to update field value needs code review

Here's a trigger that I've written to update the Site Name of the Contact record to "http://" + subdomain__c + ".com" if it meets the following conditions that the contact record type is "Billing Administrator" and the subdomain__c field has changed in the site_usage object.   Pleaset let me know if you see any problems with this code.  Thank you!

 

===

 

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

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

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

        }

    }

    

    if ( subdomain.size() > 0 ) {

 

        // Get a list of contacts where the record type is Billing Administrator

        // that have the same site_id's

 

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

        

        // 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)) && ( contactList.get(acc.User_Role__c) == "Billing Administrator") {

                acc.Site__c = subdomain.get('http://' + site.Subdomain__c + ".com") ; 

            }

        }

          

        update contactList;

 

    } // end of "if flag"

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

                            // Turn DB back on.

        CRMfusionDBR101.DB_Globals.triggersDisabled = false;        

}