You need to sign in to do that
Don't have an account?
Janna 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.
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.
You need to add the condition in the trigger.
It will be easy to explain ,if you post the trigger code.
Thanks
Anil.B
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
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';
}
}
}
}
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.
Thanks
Anil.B
You mean the else part, change the else part to this code:
Thanks
Anil.B