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
sumit dsumit d 

null pointer exeption when inserting records in batch

Hi All,
     i have requirement in which i have a juction object called family__c. i have two lookup Sister_Company__c and Sister_Company2__c. i am creating a batch in which i want to create records of Accounts having duplicate names.and both duplicates will go in Sister_Company__c and Sister_Company2__c lookup fields.
     example like Account 'ABC' with record type 'A'. and have 2 more account with same name as 'ABC' having record type 'B',and record type 'C'. than junction object records should created and the fill the fields like:-    Sister_Company__c = 'ABC(A)' and  Sister_Company2__c = 'ABC(B)'. and  2nd record as:-Sister_Company__c = 'ABC(A)' and  Sister_Company2__c = 'ABC(c)' creating two junction records like this for ABC(A).
 i created a batch for it but its giving me null pointer exeption.
 how to solve it?
 my batch is given below:-
 //Batch to create FBGFamily junction records for Account having same name
public class BatchFBGFamilies implements Database.Batchable<sObject> {
    //run method to execute batch on one record
    public static void run( Set<Id> AccIds ) {
        List<Account> accountRecords =  [Select id, Name  
                                         From Account
                                         Where Id IN:  AccIds ];
        executeHelper( accountRecords );                  
    }
    //Start method to get All Account which is exetuing batch
    public Database.QueryLocator start(Database.BatchableContext BC){
        String query = 'Select Id, Name ' +
            'FROM Account';
        return Database.getQueryLocator(query);
    }
    
    public void execute(Database.BatchableContext BC, List<Account> accounts){
        executeHelper( accounts );
    }
    
    public void finish(Database.BatchableContext BC){
    }
    
    //method to perform record creation of junction object for Account having same name
    public static void executeHelper( List<Account> accounts ) {
        //set to get Account having duplicate name
        Set<String> duplicateNames = new Set<String>();
        for (Account account : accounts)
        {
            duplicateNames.add(account.Name);
        }
        
        //map to get Account name and list of AccountIds having same name
        Map<String, List<Account>> mapDuplicateAccNameToIds = new Map<String, List<Account>>();
        for(Account acc : [SELECT id,name FROM Account  where Name IN: duplicateNames AND Id NOT IN : accounts]) {
            if(mapDuplicateAccNameToIds.containsKey(acc.Name)) {
                List<Account> ListDuplicates = mapDuplicateAccNameToIds.get(acc.Name);
                ListDuplicates.add(acc);
                mapDuplicateAccNameToIds.put(acc.Name, ListDuplicates);
            } else {
                mapDuplicateAccNameToIds.put(acc.Name, new List<Account> { acc });
            }
        }
        Map<String,FBG_Family__c> mapAlreadyexistFamily = new Map<String,FBG_Family__c>();
        for(FBG_Family__c existFamily : [select id, Name, Sister_Company__c, Sister_Company2__c FROM FBG_Family__c ]){
            String key = String.valueOf(existFamily.Sister_Company__c) + String.valueOf(existFamily.Sister_Company2__c);
            mapAlreadyexistFamily.put(key,existFamily);
        }
        //System.debug('Already exit'+mapAlreadyexistFamily);
        
        //creating junction object records
        List<FBG_Family__c> ListFBGFamily = new List<FBG_Family__c>();
        for (Account acc : accounts)
        {
            List<Account> DuplicateAccount = mapDuplicateAccNameToIds.get(acc.Name);
            System.debug('DuplicateAccount'+DuplicateAccount);
            If(DuplicateAccount.Size()>0) {
                for(Account Acc1 : DuplicateAccount ){

                    FBG_Family__c Family =  new FBG_Family__c();
                    Family.Sister_Company__c =  acc.id;
                    if( acc.Id != Acc1.Id){
                        Family.Sister_Company2__c = Acc1.id;
                        String key = String.valueOf(Family.Sister_Company__c) + String.valueOf(Family.Sister_Company2__c);
                        if(!mapAlreadyexistFamily.containsKey(key)){
                            ListFBGFamily.add(Family); 
                        }
                     } 
                }
            }
        }
        if( ListFBGFamily.Size() > 0 ){
            FBGFamilyTriggerHelper.runTrigger = FALSE;
            Insert ListFBGFamily;
        }
    }
}
 what i am missing? Any suggestions?
 
Best Answer chosen by sumit d
Ajay K DubediAjay K Dubedi
Hi Sumit,

Try the following code it may be helpful for you:
  
if(accounts.size() > 0) {
    for (Account acc : accounts)
    {
        if(mapDuplicateAccNameToIds.containsKey(acc.Name)) {
            List<Account> DuplicateAccount = mapDuplicateAccNameToIds.get(acc.Name);
        }
        
        System.debug('DuplicateAccount------ ' + DuplicateAccount);
        if(DuplicateAccount.size() > 0) {
            for(Account Acc1 : DuplicateAccount ) {
                FBG_Family__c Family =  new FBG_Family__c();
                Family.Sister_Company__c =  acc.id;
                if( acc.Id != Acc1.Id){
                    Family.Sister_Company2__c = Acc1.id;
                    String key = String.valueOf(Family.Sister_Company__c) + String.valueOf(Family.Sister_Company2__c);
                    if(!mapAlreadyexistFamily.containsKey(key)){
                        ListFBGFamily.add(Family); 
                    }
                } 
            }
        }
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi