You need to sign in to do that
Don't have an account?
Madhuri Kanthekar
CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY:AccountTrigger: System.LimitException: Too many SOQL queries: 101
Throwing error at line 12, when inserting (data loader) more than 100 records and Trigger works fine for less than 100 records
Please help, below is my trigger -
trigger AccountTrigger on Account (before insert,before update){
Set<String> groupIdSet = new Set<String>();
for(Account acc : Trigger.New){
groupIdSet.add(acc.Pro_Alpha_ID__c);
system.debug('--Trigger size in for loop1-->'+Trigger.new.size());
system.debug(groupIdSet);
}
Map<String,Account> parentAccMap = new Map<String,Account>();
List<Account> accList = new List<Account>();
accList = [SELECT id,Pro_Alpha_ID__c FROM Account WHERE Pro_Alpha_ID__c IN: groupIdSet AND RecordtypeId =: Schema.SObjectType.Account.getRecordTypeInfosByName().get('EHG Group').getRecordTypeId()];
for(Account acc : accList){
parentAccMap.put(acc.Pro_Alpha_ID__c,acc);
system.debug(parentAccMap);
}
Map<String,List<Account>> accMap = new Map<String,List<Account>>();
if(Trigger.isBefore){
system.debug('--Trigger size in for loop3-->'+Trigger.new.size());
for(Account acc : Trigger.New){
if(acc.Pro_Alpha_ID__c != null && acc.ParentId == null && acc.RecordtypeId != Schema.SObjectType.Account.getRecordTypeInfosByName().get('EHG Group').getRecordTypeId()){
if(parentAccMap.keySet().contains(acc.Pro_Alpha_ID__c)){
acc.ParentId = parentAccMap.get(acc.Pro_Alpha_ID__c).Id;
} else {
if(accMap.keySet().contains(acc.Pro_Alpha_ID__c)){
accMap.get(acc.Pro_Alpha_ID__c).add(acc);
system.debug(accMap);
} else {
List<Account> accLst = new List<Account>();
accLst.add(acc);
system.debug(accLst);
accMap.put(acc.Pro_Alpha_ID__c,accLst);
system.debug(accMap);
}
}
}
}
List<Account> accountsToCreate = new List<Account>();
for(String s : accMap.keySet()){
Account accnt = new Account(RecordtypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('EHG Group').getRecordTypeId(),
Pro_Alpha_ID__c = s,
Name = accMap.get(s).get(0).Name,
BillingStreet = accMap.get(s).get(0).BillingStreet,
BillingCity = accMap.get(s).get(0).BillingCity,
BillingState = accMap.get(s).get(0).BillingState,
BillingCountry = accMap.get(s).get(0).BillingCountry,
BillingPostalCode = accMap.get(s).get(0).BillingPostalCode
);
insert accnt;
system.debug(accnt);
for(Account acc : accMap.get(s)){
system.debug(accMap.get(s));
acc.ParentId = accnt.Id;
}
}
}
}
Please help, below is my trigger -
trigger AccountTrigger on Account (before insert,before update){
Set<String> groupIdSet = new Set<String>();
for(Account acc : Trigger.New){
groupIdSet.add(acc.Pro_Alpha_ID__c);
system.debug('--Trigger size in for loop1-->'+Trigger.new.size());
system.debug(groupIdSet);
}
Map<String,Account> parentAccMap = new Map<String,Account>();
List<Account> accList = new List<Account>();
accList = [SELECT id,Pro_Alpha_ID__c FROM Account WHERE Pro_Alpha_ID__c IN: groupIdSet AND RecordtypeId =: Schema.SObjectType.Account.getRecordTypeInfosByName().get('EHG Group').getRecordTypeId()];
for(Account acc : accList){
parentAccMap.put(acc.Pro_Alpha_ID__c,acc);
system.debug(parentAccMap);
}
Map<String,List<Account>> accMap = new Map<String,List<Account>>();
if(Trigger.isBefore){
system.debug('--Trigger size in for loop3-->'+Trigger.new.size());
for(Account acc : Trigger.New){
if(acc.Pro_Alpha_ID__c != null && acc.ParentId == null && acc.RecordtypeId != Schema.SObjectType.Account.getRecordTypeInfosByName().get('EHG Group').getRecordTypeId()){
if(parentAccMap.keySet().contains(acc.Pro_Alpha_ID__c)){
acc.ParentId = parentAccMap.get(acc.Pro_Alpha_ID__c).Id;
} else {
if(accMap.keySet().contains(acc.Pro_Alpha_ID__c)){
accMap.get(acc.Pro_Alpha_ID__c).add(acc);
system.debug(accMap);
} else {
List<Account> accLst = new List<Account>();
accLst.add(acc);
system.debug(accLst);
accMap.put(acc.Pro_Alpha_ID__c,accLst);
system.debug(accMap);
}
}
}
}
List<Account> accountsToCreate = new List<Account>();
for(String s : accMap.keySet()){
Account accnt = new Account(RecordtypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('EHG Group').getRecordTypeId(),
Pro_Alpha_ID__c = s,
Name = accMap.get(s).get(0).Name,
BillingStreet = accMap.get(s).get(0).BillingStreet,
BillingCity = accMap.get(s).get(0).BillingCity,
BillingState = accMap.get(s).get(0).BillingState,
BillingCountry = accMap.get(s).get(0).BillingCountry,
BillingPostalCode = accMap.get(s).get(0).BillingPostalCode
);
insert accnt;
system.debug(accnt);
for(Account acc : accMap.get(s)){
system.debug(accMap.get(s));
acc.ParentId = accnt.Id;
}
}
}
}
Try like this,
Let us know if it helps.
All Answers
Let us know if it helps.
No its not working, error at same line. Am not getting how to caste this keyset to a list
Illegal assignment from List<List<Account>> to List<Account>
Try like this,
Let us know if it helps.
Use this code snippet and let me know the results :)
Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com