You need to sign in to do that
Don't have an account?
sumit d
null pointer exeption while creating records
Hi All,
i have created the batch in which i am creating Custom object records for duplicate accounts.
when i run the batch i have null pointer exeption at bold lines.
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);
for(Account Acc1 : DuplicateAccount ){ //here its giving null pointer exeption
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;
}
}
}
how to solve this any suggestions?
i have created the batch in which i am creating Custom object records for duplicate accounts.
when i run the batch i have null pointer exeption at bold lines.
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);
for(Account Acc1 : DuplicateAccount ){ //here its giving null pointer exeption
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;
}
}
}
how to solve this any suggestions?
Check the Size of the List before going into the For Loop.
This should work. Mark this as your best answer to help others.
Regards,
Avishek
i tried this its still giving me null pointer exeption.
i think in these lines given below:-
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 });
}
}
here in that query its giving me null.
Can u tell me what i am missing ?
Any suggestions?
my requirement is given below:- 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).
Any suggestions?
Change your code form
for (Account acc : accounts) {
}
by this:
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