You need to sign in to do that
Don't have an account?

Trigger not firing for multiple conditions
Hi,
I wrote a below trigger on lead only for condition
if ( !gzip.isEmpty() && !gcountry.isEmpty() && !gstate.isEmpty()) it is getting fired it is not getting fired for next else if condition any thing wrong in my trigger code please suggest me. Also this is making a lookup everytime when user make a change on fields
I wrote a below trigger on lead only for condition
if ( !gzip.isEmpty() && !gcountry.isEmpty() && !gstate.isEmpty()) it is getting fired it is not getting fired for next else if condition any thing wrong in my trigger code please suggest me. Also this is making a lookup everytime when user make a change on fields
trigger territory_lookup on Lead (Before Insert, Before Update) { List<Territory_Lookup__c> territoryLookupList = null; List<Integer> gzip = new List<Integer>(); // we need a list so that we can sort it. set<String> gstate = new set<String>(); set<String> gcountry = new set<String>(); for(Lead l : Trigger.new){ gstate.add(l.state); gcountry.add(l.country); if ( l.postalcode != null) { gzip.add(Integer.valueof(l.postalcode)); } } if ( !gzip.isEmpty() && !gcountry.isEmpty() && !gstate.isEmpty()) { territoryLookupList = [select Theater__c,Region__c,Area__c,User__c FROM Territory_Lookup__c where Zip_Start__c <= :gzip and Zip_End__c >=:gzip limit 1]; } else if ( gzip.isEmpty() && !gcountry.isEmpty() && !gstate.isEmpty()) { territoryLookupList = [select Theater__c,Region__c,Area__c,User__c FROM Territory_Lookup__c where State__c = :gstate and Country__c = :gcountry limit 1]; } else if ( gzip.isEmpty() && !gcountry.isEmpty() && gstate.isEmpty()) { territoryLookupList = [select Theater__c,Region__c,Area__c,User__c FROM Territory_Lookup__c where Country__c = :gcountry limit 1]; } else if ( gzip.isEmpty() && gcountry.isEmpty() && !gstate.isEmpty()) { territoryLookupList = [select Theater__c,Region__c,Area__c,User__c FROM Territory_Lookup__c where State__c = :gstate limit 1]; } for(lead uld : Trigger.new){ Territory_Lookup__c tl = getTerritoryLookup(uld); if(tl !=null){ uld.Territory_Area__c = tl.Area__c; uld.Territory_Region__c = tl.Region__c; uld.Territory_Theater__c = tl.Theater__c; } } public Territory_Lookup__c getTerritoryLookup(Lead lead){ if(territoryLookupList == null) return null; Territory_Lookup__c temp = null; for(Territory_Lookup__c territoryLookup : territoryLookupList){ temp = territoryLookup; break; } return temp; } }
Thanks
Sudhir
You can use debug and see the code is going.
-Thanks
Ashlekh Gera
All Answers
Thanks
Sudhir
You can use debug and see the code is going.
-Thanks
Ashlekh Gera
I even tried alternative way first condition works perfect second is not working I am doing a looking from the method inside the trigger is there any alternative suggestion to tweek the code please suggest me
Thanks
Sudhir