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
SFDC 2017SFDC 2017 

Issue in Assigning Id is not assigning to a lookup field in Account

I have a scenario account and custom object Zip .So if the account Billing postal code is same as zip object zip field then in Accountt Zip lookup field will get updated .For Billing code like 75020-5678 it has to take the first 5 andcompare with zip object with that 5 characters.My code is as below .The Lookup field is not updating 
 Set<String> zipCodes = new Set<String>();
    Set<String> countryCodes = new Set<String>();
    Set<String> accountSegments = new Set<String>();
    List<String> zipCodes_Sorted = new List<String>();
    String zipStartMin, zipStartMax;
    List<Account> lstAccountsToUpdateZipTerritoryName = new List<Account>();
    System.Debug('%%%%%%LIST OF ACCOUNTS'+lstAccountsToUpdateZipTerritoryName);
    System.Debug('&&&&&&mapOldAccounts '+mapOldAccounts);
    if(mapOldAccounts != null && mapOldAccounts.size() > 0) {
      for(Account accRec : newAccounts) {
        if(String.IsNotBlank(accRec.BillingPostalCode) && String.IsNotBlank(accRec.BillingCountryCode) && String.IsNotBlank(accRec.Account_Segment__c)
          && (
           !accRec.BillingPostalCode.equalsIgnoreCase(mapOldAccounts.get(accRec.Id).BillingPostalCode) ||
           !accRec.BillingCountryCode.equalsIgnoreCase(mapOldAccounts.get(accRec.Id).BillingCountryCode) ||
           !accRec.Account_Segment__c.equalsIgnoreCase(mapOldAccounts.get(accRec.Id).Account_Segment__c)
          )) {
          lstAccountsToUpdateZipTerritoryName.add(accRec);
          System.Debug('@@@@@@@@Account List'+lstAccountsToUpdateZipTerritoryName);
          System.debug('LIST ACCOUNT SIZE'+lstAccountsToUpdateZipTerritoryName.size());
          System.Debug('*******%%%%Account RECORD'+accRec);
          if(accRec.BillingCountryCode=='US')
          {
            String s=accRec.BillingPostalCode;
            System.Debug('&&&&&BILLINGPOSTAL CODE'+s);
            String s2=s.mid(0,5);
            zipCodes.add(s2);
            zipCodes.add(s2);
            System.Debug('*****%%%%%%%%'+s2);
            System.Debug('@@@@ZIPCODES'+zipCodes);
          }
          else 
          {
              zipCodes.add(accRec.BillingPostalCode);
           }

          countryCodes.add(accRec.BillingCountryCode);
          accountSegments.add(accRec.Account_Segment__c);
        } else if(String.isNotBlank(mapOldAccounts.get(accRec.Id).BillingPostalCode) && String.IsBlank(accRec.BillingPostalCode)) {
          accRec.ZipAssignments__c = null;
        }
      }

      zipCodes_Sorted.addAll(zipCodes);
      zipCodes_Sorted.sort();
      System.Debug('%%%%%%%%%%%%SORTED ZIPCODES'+zipCodes_Sorted);
      System.Debug('$$$$$$Sorted ZipCodes Size'+zipCodes_Sorted.size());
      // Bulk Support
      if(lstAccountsToUpdateZipTerritoryName.size() >= 1 && zipCodes_Sorted.size() > 0) {//It is entering to this loop but after 
        zipStartMin = zipCodes_Sorted[0];
        zipStartMax = zipCodes_Sorted[zipCodes_Sorted.size() -1];
        System.Debug('&&&&&&&ZIPSTARTMIN'+zipStartMin);
        System.Debug('!!!!!!!ZIPSTARTMIN'+zipStartMax);
        List<ZipAssignments__c> sortedZipAssignments_IN = [ SELECT  Id, Zipcode_Start__c, Zipcode_End__c,
                                                                    Account_Segment__c, Country_Code__c
                                                            FROM    ZipAssignments__c
                                                            WHERE   Action__c = :IS_Constants.CONST_INSERT
                                                            AND     Zipcode_Start__c <= :zipStartMin
                                                            AND     Zipcode_End__c >= :zipStartMax
                                                            AND     Country_Code__c IN :countryCodes
                                                            ORDER BY Zipcode_Start__c];
             System.debug('^^^^^^^^SORTEDZIPASSIGNMENTS'+sortedZipAssignments_IN );//till here it is executing
        for(Account objAccount : lstAccountsToUpdateZipTerritoryName) {
          objAccount.ZipAssignments__c = null;
          for(ZipAssignments__c objZip : sortedZipAssignments_IN) {
            if(objZip.Country_Code__c.equalsIgnoreCase(objAccount.BillingCountryCode) &&
               objZip.Account_Segment__c.equalsIgnoreCase(objAccount.Account_Segment__c) &&
               objZip.Zipcode_Start__c <= objAccount.BillingPostalCode && objZip.Zipcode_End__c >= objAccount.BillingPostalCode ) {
              objAccount.ZipAssignments__c = objZip.Id;
               System.Debug('ACCOUNT ZIP'+objZip.Id);
              break;
            }
          }
        }
      }

Any one look into it and kindly provide what is the mistake i am doing
Magesh Mani YadavMagesh Mani Yadav
Hi Archu,

Is this the final code? and is this in the before trigger logic? because am not able to see the update DML for the account
 
Magesh Mani YadavMagesh Mani Yadav

Hi Archu

I think you are adding s2 twice in the list you can comment one of them
zipCodes.add(s2);
zipCodes.add(s2);

and in the BULK code
instead of using 
AND     Zipcode_Start__c <= :zipStartMin
AND     Zipcode_End__c >= :zipStartMax

Use this

AND     Zipcode_Start__c >= :zipStartMin
AND     Zipcode_End__c <= :zipStartMax

Hope this may sovle your issue.