0

My requirement is to get the list of territories present in an Account_Territory_Loader_vod__c object for a particular account and check that combination of account and territory whether it is present in TSF_vod__c or not. I am getting the following error : Attempt to de-reference a null object

Following is the code I have written :

global class AccountTerritoryCombination implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'SELECT Account_vod__c,Id,Territory_vod__c FROM Account_Territory_Loader_vod__c';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Account_Territory_Loader_vod__c> scope)
    {    
         List<Id> Acc = new List<Id>(); 
         List<String> Ter = new List<String>(); 
         for(Account_Territory_Loader_vod__c ac: scope){
             Acc.add(ac.account_vod__c);
             if(ac.Territory_vod__c.length()>0){
                 Ter.add(ac.territory_vod__c);    
             }
         }  
         List<TSF_vod__c> TSF = [select id,account_vod__c,territory_vod__c from TSF_vod__c where account_vod__c IN: Acc and territory_vod__c IN:Ter];
         for(Account_Territory_Loader_vod__c a: scope){
             integer i =0; 
             String S1;
             if(a.territory_vod__c.length()>0){
                 S1 = a.territory_vod__c;            
                 List<String> S2 = S1.split(';');
                 for(String S: S2){
                     for(TSF_vod__c t: TSF){
                         if(t.account_vod__c == a.account_vod__c && t.territory_vod__c == S){
                             system.debug('The combination is present');
                         }
                         else{
                             system.debug('The combination is not present');
                         }
                     }
                 }
            }
        }
        update scope;
    }

    global void finish(Database.BatchableContext BC)
    {
    }
}

Please tell me how to filter the account_territory_loader records where territory_vod__c is not null. Territory_vod__c is long text area so we can not filter using query. Please let me know if there are errors in this code.

1 Answer 1

8

Change both instances of :

if(ac.Territory_vod__c.length()>0){

to:

if (ac.Territory_vod__c != null && ac.Territory_vod__c.length() > 0) {

Expressions like these evaluate from left to right and because it is an "and" expression evaluation will stop at the first false term i.e. when ac.Territory_vod__c is null so the second term will not be evaluated and so not result in a null pointer exception.

PS Your current algorithm has a triply nested loop which means the execution time could rise dramatically if the number of elements looped over becomes large. The code below eliminates one of those loops by using a set. It also includes the split in the first loop: that should be in both loops (if it is a multi-select picklist).

Set<Id> acc = new Set<Id>(); 
Set<String> ter = new Set<String>(); 
for(Account_Territory_Loader_vod__c ac : scope) {
    acc.add(ac.account_vod__c);
    if (a.territory_vod__c != null && ac.Territory_vod__c.length() > 0) {
        ter.addAll(ac.territory_vod__c.split(';'));    
    }
}
Set<String> tsfAccountTerritory = new Set<String>();
for (TSF_vod__c tsf : [
        select id, account_vod__c, territory_vod__c
        from TSF_vod__c
        where account_vod__c IN: acc
        and territory_vod__c IN: ter
        ]) {
    String key = tsf.account_vod__c + '::::' + tsf.territory_vod__c;
    tsfAccountTerritory.add(key);
}
for(Account_Territory_Loader_vod__c ac : scope){
    if (ac.territory_vod__c != null && ac.territory_vod__c.length() > 0) {
        for (String t : ac.territory_vod__c.split(';')) {
            String key = ac.account_vod__c + '::::' + t;
            if (tsfAccountTerritory.contains(key) {
                system.debug('The combination is present');
            } else {
                system.debug('The combination is not present');
            }
        }
    }
}
3
  • Thank you very much Keith for your help. In the last part, I need to generate the report of combination not present in TSF and send them in an e-mail to a group of users. Can you please help me in this regard. It will be so helpful. Commented Jun 17, 2014 at 18:56
  • Hi Keith, In the above code which you have written, In String 'Key', I am not getting territory_vod__c while accound_vod___c is there. Please help me in this regard. Commented Jun 19, 2014 at 17:31
  • @NitinVarshney I'm afraid you'll need to debug this (print out the results of the queries and values of varies - see How do I start to debug my own Apex code?) to see where it is going wrong.
    – Keith C
    Commented Jun 22, 2014 at 15:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .