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
Karthik Mohan 6Karthik Mohan 6 

An Apex error occurred: System.QueryException: List has more than 1 row for assignment to SObject

I wrote a apex code which is triggerd when a particular filed in updated above a certain value. When i manually edit that field the code works fine. But the code had to be excute whenever the value is updated, it shouldnt wait until manual edit. The apex helps in assigning a account to particular queue.
the apex code is as follows : 
public class AssignLeadsUsingAssignmentRules
{
    @InvocableMethod
    public static void LeadAssign(List<Id> LeadIds)
    {
            Database.DMLOptions dmo = new Database.DMLOptions();
            dmo.assignmentRuleHeader.useDefaultRule= true;          
            Lead Leads=[select id from lead where lead.id in :LeadIds];
            Leads.setOptions(dmo);
            update Leads;
   }
}


Error message :  Error element myWaitEvent_myWait_myRule_1_event_0_SA1 (FlowActionCall).
An Apex error occurred: System.QueryException: List has more than 1 row for assignment to SObject


Can someone help me with this?. Thanks in advance.
GauravendraGauravendra
Hi karthik,

I think the query you have written is returning more than one leads.
select id from lead where lead.id in :LeadIds
Make it List<Lead>

Hope this helps.
 
Karthik Mohan 6Karthik Mohan 6
Hi Gauravendra..
can you explain bit more clearly, I'm a newbie in this coding
Karthik Mohan 6Karthik Mohan 6

Hi Gauravendra,
I tried using List<Lead>. but i tdint work since the next line Leads.setOptions(dmo); thorws an error. Because the retun type on setOptions() method is void by default.

 

GauravendraGauravendra

Hi Karthik,

Since you are getting a list of lead record. You may call setOptions() for each lead record individually.
List<Lead> Leads=[select id from lead where lead.id in :LeadIds];
 for(Lead ld : Leads ) { 
ld.setOptions(dmo);
 }


Please try this, and let me know if its work.
Karthik Mohan 6Karthik Mohan 6
I tried , but i didnt work, Im getting : Method does not exist or incorrect signature: void setOptions(Database.DMLOptions) from the type Id
Karthik Mohan 6Karthik Mohan 6
Hi Gauravendra, 
It worked. Thnaks for the help