0

Title says it all (maybe?).

I have code that is well tested and deployed. It iterates over roughly 99.8% of records without issue.. Every 500th record or so I will get a "cannot de-reference a null object" error.

Here's how I get to that error:

  1. I build a set of Account IDs from a formula field in the Account object that navigates the account hierarchy and returns a given account ID. This formula field cannot return null values based on how it's built. If the account hierarchy is non-exist it returns the account id of the given record.
  2. I build a map that takes an Account ID and returns an Account record (again navigating up to a desired point of the account hierarchy and retrieving that full account record)
  3. I build a map of zip codes and some associated information I want about that zip code and how we decide ownership, etc..
  4. I take my first map (Child Account ID to Parent Account Object) and then map that into my second map (Zip Code to associated metadata about zip codes in custom object).

This returns a null record for every 500th record.

When I debug two records, one that works and one that does not, the debug log on the error file shows that the formula field referencing an Account ID in my first step is not present in the account variable. For records that this works on, I can see it clearly in the debug.

What is causing this? I figured this would be a creative way to easily navigate account hierarchies while getting around the circular reference that Salesforce by default prevents. Is the system doing some evaluation in the event my formula field points be back to the account and erroring out?

        Set<Id> pAccIds = new Set<Id>() ;
        For( Account accs : newAcc ) {
            ID pAccId = accs.Highest_Parent_ID_for_Territory_Assign__c ;
            pAccIds.add(pAccId);
        }
        
        system.debug('pAccIds after process' + pAccIds) ;
        
        Map<String, Account> pAccIdtopTerrParAccount = new Map<String, Account>() ;     
        For( Account pAccLoop : 
            [Select Id, Segment__c, BillingPostalCode, BillingCity, BillingState, BillingCountry 
             From Account Where Id in: pAccIds]) {
                 string pAcc15 = string.valueOf(pAccLoop.Id).subString(0,15);
                 pAccIdtopTerrParAccount.put(pAcc15, pAccLoop);
            }
        
        system.debug('pAccIdObjectMap' + pAccIdtopTerrParAccount) ;            
        
        // Pull territories with zip codes
        // Split zip codes into list
        // Create map for individual zip code to territory ID
        
        Map<String, SOPS_Territory_Extension__c> zipToSOPSTerr = new Map<String, SOPS_Territory_Extension__c>() ;
        Set<String> zips = new Set<String>() ;
        For( SOPS_Territory_Extension__c zipT : 
            [Select Id, Name, Zip_Codes_Included__c, Commercial_Territory_Name__c From SOPS_Territory_Extension__c 
             WHERE Uses_Zip_Codes__c = TRUE]) {
                 zips.addAll(zipT.Zip_Codes_Included__c.split(';')) ;
                 List<String> zipCodes = new List<String>(zipT.Zip_Codes_Included__c.split(';')) ;
                 For( String z : zipCodes) {
                     zipToSOPSTerr.put(z, zipT) ;
                 }
             }
        
        system.debug('zipToSOPSTerr Map' + zipToSOPSTerr) ;
        
        For( Account accs1 : newAcc ) {
            If( zips.contains(pAccIdtopTerrParAccount.get(accs1.Highest_Parent_ID_for_Territory_Assign__c).BillingPostalCode) 
               && linkedAccs.contains(accs1.Id) == false
               && pAccIdtopTerrParAccount.get(accs1.Highest_Parent_ID_for_Territory_Assign__c).BillingPostalCode != '' 
               && pAccIdtopTerrParAccount.get(accs1.Highest_Parent_ID_for_Territory_Assign__c).BillingPostalCode != Null) { 

Happy to drop in full debug files but the interesting parts are shown below.

Record that works (does contain the field "Highest_Parent_ID_for_Territory_Assign__c"):

22:40:36.391 (408813968)|VARIABLE_ASSIGNMENT|[51]|accs|{"ShippingLatitude":42.483210000000000,"LastModifiedDate":"2021-02-01T06:40:37.000Z","Agreed_to_Case_Study__c":false,"Account_Health__c":"Good","Domain_2__c":"covisint.com","Has_Closed_Won_Oppty__c":0,"dfox__Year_Founded__c":2000,"Postbound_Automation_ICP_Criteria_Met__c":true,"Owner_Role__c":"VP Sales","SDR_Owner2__c":" ","IsDeleted":false,"saasoptics__closed_won_opp_count__c":0,"dfox__LinkedIn_URL__c":"https://www.linkedin (18 more) ...","LeanData__Reporting_Customer__c":false,"Account_ID_18__c":"00136000019zTlvAAE","dfox__Lists__c":"[Tier 2] [Salesforce (32 more) ...","WebEx_dial_in__c":"6508627207","LeanData__Reporting_Target_Account_Number__c":0,"Segment_2__c":"1. Mid Market","dfox__Techstack__c":"Marketo, Liferay, Go (1585 more) ...","LeanData__Search__c":"00019zTlvAAE w:covis (30 more) ...","ARR__c":0.00,"ShippingPostalCode":"48076","ShippingState":"Michigan","Id":"00136000019zTlvAAE","dfox__HR_Score__c":775,"Tier__c":"Tier 2","Description":"Covisint Corp. provi (480 more) ...","Employee_Count_Source__c":"LinkedIn","Domain__c":"covisint.com","Website":"covisint.com","BillingLatitude":42.496000000000000,"NumberOfEmployees":330,"dfox__Crunchbase_URL__c":"https://www.crunchba (28 more) ...","dfox__Web_Traffic_Estimate__c":1190777,"AE_Target_Account__c":false,"Forecasted_Value__c":0.00,"Reserved__c":false,"OwnerId":"005360000071oAeAAI","BillingLongitude":-83.231800000000000,"dfox__Revenue_Estimate__c":70243000,"ShippingCity":"Southfield","recurly_v2__Past_Due_Recurly_Invoice__c":false,"dfox__Last_Synced_Date__c":"2021-01-29T22:51:03.000Z","Size_of_Prize_SOP__c":0.00,"ParentId":"0013600001qGHuuAAG","dfox__Alexa_Rank__c":80515,"Employees_Linkedin__c":330.0,"PFR__c":false,"leadfeederapp__LeadfeederLink__c":"https://app.leadfeed (22 more) ...","Alert_Set_Up_for_DM__c":false,"dfox__Url__c":"https://app.datafox. (34 more) ...","Account_Link__c":"https://chorusapp.my (34 more) ...","SystemModstamp":"2021-02-01T06:40:37.000Z","Round_Robin_ID__c":"20190","Type":"Target","BillingCountry":"United States","LastActivityDate":"2019-11-11T00:00:00.000Z","LeanData__Reporting_Has_Opportunity__c":false,"bizible2__Engagement_Score__c":"D","ShippingLongitude":-83.241335000000000,"Recently_Closed_Opps__c":0,"Cluster_ID__c":"XXSEAR","BillingCity":"Southfield","dfox__Finance_Score__c":1005,"Sales_Employees_Linkedin__c":30.0,"Funding_in_Last_6_Months__c":false,"affectlayer__AffectLayer_Notes__c":"_HL_ENCODED_/apex/af (102 more) ...","Name":"Covisint Corporation","Industry":"Information Technolo (2 more) ...","Highest_Parent_ID_for_Territory_Assign__c":"00136000019zTlv","Ultimate_Parent_ID__c":"0013600001qGHuu","IsExcludedFromRealign":false,"CreatedById":"005360000056elzAAA","Ignore_Hierarchy_in_Territory_Assignment__c":false,"Longitude__c":-83.231800000000000,"Account_Stage__c":"Prospect","BillingGeocodeAccuracy":"Zip","BillingPostalCode":"48076","dfox_Account_Score__c":200,"dfox__Stage__c":"Acquired","recurly_v2__Recurly_In_Trial__c":false,"Segment__c":"3. Commercial","dfox__Keywords__c":"identity management, (179 more) ...","DPA__c":false,"Employee_Source_Refresh_Date__c":"2020-10-31T00:00:00.000Z","Open_Opportunities__c":0,"ShippingGeocodeAccuracy":"Block","dfox__Twitter_Handle__c":"covisint","ShippingStreet":"26533 Evergreen Road","dfox__Id__c":"5130f0ad8989846a3603 (4 more) ...","Active_Partner__c":false,"Not_a_Fit__c":false,"Owners_Set_From_Team__c":false,"Latitude__c":42.496000000000000,"CreatedDate":"2017-06-20T15:51:50.000Z","BillingState":"Michigan","Today_Less_90_Days__c":"2020-11-02T00:00:00.000Z","recurly_v2__Recurly_Active_Subscriber__c":false,"dfox__Sourced_From_DataFox__c":false,"BDR_Owner_Full_Name__c":" ","Account_Tier_Review__c":false,"Account_Status__c":"Not Working","dfox__Growth_Score__c":954,"Power_Of_One__c":1,"dfox__DataFox_Score__c":976,"dfox__Twitter_Followers__c":1396,"Phone":"800.229.4125","Churn_Risk__c":false,"ShippingCountry":"United States","LinkedIn_Company_Page_URL__c":"http://www.linkedin. (17 more) ...","Customer_ARR__c":0.00,"LeanData__Reporting_Target_Account__c":false,"Account_Value__c":0.00,"DF_Drift__c":false,"Account_Sync_to_Zendesk__c":false,"Do_Not_Re_Assign__c":true,"Email_provider__c":"Unknown","LeanData__LD_EmailDomains__c":"00136000019zTlvAAE w (13 more) ...","Round_Robin_ISR__c":1,"Count_Open_non_Renewal_Opportunities__c":0,"DF_IS2__c":false,"BillingStreet":"26533 Evergreen Road (32 more) ...","CRM__c":"Salesforce","ARR_Pre_Churn__c":0.00,"dfox__Influence_Score__c":1204,"LastModifiedById":"0051Q00000TrB6XQAV","dfox_Company_Number_of_Employees__c":340,"Technologies__c":"CSS: max-width, F5 B (222 more) ..."}|0x51593348

Record that does not work (does not contain the field "Highest_Parent_ID_for_Territory_Assign__c"):

22:36:11.776 (842504414)|VARIABLE_ASSIGNMENT|[51]|accs|{"ShippingLatitude":41.879727000000000,"LastModifiedDate":"2021-02-01T06:36:11.000Z","Agreed_to_Case_Study__c":false,"Account_Health__c":"Good","Domain_2__c":"emmisolutions.com","Has_Closed_Won_Oppty__c":0,"dfox__Year_Founded__c":2002,"Postbound_Automation_ICP_Criteria_Met__c":true,"Owner_Role__c":"SFDC Admin","SDR_Owner2__c":" ","IsDeleted":false,"saasoptics__closed_won_opp_count__c":0,"dfox__LinkedIn_URL__c":"https://www.linkedin (18 more) ...","LeanData__Reporting_Customer__c":false,"Account_ID_18__c":"00136000019zTnAAAU","dfox__Lists__c":"[Salesforce] [ClearS (5 more) ...","WebEx_dial_in__c":"6508627207","LeanData__Reporting_Target_Account_Number__c":0,"Segment_2__c":"1. Mid Market","dfox__Techstack__c":"Google Font API, GoD (919 more) ...","LeanData__Search__c":"00019zTnAAAU w:emmis (38 more) ...","ARR__c":0.00,"ShippingPostalCode":"60606","ShippingState":"Illinois","Id":"00136000019zTnAAAU","dfox__HR_Score__c":775,"Description":"Emmi Solutions is a  (118 more) ...","Employee_Count_Source__c":"LinkedIn","Domain__c":"emmisolutions.com","Website":"emmisolutions.com","BillingLatitude":41.879727000000000,"NumberOfEmployees":301,"dfox__Crunchbase_URL__c":"https://www.crunchba (34 more) ...","dfox__Web_Traffic_Estimate__c":82367,"AE_Target_Account__c":false,"Forecasted_Value__c":0.00,"Reserved__c":false,"OwnerId":"005360000071aA5AAI","BillingLongitude":-87.635681000000000,"dfox__Revenue_Estimate__c":30000000,"ShippingCity":"Chicago","recurly_v2__Past_Due_Recurly_Invoice__c":false,"dfox__Last_Synced_Date__c":"2021-01-29T22:51:15.000Z","Size_of_Prize_SOP__c":0.00,"ParentId":"0011Q00002KM8XyQAL","dfox__Alexa_Rank__c":1139583,"Employees_Linkedin__c":301.0,"PFR__c":false,"Alert_Set_Up_for_DM__c":false,"dfox__Url__c":"https://app.datafox. (28 more) ...","Account_Link__c":"https://chorusapp.my (34 more) ...","SystemModstamp":"2021-02-01T06:36:11.000Z","Round_Robin_ID__c":"20264","Type":"Target","BillingCountry":"United States","LastActivityDate":"2017-06-26T00:00:00.000Z","LeanData__Reporting_Has_Opportunity__c":false,"bizible2__Engagement_Score__c":"D","ShippingLongitude":-87.635681000000000,"Recently_Closed_Opps__c":0,"Cluster_ID__c":"FH7TEW","BillingCity":"Chicago","dfox__Finance_Score__c":983,"Sales_Employees_Linkedin__c":35.0,"Funding_in_Last_6_Months__c":false,"affectlayer__AffectLayer_Notes__c":"_HL_ENCODED_/apex/af (102 more) ...","Name":"Emmi Solutions","Industry":"Information Technolo (2 more) ...","IsExcludedFromRealign":false,"CreatedById":"005360000056elzAAA","Ignore_Hierarchy_in_Territory_Assignment__c":false,"Longitude__c":-87.635681000000000,"Account_Stage__c":"Prospect","BillingGeocodeAccuracy":"Address","BillingPostalCode":"60606","dfox_Account_Score__c":180,"dfox__Stage__c":"Product","recurly_v2__Recurly_In_Trial__c":false,"Segment__c":"3. Commercial","dfox__Keywords__c":"healthcare, medical, (85 more) ...","DPA__c":false,"Employee_Source_Refresh_Date__c":"2020-10-17T00:00:00.000Z","Open_Opportunities__c":0,"ShippingGeocodeAccuracy":"Address","dfox__Twitter_Handle__c":"EmmiSolutions","ShippingStreet":"300 West Adams Stree (1 more) ...","dfox__Id__c":"5130f0aa8989846a3603 (4 more) ...","Active_Partner__c":false,"Web_Conferencing__c":"Clearslide","Not_a_Fit__c":false,"Owners_Set_From_Team__c":false,"Latitude__c":41.879727000000000,"CreatedDate":"2017-06-20T15:51:50.000Z","BillingState":"Illinois","Today_Less_90_Days__c":"2020-11-02T00:00:00.000Z","recurly_v2__Recurly_Active_Subscriber__c":false,"dfox__Sourced_From_DataFox__c":false,"BDR_Owner_Full_Name__c":" ","Account_Tier_Review__c":false,"Account_Status__c":"Not Working","dfox__Growth_Score__c":954,"Power_Of_One__c":1,"dfox__DataFox_Score__c":965,"dfox__Twitter_Followers__c":1419,"Phone":"13122363650","Churn_Risk__c":false,"ShippingCountry":"United States","LinkedIn_Company_Page_URL__c":"http://www.linkedin. (17 more) ...","Customer_ARR__c":0.00,"LeanData__Reporting_Target_Account__c":false,"Account_Value__c":0.00,"DF_Drift__c":false,"Account_Sync_to_Zendesk__c":false,"Do_Not_Re_Assign__c":true,"LeanData__LD_EmailDomains__c":"00136000019zTnAAAU w (18 more) ...","Round_Robin_ISR__c":1,"Count_Open_non_Renewal_Opportunities__c":0,"DF_IS2__c":false,"BillingStreet":"300 W Adams St, Suit (6 more) ...","CRM__c":"Salesforce","ARR_Pre_Churn__c":0.00,"dfox__Influence_Score__c":1204,"LastModifiedById":"0051Q00000TrB6XQAV","dfox_Company_Number_of_Employees__c":150,"Technologies__c":"Google Universal Ana (156 more) ..."}|0x38447d4b

** Edit: Would also like to add I know I can insert a "try" function on my map to determine if the map is going to throw an error. That won't solve the route cause for me here. ** 2nd Edit: Clarifying that the formula field reference in bullet 1 and that is the root cause cannot be a null value based on how it's built.

2
  • Is the problem purely about the null formula field value?
    – Nick C
    Feb 1, 2021 at 7:45
  • The formula does not return null in any scenarios as it references either the account itself or an account above it in the hierarchy. When I check this in system with select count(id) from account where highest_parent_id_for_territory_assign__c = null it returns no records.
    – j.sweet
    Feb 1, 2021 at 8:13

0

You must log in to answer this question.

Browse other questions tagged .