-2

I am getting error

system.NullpointerException:Attempt to de-reference a null object

It is working fine in sandbox once i deploy in production it gave me error as above.

set<String> unqName = new Set<String>();
List<Account> actLst = new List<Account>();
Map<String,List<Account>> act = new Map<String,List<Account>>(); // add list item to map

for(Account a:[SELECT Id,Name,duplicate__c FROM Account where Id In (Select Account_Name__c FROM Opportunity__c)])
{
    unqName.add(a.Name);
}

for(Account a:[SELECT Id,Name,duplicate__c FROM Account where Id In (Select AccountId FROM Contact)])
{
    unqName.add(a.Name);
}

List<AggregateResult> acc=[SELECT Name accname,  Prospect_Code__c  , COUNT(Id) ids FROM Account where Id Not
                           In (Select Account_Name__c FROM Opportunity__c) And Id Not In (Select AccountId FROM Contact)                           
                           GROUP BY Name, Prospect_Code__c  HAVING count(Id)>1];

List<Account> accList = [SELECT Id,Name,duplicate__c FROM Account where Id Not
                           In (Select AccountId from Contact) And Id Not In (Select Account_Name__c FROM Opportunity__c)];

for(Account acc : accList)
{
    if(!act.containsKey(acc.Name))
        {
            act.put(acc.Name,new List<Account>{acc});
        }
    else 
        {
            act.get(acc.Name).add(acc);
           // System.debug(act.get(acc.Name));
        }

}

System.debug('Name' + unqName);

for(String tmp : act.keySet()){
    for(String s:unqName){
        if(s!=tmp){
            for(Account a:act.get(tmp)){
                if(act.get(tmp).indexOf(a) != 0)
                {
                    a.duplicate__c = True;
                    actLst.add(a);
                }
            }
        }
        else{
            for(Account a:act.get(s)){         //error here....
                    a.duplicate__c = True;
                    actLst.add(a);
            }
        }
    }
}

System.debug(' actLst size '+ actLst.size());

        if(!actLst.isEmpty()){
            update accList;
            System.debug('accList size ' + actLst.size());
            System.debug('accList ' + actLst);
        }
  • 1
    Please edit to explain where you receive this exception. – David Reed Jul 2 at 13:33
  • done Please check – Palash Jul 2 at 13:35
  • 1
    Welcome! I've answered your question, but please review How to Ask to improve future questions here. More detail about what you've tried and where you are stuck would have been helpful here. – Thomas Taylor Jul 2 at 14:07
1

The problem has to do with case sensitive vs. insensitive string equality.

TL;DR; use the current act key, tmp, rather than the current unqName value, s to get the map value:

        else{
            for(Account a:act.get(tmp)){

You hit the else when s!=tmp is false, ie, when s == tmp is true. From the answer and link at this question, we see that == is case-insensitive for Strings. But Map keys that are strings are case sensitive. So 2 Account.Name values, General Electric and gEneral eLectric, would be ==, but Map.get('General Electric') will fail if there's only a map key 'gEneral eLectric'.

  • Thanks @Thomas Taylor but i am getting System.LimitException: Apex CPU time limit exceeded .....as i have more than 3000 records – Palash Jul 3 at 4:59
  • Sorry to hear that. That's separate from your question, though. CPU timeouts are a whole different issue - you need to consider not just this code, but any triggers or declarative automation your updates may cause to run. – Thomas Taylor Jul 3 at 5:07
0

I found the solution using APEX BATCH

global class findDuplicateAccountBatchClass implements Database.Batchable<sObject>,  Database.Stateful {
    global set<String> unqName = new Set<String>();
    global List<Account> actLst = new List<Account>();
    global Map<String, List<Account>> act = new Map<String, List<Account>>();
    global map<id, account> accmap = new map<id, account>();

    global findDuplicateAccountBatchClass(){
        System.debug('In findDuplicateAccountBatchClass');
        for(Account a : [SELECT Id, Name, duplicate__c FROM Account where Id In (Select Account_Name__c FROM Opportunity__c)]){
            unqName.add(a.Name);
        }
        for(Account a : [SELECT Id,Name,duplicate__c FROM Account where Id In (Select AccountId FROM Contact)]){
            unqName.add(a.Name);
        }
        List<AggregateResult> acc = [SELECT Name accname,  Prospect_Code__c  , COUNT(Id) ids FROM Account where Id Not
                                   In (Select Account_Name__c FROM Opportunity__c) And Id Not In (Select AccountId FROM Contact)                           
                                   GROUP BY Name, Prospect_Code__c  HAVING count(Id)>1];
    }
    global Database.QueryLocator start(Database.BatchableContext bc) {
        String query = 'SELECT Id,Name,duplicate__c FROM Account where Id Not In (Select AccountId from Contact) And Id Not In (Select Account_Name__c FROM Opportunity__c)';    
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<sObject> scope){
        System.debug('In execute');
        for(sObject ss : scope)
        {
            Account acc = (Account)ss;
            System.debug('Account '+ acc.Id +' '+ acc.Name);
            if(!act.containsKey(acc.Name))
            {
                act.put(acc.Name, new List<Account>{acc});
            }
            else 
            {
                act.get(acc.Name).add(acc);
                // System.debug(act.get(acc.Name));
            }
        }
        System.debug('Name :' + unqName);
        for(String tmp : act.keySet()){
            for(String s : unqName){
                if(s!=tmp){
                    for(Account a : act.get(tmp)){
                        if(act.get(tmp).indexOf(a) != 0)
                        {
                            a.duplicate__c = True;
                            actLst.add(a);
                        }
                    }
                }
                else{
                    for(Account a : act.get(tmp)){
                        a.duplicate__c = True;
                        actLst.add(a);
                    }
                }
            }
        }    
        System.debug(' actLst size ' + actLst.size());
        accmap.putall(actLst);
        System.debug(' accmap size ' + accmap.size());
        if(accmap.size()>0){
            update accmap.values();
        }
        /*if(!actLst.isEmpty()){
            System.debug('accList size ' + actLst.size());
            update actLst;
            System.debug('accList size ' + actLst.size());
            System.debug('accList ' + actLst);
        }*/
    }
    global void finish(Database.BatchableContext BC){
    }
}

Run below code in console

findDuplicateAccountBatchClass obj = new findDuplicateAccountBatchClass();
Database.executeBatch(obj, 10);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.