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
montezmontez 

List has more than 1 row for assignment to SObject - Error on Process Builder

I have this class I am using to assign leads via process builder. When the class tries to run in bulk, I get the following error:
Error Occurred: An Apex error occurred: System.QueryException: List has more than 1 row for assignment to SObject​

Any thoughts on how to resolve this error?
Here is the class I an using to assign leads:
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;
   }
}

 
Best Answer chosen by montez
Temoc MunozTemoc Munoz
Hi montez.

Please try the following code:
 
public class AssignLeadsUsingAssignmentRules
{
    @InvocableMethod
    public static void LeadAssign(List<Id> LeadIds)
    {
            Database.DMLOptions dmo = new Database.DMLOptions();
            dmo.assignmentRuleHeader.useDefaultRule= true;          
            List<Lead> Leads = [select id from lead where lead.id in :LeadIds];
            
            for(Lead ld : Leads)
            {
                 ld.setOptions(dmo);
            }
            update Leads;
   }
}


 

All Answers

Temoc MunozTemoc Munoz
Hi montez.

Please try the following code:
 
public class AssignLeadsUsingAssignmentRules
{
    @InvocableMethod
    public static void LeadAssign(List<Id> LeadIds)
    {
            Database.DMLOptions dmo = new Database.DMLOptions();
            dmo.assignmentRuleHeader.useDefaultRule= true;          
            List<Lead> Leads = [select id from lead where lead.id in :LeadIds];
            
            for(Lead ld : Leads)
            {
                 ld.setOptions(dmo);
            }
            update Leads;
   }
}


 
This was selected as the best answer
Naval Sharma4Naval Sharma4
Hi Montez.

Updated your code : 

 
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 LIMIT 1];
            Leads.setOptions(dmo);
            update Leads;
   }
}

I hope this will work for you.

Regards,
Naval
Mahesh DMahesh D
Hi Montez,

Please find the below code:

Here I considered:

(1) Naming Convention
(2) Always its better to send Set of Ids as it will not maintain duplicate ids and it duplicates are not required here.
(3) Corrected the logic to not to get exception.
public class AssignLeadsUsingAssignmentRules
{
    @InvocableMethod
    public static void leadAssign(Set<Id> leadIdSet)
    {
            Database.DMLOptions dmo = new Database.DMLOptions();
            dmo.assignmentRuleHeader.useDefaultRule= true;          
            List<Lead> leadList = [select Id from lead where lead.Id IN :leadIdSet];
            
            for(Lead ld : leadList)
            {
                 ld.setOptions(dmo);
            }
            update leadList;
   }
}

Please do let me know if it helps you.

Regards,
Mahesh
montezmontez
Thank you Temoc and Naval. I am running my class as Temoc posted and it appers to work as expected.
Temoc MunozTemoc Munoz
Awesome. I'm glad it's working Montez.