• Rookie Developer
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 5
    Replies

I discovered geocodes could be added to Accounts without needing code but for the sake of learning, could someone provide a newbie with constructive feedback on this trigger? I'd really like to figure this out.

I have two sales reps splitting California for their sales territory. One owns anything above Santa Barbara and one owns everything else. I created a custom object labeled 'Cities' with fields for City__c and Latitude__c, and imported this data for all California cities.

Whenever an Account is created or updated, I want my trigger to query the custom object 'Cities', match the Account BillingCity to the City__c field on my custom object, and evaluate if the corresponding Latitude__c field on my custom object is greater than Santa Barbara's latitude.  

If the latitude is greater than Santa Barbara's, add the Account to a list which will be updated with the Northern Sales Rep as the Owner. If the latitude is not greater than Santa Barbara's, add the Account to a list which will be updated with the Southern Sales Rep as the Owner.

When I test the trigger, I get the dreaded "System.NullPointerException: Attempt to de-reference a null object ()" error which confuses me because I check to see if BillingCity is null and confirmed I have all California cities and latitudes imported into my custom object. I have a feeling the error is somewhere around line 13 where I query my custom object and try to find the corresponding latitude...

Thanks to anyone for taking the time to help me learn! 

Here is my trigger:

trigger CaliSalesTerritory on Account (before insert, before update) {

    List <Account> caliAccts; // A List to hold all California Accounts
    List <Account> norCalCities; // A List to hold all Northern California Accounts
    List <Account> soCalCities; // A List to hold all Southern California Accounts
    
    for (Account acct : Trigger.new) { 
        if (acct.BillingState == 'California' && acct.BillingCity != null) {
            caliAccts.add(acct); 
// For all Accounts being inserted or updated, if the BillingState is "California" and the BillingCity isn't null, add the Account to the caliAccts list    
        }
    } 
   
    for (Account acct2 : caliAccts) {
        Cities__c caliLatitude = [Select Latitude__c FROM Cities__c WHERE City__c = :acct2.BillingCity]; 
 // For all Accounts in the caliAccts list, query the Cities__c object and return the Latitude based on the Account's BillingCity
       
    if (caliLatitude.Latitude__c > 34.4285) {
           norCalCities.add(acct2);
// If the Latitude is greater than Santa Barbara's latitude, add the Account to the norCalCities list            
        }    
            else {
                soCalCities.add(acct2);
// If the Latitude is not greater than Santa Barbara's latitude, add the Account to the soCalCities list                            
            }
    }
           
    for (Account northRepAcct : norCalCities) {
        northRepAcct.OwnerId = '0050f0000097wqI'; //Northern Sales Rep UserId
// Assign all Accounts in the norCalCities list to the Northern Sales Rep
    }  
    update norCalCities;
    
    for (Account southRepAcct : soCalCities) {
        southRepAcct.OwnerId = '0050f000009K2kr'; //Souther Sales Rep UserId
// Assign all Accounts in the soCalCities list to Southern Sales Rep        
    } 
    update soCalCities;
}

I discovered geocodes could be added to Accounts without needing code but for the sake of learning, could someone provide a newbie with constructive feedback on this trigger? I'd really like to figure this out.

I have two sales reps splitting California for their sales territory. One owns anything above Santa Barbara and one owns everything else. I created a custom object labeled 'Cities' with fields for City__c and Latitude__c, and imported this data for all California cities.

Whenever an Account is created or updated, I want my trigger to query the custom object 'Cities', match the Account BillingCity to the City__c field on my custom object, and evaluate if the corresponding Latitude__c field on my custom object is greater than Santa Barbara's latitude.  

If the latitude is greater than Santa Barbara's, add the Account to a list which will be updated with the Northern Sales Rep as the Owner. If the latitude is not greater than Santa Barbara's, add the Account to a list which will be updated with the Southern Sales Rep as the Owner.

When I test the trigger, I get the dreaded "System.NullPointerException: Attempt to de-reference a null object ()" error which confuses me because I check to see if BillingCity is null and confirmed I have all California cities and latitudes imported into my custom object. I have a feeling the error is somewhere around line 13 where I query my custom object and try to find the corresponding latitude...

Thanks to anyone for taking the time to help me learn! 

Here is my trigger:

trigger CaliSalesTerritory on Account (before insert, before update) {

    List <Account> caliAccts; // A List to hold all California Accounts
    List <Account> norCalCities; // A List to hold all Northern California Accounts
    List <Account> soCalCities; // A List to hold all Southern California Accounts
    
    for (Account acct : Trigger.new) { 
        if (acct.BillingState == 'California' && acct.BillingCity != null) {
            caliAccts.add(acct); 
// For all Accounts being inserted or updated, if the BillingState is "California" and the BillingCity isn't null, add the Account to the caliAccts list    
        }
    } 
   
    for (Account acct2 : caliAccts) {
        Cities__c caliLatitude = [Select Latitude__c FROM Cities__c WHERE City__c = :acct2.BillingCity]; 
 // For all Accounts in the caliAccts list, query the Cities__c object and return the Latitude based on the Account's BillingCity
       
    if (caliLatitude.Latitude__c > 34.4285) {
           norCalCities.add(acct2);
// If the Latitude is greater than Santa Barbara's latitude, add the Account to the norCalCities list            
        }    
            else {
                soCalCities.add(acct2);
// If the Latitude is not greater than Santa Barbara's latitude, add the Account to the soCalCities list                            
            }
    }
           
    for (Account northRepAcct : norCalCities) {
        northRepAcct.OwnerId = '0050f0000097wqI'; //Northern Sales Rep UserId
// Assign all Accounts in the norCalCities list to the Northern Sales Rep
    }  
    update norCalCities;
    
    for (Account southRepAcct : soCalCities) {
        southRepAcct.OwnerId = '0050f000009K2kr'; //Souther Sales Rep UserId
// Assign all Accounts in the soCalCities list to Southern Sales Rep        
    } 
    update soCalCities;
}