You need to sign in to do that
Don't have an account?

Trigger with errors
Hi all,
If anyone could offer some advice I'd really appreciate it. My trigger is getting the following error:
Apex script unhandled trigger exception by user/organization: 00540000000rfc7/00D300000000WHR
UpdateAssetNames: execution of BeforeUpdate
Caused by: System.Exception: Too many SOQL queries: 21
Trigger.UpdateAssetNames: line 20, column 20
The trigger updates associated Assets when an Account Name is updated. I thought I'd set it up to process the records in bulk, but evidently I'm doing something wrong:
trigger UpdateAssetNames on Account (before update) {
// Assemble Accounts with new Names
Map<Id, Account> acctsWithNewNames = new Map<Id, Account>();
// Trigger.new - list of the Accounts
// Loop iterates over the list, and adds any that have new
// names to the acctsWithNewNames map.
for (Integer i = 0; i < Trigger.new.size(); i++) { if ( (Trigger.old[i].Name != Trigger.new[i].Name)) {
acctsWithNewNames.put(Trigger.old[i].id, Trigger.new[i]);
}
}
List<Asset> updatedAssets = new List<Asset>();
for (Asset a : [SELECT id, accountId, Name FROM asset WHERE accountId in :acctsWithNewNames.keySet()]) {
Account parentAccount = acctsWithNewNames.get(a.accountId);a.Name = parentAccount.Name;
updatedAssets.add(a);
}
update updatedAssets;}
Many thanks in advance!
Deb
Please try this
for (Asset[] a : [SELECT id, accountId, Name FROM asset WHERE accountId in :acctsWithNewNames.keySet()]) {
for(Asset ast : a)
{
Account parentAccount = acctsWithNewNames.get(ast.accountId);
ast.Name = parentAccount.Name;
updatedAssets.add(ast);
}
}
update updatedAssets;
All Answers
Please try this
for (Asset[] a : [SELECT id, accountId, Name FROM asset WHERE accountId in :acctsWithNewNames.keySet()]) {
for(Asset ast : a)
{
Account parentAccount = acctsWithNewNames.get(ast.accountId);
ast.Name = parentAccount.Name;
updatedAssets.add(ast);
}
}
update updatedAssets;
That did it -- thank you very much for your help!
I'll use this syntax moving forward.