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
TilluTillu 

Below code is not executed properly ?

Hi,

I have a small problem while executing the If , else statements....

i am working with Contact,Territory objects...if  contacts  zip code  matches with the condition Then  correct territory record  would need to be displayed on Contact.

Here is Condition...If  contact's Account name  is equals  to  Territory field  Protect name  Then the Territory record should be pulled on to contact. Otherwise  other territory record need to be pulled on contact.

For below   if/else  at a time only one condition executed....when i remove else statement it ables to  pull the  matched one...

when i add the else  it always  executes else statement...even when it  satisfies if statement....





try
          {  
                  if(tListForecare.size() > 0)
                  {
                   for(Territory__c t: tListForecare) //null create a map of State to territory. all go in this comparison
                   {
                        if(!ziplocked)
                        {
                            if(t.zip_start__c != null && t.zip_end__c != null && c.mailingPostalCode != null&&t.Protected_Accounts__c !=null)
                            {
                                if(t.Protected_Accounts__c.equals(aIBDMap.get(c.accountId).name)&&c.mailingPostalCode.substring(0,5) >= t.zip_start__c &&
                            c.mailingPostalCode.substring(0,5) <= t.zip_end__c)
                                {
                                    c.Forecare_Territory__c = t.Id;
                                    ziplocked = true;
                                }                          
                        else if(c.mailingState == t.state__c &&
                            (t.zip_start__c == null || t.zip_start__c == null) &&
                            t.Id!= null  )
                            {
                                c.Forecare_Territory__c = t.Id;
                            }
                      }       
   }
}
Ankit AroraAnkit Arora
It's difficult to answer this, as you are using custom fields. Still would suggest you to please apply debug before the if statements and let us know the results that what exact values you are getting for example :

for this t.Protected_Accounts__c.equals(aIBDMap.get(c.accountId).name)

for this : c.mailingPostalCode.substring(0,5) >= t.zip_start__c

and for this : c.mailingPostalCode.substring(0,5) <= t.zip_end__c