1

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?

2

You've only got one query here, so that gives us a pretty good bead on where the issue lies:

        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()]);

List<Legal__c>.add() takes a single argument of type Legal__c. As a result, Salesforce is treating this query the same way it would if it were written out as a single sObject assignment:

Legal__c l = [SELECT .... FROM Legal__c ...];

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 an addAll() method, which accepts a List of objects to add. Here, the type of that parameter would be List<Legal__c>, which is what a SOQL query against Legal__c would normally return.

Changing add() to addAll() will allow the query to complete in that "normal" List context, which doesn't throw QueryException when 0 or >1 result is returned. It's legal to call addAll() with an empty list, too.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.