You need to sign in to do that
Don't have an account?
lp oklll
Trigger has hit a max query size:50001
This is my trigger but I am getting an error Trigger has hit a max query size:50001
The following trigger is trying to populate a lookup field APA on child account. This value is based on the value in another field call APA_Name__c which is populated as a text field on child account. I have 3 accounts of APA . . Please can someone help on this.
Trigger PopulateAPA on Account (before insert,before update) {
// Get a list of all active record types within the system associated to Accounts
List<Account> APAList = [Select Name, Id From Account];
// Create a map between the Account record Type Name and Id for for easy retrieval
Map<String,String> NewAPA = new Map<String,String>{};
for(Account APAAcc: APAList)
NewAPA.put(APAAcc.Name,APAAcc.Id);
for (Account NewAccount : Trigger.new)
{
If(NewAccount.APA_Name__c == 'Global Heat Source')
{
NewAccount.APA__c = NewAPA.get('Global Heat Source');
}
else If(NewAccount.APA_Name__c == 'Forrest')
{
NewAccount.APA__c = NewAPA.get('Forrest');
}
else If(NewAccount.APA_Name__c == 'Keepmoat')
{
NewAccount.APA__c = NewAPA.get('Keepmoat');
}
}
}
The following trigger is trying to populate a lookup field APA on child account. This value is based on the value in another field call APA_Name__c which is populated as a text field on child account. I have 3 accounts of APA . . Please can someone help on this.
Trigger PopulateAPA on Account (before insert,before update) {
// Get a list of all active record types within the system associated to Accounts
List<Account> APAList = [Select Name, Id From Account];
// Create a map between the Account record Type Name and Id for for easy retrieval
Map<String,String> NewAPA = new Map<String,String>{};
for(Account APAAcc: APAList)
NewAPA.put(APAAcc.Name,APAAcc.Id);
for (Account NewAccount : Trigger.new)
{
If(NewAccount.APA_Name__c == 'Global Heat Source')
{
NewAccount.APA__c = NewAPA.get('Global Heat Source');
}
else If(NewAccount.APA_Name__c == 'Forrest')
{
NewAccount.APA__c = NewAPA.get('Forrest');
}
else If(NewAccount.APA_Name__c == 'Keepmoat')
{
NewAccount.APA__c = NewAPA.get('Keepmoat');
}
}
}
The following code will work for you
All Answers
You should get rid of this query
What are APA list? Do you have a specific criteria to get APA list?
like all account with Name 'APA'?
The following code will work for you
The code looks good and it works fine too. the only problem is that earlier i was getting 100% coverage but now i am getting 91%
Please share the test class code,
and choose the trigger answer as best answer
@isTest
public class PopReselleroNGSMandCASE {
static testMethod void insertNewGSMHandshake() {
// Get a list of all active record types within the system associated to Accounts
List<RecordType> accRTypes = [Select Name, Id From RecordType where sObjectType='Account'];
// Create a map between the Account record Type Name and Id for for easy retrieval
Map<String,String> accountRecordTypes = new Map<String,String>{};
for(RecordType accRT: accRTypes)
accountRecordTypes.put(accRT.Name,accRT.Id);
Account testReseller = new Account();
testReseller.Name='Test of the Account1';
testReseller.type='Reseller';
testReseller.RecordTypeId= accountRecordTypes.get('Reseller');
testReseller.Shippingcountry='United Kingdom';
insert testReseller;
Account acc=[select id,name from account where id=:testReseller.id];
Account testAPA = new Account();
testAPA.Name='Test of the Account1';
testApa.APA_Name__c='Forrest';
testAPA.type='APA';
testAPA.RecordTypeId= accountRecordTypes.get('APA');
testAPA.Reseller__c = testReseller.id ;
testAPA.Shippingcountry='United Kingdom';
insert testAPA;
Account testAPA1 = new Account();
testAPA1.Name='Test of the Account1';
testAPA1.APA_Name__c='Global Heat Source';
testAPA1.type='APA';
testAPA1.RecordTypeId= accountRecordTypes.get('APA');
testAPA1.Reseller__c = testReseller.id ;
testAPA1.Shippingcountry='United Kingdom';
insert testAPA1;
Account testAPA2 = new Account();
testAPA2.Name='Test of the Account1';
testAPA2.APA_Name__c='Keepmoat';
testAPA2.type='APA';
testAPA2.RecordTypeId= accountRecordTypes.get('APA');
testAPA2.Reseller__c = testReseller.id ;
testAPA2.Shippingcountry='United Kingdom';
insert testAPA2;
Account testHome = new Account();
testHome.Name='Test of the Account1';
testHome.type='Home';
testHome.RecordTypeId= accountRecordTypes.get('Home');
testHome.Shippingcountry='United Kingdom';
testHome.Reseller__c = testReseller.id ;
testHome.APA__c= testAPA.id;
insert testHome;
Account testHome1 = new Account();
testHome1.Name='Test of the Account1';
testHome1.type='Home';
testHome1.RecordTypeId= accountRecordTypes.get('Home');
testHome1.Shippingcountry='United Kingdom';
testHome1.Reseller__c = testReseller.id ;
testHome1.APA__c= testAPA1.id;
insert testHome1;
Account testHome2 = new Account();
testHome2.Name='Test of the Account1';
testHome2.type='Home';
testHome2.RecordTypeId= accountRecordTypes.get('Home');
testHome2.Shippingcountry='United Kingdom';
testHome2.Reseller__c = testReseller.id ;
testHome2.APA__c= testAPA2.id;
insert testHome2;
GSM_Handshake__c testGSM_Handshake = new GSM_Handshake__c();
testGSM_Handshake.Contact_Name__c='Test of the GSM_Handshake';
testGSM_Handshake.Home_Account__c = testHome.id ;
insert testGSM_Handshake;
Case TestCase = new Case();
TestCase.accountid = testHome.id ;
TestCase.Reseller__c = testReseller.id;
TestCase.APA__c = testAPA.id ;
TestCase.status = 'New';
insert TestCase;
}
}
Try the following test class
System.DmlException: Insert failed. First exception on row 0 with id 001L000000cibI3IAI; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
I thought of it,
Can you quickly try this?