You need to sign in to do that
Don't have an account?
NatureGod
Hi All,
Hi all,
I have the following trigger:
trigger UpdateLocationStatusonLocationUsingMap on Account (before insert) {
for( Account parent: Trigger.new)
{
List<Location__c > children = [ SELECT Id, Associate__c , Status__c from Location__c where Associate__c = :parent.Id];
List<Location__c > childrenToUpdate = new List<Location__c >();
for(Location__c thischild: children )
{
if( thischild.Status__c != parent.Status__c )
{
if ( parent.Status__c == 'Under Deactivation' || parent.Status__c == 'Inactive')
{
thischild.Status__c = parent.Status__c ;
childrenToUpdate.add(thischild);
}
}
}
update childrenToUpdate;
}
}
Which works fine and has passed 100 and works in production.
But when it comes running batch using the apex data loader and the .xml, it reaches the governing limit of 100 giving the message:
«Definitely you are using one or more soql query in a loop and hence the number of query executed in one run time is crossing the governing limit of 100 query.......The alternative is to get the values in map beforehand and use map values instead of query in loop.......»
Any suggestions on how to skip the for and introduce a map?
Best Regards,
Stefanos T.
I have the following trigger:
trigger UpdateLocationStatusonLocationUsingMap on Account (before insert) {
for( Account parent: Trigger.new)
{
List<Location__c > children = [ SELECT Id, Associate__c , Status__c from Location__c where Associate__c = :parent.Id];
List<Location__c > childrenToUpdate = new List<Location__c >();
for(Location__c thischild: children )
{
if( thischild.Status__c != parent.Status__c )
{
if ( parent.Status__c == 'Under Deactivation' || parent.Status__c == 'Inactive')
{
thischild.Status__c = parent.Status__c ;
childrenToUpdate.add(thischild);
}
}
}
update childrenToUpdate;
}
}
Which works fine and has passed 100 and works in production.
But when it comes running batch using the apex data loader and the .xml, it reaches the governing limit of 100 giving the message:
«Definitely you are using one or more soql query in a loop and hence the number of query executed in one run time is crossing the governing limit of 100 query.......The alternative is to get the values in map beforehand and use map values instead of query in loop.......»
Any suggestions on how to skip the for and introduce a map?
Best Regards,
Stefanos T.
Simply move the query outside the for loop. Get the account ids in a Map in the for loop:
Example:
Map<Id, Account> accountsMap = new Map<Id, Account>();
for(Account a :Trigger.new){
accountsMap.put(a.Id, a);
}
List<Location__c> children = [SELECT Id, Associate__c , Status__c from Location__c where Associate__c In :accountsMap.keySet()];
List<Location__c> childrenToUpdate = new List<Location__c>();
for(Location__c thischild :children){
//Compare with the corresponding account
if( thischild.Status__c != accountsMap.get(thisChild.Associate__c).Status__c )
{
if ( accountsMap.get(thisChild.Associate__c).Status__c == 'Under Deactivation' || accountsMap.get(thisChild.Associate__c).Status__c == 'Inactive')
{
thischild.Status__c = accountsMap.get(thisChild.Associate__c).Status__c ;
childrenToUpdate.add(thischild);
}
}
}
update toUpdate;