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
Janna McNeil 13Janna McNeil 13 

Prevent a Trigger from firing for Account when not located in the United States

I have an excellent trigger (inherited from the previous SFDC Admin) that pulls in values from a custom object that manages my territories by zip code. I'm rolling out Salesforce for my European team and unfortunately the zip codes in Germany are duplicates to zip codes in the US. 

How do I update the trigger to prevent it from firing if the Country is not USA? I am using standard country and state picklists.
@anilbathula@@anilbathula@
Hi Janna,

You need to add the condition in the trigger.
It will be easy to explain ,if you post the trigger code.

Thanks
Anil.B
 
Ajay K DubediAjay K Dubedi
Hi Janna,

Update your code like below code :

trigger triggerOnCustomObject on Account(trigger_events){
    for(Account accObj : trigger.New){
        if(accObj.Country != 'United States'){
            //Write your code here
        }
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Janna McNeil 13Janna McNeil 13
The trigger code is as follows:

trigger AccountAssignTerritoryTrigger on Account (before insert, before update) {
    String[] zips = new String[0];
    for(Account a : Trigger.new) {
        if(Trigger.isInsert || a.ShippingPostalCode != Trigger.oldMap.get(a.Id).ShippingPostalCode) {
            zips.add(a.ShippingPostalCode);
            System.debug('#####'+a.ShippingPostalCode);
        }
    }
    
    Map<String, Custom_Territory__c> zipMap = new Map<String, Custom_Territory__c>();
    for(Custom_Territory__c[] cts : [Select Id, Name, Territory__c, Zip_Code__c,Patient_Testing_Rep__r.LastName, CSA__c,Sales_Director__c, Owner__c, Owner__r.LastName, Area_VP__c,Patient_Testing_Rep__c, Region__c, TM1__c from Custom_Territory__c where Zip_Code__c in :zips Limit 1]) {
        for(Custom_Territory__c ct : cts) {
        System.debug('@@@@@@@@@'+ct.Patient_Testing_Rep__c );
            zipMap.put(ct.Zip_Code__c, ct);
        }
    }
    
    for(Account a : Trigger.new) {
        if(Trigger.isInsert || a.ShippingPostalCode != Trigger.oldMap.get(a.Id).ShippingPostalCode) {
            if(zipMap.containsKey(a.ShippingPostalCode)) {
                a.Territory__c = zipMap.get(a.ShippingPostalCode).Territory__c;
                a.Assigned_Territory__c = zipMap.get(a.ShippingPostalCode).Name;
                if(zipMap.get(a.ShippingPostalCode).Owner__c != null) {
                    a.OwnerId = zipMap.get(a.ShippingPostalCode).Owner__c;
if(a.Sales_Credit_Type__c == 'Patient Testing') {
                    a.OwnerId= zipMap.get(a.ShippingPostalCode).Patient_Testing_Rep__c; 
                    System.Debug('********'+zipMap.values());
}
else {
a.OwnerId = zipMap.get(a.ShippingPostalCode).Owner__c;
}
                    a.TM1__c = zipMap.get(a.ShippingPostalCode).TM1__c;
                    a.Region__c = zipMap.get(a.ShippingPostalCode).Region__c;
                    a.AVP__c = zipMap.get(a.ShippingPostalCode).Area_VP__c;
                    a.Patient_Testing_Rep__c = zipMap.get(a.ShippingPostalCode).Patient_Testing_Rep__c;
                    a.CSA__c = zipMap.get(a.ShippingPostalCode).CSA__c;
                }
                a.Sales_Director__c = zipMap.get(a.ShippingPostalCode).Sales_Director__c;
            }
            else {
                a.Territory__c = '';
                a.Assigned_Territory__c = 'Default';
            }
        }
    }
}
Janna McNeil 13Janna McNeil 13
@Ajay K Dubedi - I have attempted to insert your suggested code in several places but I am not getting the results that I want. 
@anilbathula@@anilbathula@
Hi Janna,

Try this code hope your country field API name is ShippingCountry ,if its not shipping country please replace the ShippingCountry with your field API name.
 
trigger AccountAssignTerritoryTrigger on Account (before insert, before update) {
    String[] zips = new String[0];
    for(Account a : Trigger.new) {
        if((Trigger.isInsert || a.ShippingPostalCode != Trigger.oldMap.get(a.Id).ShippingPostalCode) && a.Shippingcountry !='United States') {
            zips.add(a.ShippingPostalCode);
            System.debug('#####'+a.ShippingPostalCode);
        }
    }
    
    Map<String, Custom_Territory__c> zipMap = new Map<String, Custom_Territory__c>();
    for(Custom_Territory__c[] cts : [Select Id, Name, Territory__c, Zip_Code__c,Patient_Testing_Rep__r.LastName, CSA__c,Sales_Director__c, Owner__c, Owner__r.LastName, Area_VP__c,Patient_Testing_Rep__c, Region__c, TM1__c from Custom_Territory__c where Zip_Code__c in :zips Limit 1]) {
        for(Custom_Territory__c ct : cts) {
        System.debug('@@@@@@@@@'+ct.Patient_Testing_Rep__c );
            zipMap.put(ct.Zip_Code__c, ct);
        }
    }
    
    for(Account a : Trigger.new) {
        if((Trigger.isInsert || a.ShippingPostalCode != Trigger.oldMap.get(a.Id).ShippingPostalCode)&& a.Shippingcountry!='United States') {
            if(zipMap.containsKey(a.ShippingPostalCode)) {
                a.Territory__c = zipMap.get(a.ShippingPostalCode).Territory__c;
                a.Assigned_Territory__c = zipMap.get(a.ShippingPostalCode).Name;
                if(zipMap.get(a.ShippingPostalCode).Owner__c != null) {
                    a.OwnerId = zipMap.get(a.ShippingPostalCode).Owner__c;
if(a.Sales_Credit_Type__c == 'Patient Testing') {
                    a.OwnerId= zipMap.get(a.ShippingPostalCode).Patient_Testing_Rep__c; 
                    System.Debug('********'+zipMap.values());
}
else {
a.OwnerId = zipMap.get(a.ShippingPostalCode).Owner__c;
}
                    a.TM1__c = zipMap.get(a.ShippingPostalCode).TM1__c;
                    a.Region__c = zipMap.get(a.ShippingPostalCode).Region__c;
                    a.AVP__c = zipMap.get(a.ShippingPostalCode).Area_VP__c;
                    a.Patient_Testing_Rep__c = zipMap.get(a.ShippingPostalCode).Patient_Testing_Rep__c;
                    a.CSA__c = zipMap.get(a.ShippingPostalCode).CSA__c;
                }
                a.Sales_Director__c = zipMap.get(a.ShippingPostalCode).Sales_Director__c;
            }
            else {
                a.Territory__c = '';
                a.Assigned_Territory__c = 'Default';
            }
        }
    }
}

Thanks
Anil.B​​​​​​​
Janna McNeil 13Janna McNeil 13
Hi Anil.B - I've tried that code but the section at the bottom doesn't contain the condition so anything with a different ShippingCountry from United States still has the trigger applied. Any idea on how to add that condition there as well?
@anilbathula@@anilbathula@
Hi Janna,

You mean the else part, change the else part to this code:
 
else if(a.Shippingcountry!='United States'){
                a.Territory__c = '';
                a.Assigned_Territory__c = 'Default';
            }

Thanks
Anil.B
Janna McNeil 13Janna McNeil 13
None of those edits worked. 
@anilbathula@@anilbathula@
So what its Happening and what exactly you want with the bottom section?
Janna McNeil 13Janna McNeil 13
Hi Anil - The current behavior is that when a zip code is added to an account record that the owner is automatically updated by looking it up on my custom territory object. Add those conditions have not prevented the trigger from firing. Should I consider doing the opposite condition for a.ShippingCountry <> 'Germany'?