• Betty_force
  • NEWBIE
  • 0 Points
  • Member since 2013

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

Hi,

 

New to Apex. Any suggestions on how to bulkify this code?

====

 

/* Provide summary of Number of Contacts on Account record */

trigger ContactSumTrigger on Contact (after insert) {

try {
// Turn DB off.
CRMfusionDBR101.DB_Globals.triggersDisabled = true;

System.debug('########## Entering ContactSumTrigger');
Contact[] cons;
if (Trigger.isDelete)
cons = Trigger.old;
else
cons = Trigger.new;

// get list of accounts
Set<ID> acctIds = new Set<ID>();
for (Contact con : cons) {
acctIds.add(con.AccountId);
}

Map<ID, Contact> contactsForAccounts = new Map<ID, Contact>([select Id
,AccountId
from Contact
where AccountId in :acctIds]);

Map<ID, Account> acctsToUpdate = new Map<ID, Account>([select Id
,Number_of_Contacts__c
from Account
where Id in :acctIds]);

for (Account acct : acctsToUpdate.values()) {
Set<ID> conIds = new Set<ID>();
for (Contact con : contactsForAccounts.values()) {
if (con.AccountId == acct.Id)
conIds.add(con.Id);
}
if (acct.Number_of_Contacts__c != conIds.size())
acct.Number_of_Contacts__c = conIds.size();
}

update acctsToUpdate.values();
System.debug('########## Exiting ContactSumTrigger'); }
catch(Exception e) {
system.debug ('error: '+e.getMessage() );
}

// Turn DB back on.
CRMfusionDBR101.DB_Globals.triggersDisabled = false;

}

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;        

}

 

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;        

}

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;        

}