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 from one custom object to 3 objects

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;        

}

kranjankranjan

Hi Betty,

 

As i understand you wanted to propogate the sub domain name to the name field in account, contact and opportuity. So you shoul be updating those fields in account, contact and opportunity respectively instead of Subdomain__c field. Or else if there is really a field with this name in account, contact and opportunity and you actually wanted to update that field, then you also need to fetch this field in the 3 queries respectively so that you can update them too. Hope it helps.

 

Regards

Betty_forceBetty_force

Thanks for your reply.  What I'd like to do is to get the value from the subdomain__c field from the site_usage_cc object whenever it's been updated and update the name field in the account, contact and opportunity objects.  It sounds like I need to fetch the field in the three queries, which I thought I did and update them respectively.  What would you suggest I change?  I appreciate your help.

kranjankranjan

Hi Betty,

 

You fetched the respective name fields in the queries, however you were assiging to the subdomain_c field itself. Instead you need to assign it to the name fields respectively as follows:

 

// 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.Name = 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.Name = 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.opportunity_name__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;        
}

 

 

Regards

BettyDeskBettyDesk

What if I wanted to create a trigger to update the subdomain field (and append "http://" in the front and ".com" at the end of the subdomain name) in the contact object everytime the record has been updated and the contact record type  "Billing Administrator" ?  The error I'm getting is no viable alternative at character '' in the line of code where I'm matching up record type to the Billing Administrator and an error on the variable, site.Subdomain__c.  Thanks in advance if you have a suggestion.  

 

trigger SiteNameUpdTrigger on Site_Usage__c(after update) {

// Turn DB off
CRMfusionDBR101.DB_Globals.triggersDisabled = true;
//System.debug ('########## Entering SiteNameUpdTrigger');   
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 assistly_site_id's
if (site.Subdomain__c != oldSite.Subdomain__c) {
subdomain.put(site.assistly_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;
}