function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JN22JN22 

Dataloader Error When Trigger Fires

Hello,

 

I have a trigger that fires on a custom object to update the associated Account record.  The trigger works fine when I make changes manaully, however, when I try to make a bulk change to records via the Dataloader, I get the following error message:

 

Scorecard2Account: execution of BeforeUpdate caused by: System.QueryException: List has more than 1 row for assignment to Sobject Trigger.Scorecard2Account: line 14, column 1

 

 

I know it's probably something pretty simple, but I can't figure it out.  Can anyone help?  My trigger code is below.  Thanks,

 

 

trigger Scorecard2Account on Client_Status_Scorecard__c (before insert, before update, before delete)
{
//Update Account with Scorecard name in lookup field

list<Account> L1 = new list<Account>();
set<Id> Ids = new Set <Id>();

if(trigger.isUpdate)
{
FOR(Client_Status_Scorecard__c css :trigger.new) {
Ids.add(css.Client_Name__c);
}

Account acct1 = [SELECT Id
FROM Account
WHERE Id IN :Ids];

for(Client_Status_Scorecard__c css1 : trigger.new)
{
acct1.Client_Status_Scorecard__c = css1.Id;
L1.add(acct1);
}
}
IF(trigger.isDelete)
{
FOR(Client_Status_Scorecard__c css :trigger.old) {
Ids.add(css.Client_Name__c);
}

Account acct1 = [SELECT Id
FROM Account
WHERE Id IN :Ids];

for(Client_Status_Scorecard__c css1 : trigger.old)
{
acct1.Client_Status_Scorecard__c = null;
L1.add(acct1);
}
}
IF(L1.size()>0)
Update L1;
}

Best Answer chosen by Admin (Salesforce Developers) 
SFAdmin5SFAdmin5

try this revised trigger and see if you get that error

 

trigger scorecardtoAccountREVISED on Client_Status_Scorecard__c (after insert, after update) 
{          
     Map<Id,Account> accMap = new Map<Id,Account>();
     Set<id> Ids = new Set<id>();
     for (Client_Status_Scorecard__c css : Trigger.new) 
     {
         Ids.add(css.Client_Name__c);
     }
     
     Map<id,Account> accMap2 = new Map<id,Account>([Select Id,Name,Client_Status_Scorecard__c from Account Where Id in :Ids]);  
     
     for (Client_Status_Scorecard__c  css : Trigger.new) 
     {
                 Account a = accMap2.get(css.Client_Name__c);
                 a.Client_Status_Scorecard__c = css.Id;

                 accMap.put(a.id,a);
     }
                 update accMap.values();
     
}

 

All Answers

SFAdmin5SFAdmin5

what's the relationship of the custom object to the account object?  m-d/lookup?  is the custom object a child of the account?

JN22JN22

The custom object has a Master-Detail relationship with the Account and is the child.

SFAdmin5SFAdmin5

try this revised trigger and see if you get that error

 

trigger scorecardtoAccountREVISED on Client_Status_Scorecard__c (after insert, after update) 
{          
     Map<Id,Account> accMap = new Map<Id,Account>();
     Set<id> Ids = new Set<id>();
     for (Client_Status_Scorecard__c css : Trigger.new) 
     {
         Ids.add(css.Client_Name__c);
     }
     
     Map<id,Account> accMap2 = new Map<id,Account>([Select Id,Name,Client_Status_Scorecard__c from Account Where Id in :Ids]);  
     
     for (Client_Status_Scorecard__c  css : Trigger.new) 
     {
                 Account a = accMap2.get(css.Client_Name__c);
                 a.Client_Status_Scorecard__c = css.Id;

                 accMap.put(a.id,a);
     }
                 update accMap.values();
     
}

 

This was selected as the best answer
JN22JN22

Hi Ross, that worked great!!  Thanks for your help.  My final code is below:

 

trigger Scorecard2Account on Client_Status_Scorecard__c (after insert, after update, after delete)
{
Map<Id,Account> oppMap = new Map<Id,Account>();
Set<id> Ids = new Set<id>();

if(trigger.isUpdate)
{

for (Client_Status_Scorecard__c prgm : Trigger.new)
{
Ids.add(prgm.Client_Name__c);
}

Map<id,Account> oppMap2 = new Map<id,Account>([Select Id,Name,Client_Status_Scorecard__c from Account Where Id in :Ids]);

for (Client_Status_Scorecard__c prgm : Trigger.new)
{
Account a = oppMap2.get(prgm.Client_Name__c);
a.Client_Status_Scorecard__c = prgm.Id;

oppMap.put(a.id,a);
}
update oppMap.values();
}

if(trigger.isDelete)
{

for (Client_Status_Scorecard__c prgm : Trigger.old)
{
Ids.add(prgm.Client_Name__c);
}

Map<id,Account> oppMap2 = new Map<id,Account>([Select Id,Name,Client_Status_Scorecard__c from Account Where Id in :Ids]);

for (Client_Status_Scorecard__c prgm : Trigger.old)
{
Account a = oppMap2.get(prgm.Client_Name__c);
a.Client_Status_Scorecard__c = null;

oppMap.put(a.id,a);
}
update oppMap.values();
}
}

SFAdmin5SFAdmin5

cool.  just fyi though, part of the reason i revised your trigger was b/c you've got "after insert" in the trigger definition, but your trigger is only set up to fire on update and delete, which is fine, but if you don't want it firing on after insert just take that out of the trigger definition (just copy my trigger events into yours).  it might confuse developers who look at your trigger and want to know what you are trying to do, even though leaving it in won't cause any problems.

JN22JN22

Hi Ross,

 

Good catch!  However, I do want it to fire on insert so I was actually missing that piece in my code.  Below is the updated code.  Thanks again!

 

trigger Scorecard2Account on Client_Status_Scorecard__c (after insert, after update, after delete)
{
Map<Id,Account> oppMap = new Map<Id,Account>();
Set<id> Ids = new Set<id>();

if(trigger.isUpdate || trigger.isInsert)
{

for (Client_Status_Scorecard__c prgm : Trigger.new)
{
Ids.add(prgm.Client_Name__c);
}

Map<id,Account> oppMap2 = new Map<id,Account>([Select Id,Name,Client_Status_Scorecard__c from Account Where Id in :Ids]);

for (Client_Status_Scorecard__c prgm : Trigger.new)
{
Account a = oppMap2.get(prgm.Client_Name__c);
a.Client_Status_Scorecard__c = prgm.Id;

oppMap.put(a.id,a);
}
update oppMap.values();
}

if(trigger.isDelete)
{

for (Client_Status_Scorecard__c prgm : Trigger.old)
{
Ids.add(prgm.Client_Name__c);
}

Map<id,Account> oppMap2 = new Map<id,Account>([Select Id,Name,Client_Status_Scorecard__c from Account Where Id in :Ids]);

for (Client_Status_Scorecard__c prgm : Trigger.old)
{
Account a = oppMap2.get(prgm.Client_Name__c);
a.Client_Status_Scorecard__c = null;

oppMap.put(a.id,a);
}
update oppMap.values();
}
}