In my Apex trigger I got an error:
Error:Apex trigger Remittance caused an unexpected exception, contact your administrator: Remittance: execution of AfterUpdate caused by: System.QueryException: List has more than 1 row for assignment to SObject: External entry point
and here is my code:
public class OutstandingFieldUpdateOnLegal {
public List<Legal__c> legalFieldUpdate(List<Remittance__c>newRemit){
Map<Id,String> remAccnt=new Map<Id,String>();
for(Remittance__c remit : newRemit){
if(remit.Status__c =='Manual Payment' && remit.Payment_Type__c =='Legal Settlement Payment')
remAccnt.put(remit.Id,remit.Customer__c);
}
List<Legal__c> legallist = new List<Legal__c>();
if(remAccnt.size() >0)
legallist.add([SELECT id,Outstanding_Final_Judgment_Amount__c,
Outstanding_Final_Settlement_Amount__c,
Final_Judgment_Date__c,Final_Settlement_Date__c,
Customer__c From Legal__c
WHERE Customer__c IN : remAccnt.Values()]);
return legallist;
}
public void legalFieldUpdating(List<Remittance__c>newRemit){
List<Legal__c> lgl =new List<Legal__c>();
List<Legal__c> legaldata = legalFieldUpdate(newRemit);
for(Remittance__c remit : newRemit){
if(legaldata.size() > 0){
for(Legal__c legals : legaldata){
if(legals.Final_Judgment_Date__c!=Null && remit.Date__c!=Null &&
legals.Final_Judgment_Date__c > remit.Date__c){
legals.Outstanding_Final_Judgment_Amount__c =
legals.Outstanding_Final_Judgment_Amount__c - remit.Payment_Amount__c;
lgl.add(legals);
}
else if(legals.Final_Settlement_Date__c !=Null && remit.Date__c !=Null &&
legals.Final_Settlement_Date__c > remit.Date__c){
legals.Outstanding_Final_Settlement_Amount__c =
legals.Outstanding_Final_Settlement_Amount__c - remit.Payment_Amount__c;
lgl.add(legals);
}
else if(legals.Final_Judgment_Date__c !=Null && remit.Date__c !=Null &&
legals.Final_Judgment_Date__c < remit.Date__c){
legals.Outstanding_Final_Judgment_Amount__c =
legals.Outstanding_Final_Judgment_Amount__c - remit.Payment_Amount__c;
legals.Outstanding_Final_Settlement_Amount__c =
legals.Outstanding_Final_Settlement_Amount__c - remit.Payment_Amount__c;
lgl.add(legals);
}
}
}
}
update lgl;
}
}
Can someone help me why is it so?
Best Answer
You've only got one query here, so that gives us a pretty good bead on where the issue lies:
List<Legal__c>.add()
takes a single argument of typeLegal__c
. As a result, Salesforce is treating this query the same way it would if it were written out as a single sObject assignment:so as to provide the right argument type to this method.
When the query returns 0 or more than 1 object in a single-sObject assignment, a
QueryException
results.Fortunately,
List
also provides anaddAll()
method, which accepts aList
of objects to add. Here, the type of that parameter would beList<Legal__c>
, which is what a SOQL query againstLegal__c
would normally return.Changing
add()
toaddAll()
will allow the query to complete in that "normal" List context, which doesn't throwQueryException
when 0 or >1 result is returned. It's legal to calladdAll()
with an empty list, too.