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
Shobana RavichandranShobana Ravichandran 

List has more than 1 row error

global with sharing class DebtCollection implements Schedulable 
{
    /
    global void execute(SchedulableContext sc)
    {
        createDebtCollectionCases();
        updateAccounts();
        updateCasesToDemand();
        //sendReminderEmails(AccountsRequiringReminder());
    }
    private static void createDebtCollectionCases(){
        try{
           insert casesToCreate(AccountsRequiringDebtCase());
        }catch(Exception e){
            
        }
    }
    
   private static void updateAccounts()
    {
        List<Account> accsToUpdate = AccountsRequiringDebtCase();
        for(Account acc : accsToUpdate){
            acc.Has_Open_Debt_Case__c = TRUE;
        }
        try {
            update accsToUpdate;
        }catch(Exception e){
            
        }
    }
      
    private static void updateCasesToDemand()
    {
        try {
            update casesToUpdate(CasesRequiringDemand());
        }catch(Exception e){
            
        }
    }
    
    
    public static List<Account> AccountsRequiringDebtCase()
    {   
        //Accounts with credit term of 30 have their 60 day balance checked on the 2nd of each month
        Integer creditTermDate30 = 2;
        //Accounts with credit term of 35 have their 60 day balance checked on the 7th of each month
        Integer creditTermDate35 = 7;
        //Accounts with credit term of 45 have their 60 day balance checked on the 17th of each month
        Integer creditTermDate45 = 17;
        List<Account> accList = new List<Account>();
        if(Date.today().Day()==creditTermDate30){
            accList.add([
                select Id, Has_Open_Debt_Case__c, Most_Recent_Statement_Date__c 
                from Account where
                Has_Open_Debt_Case__c = FALSE and
                Day_60_Balance__c > 0 and Credit_Terms__c = 30
                ]);  
            system.debug('accList' +accList);
        }
        if(Date.today().Day()==creditTermDate35){
            accList.add([
                select Id, Has_Open_Debt_Case__c, Most_Recent_Statement_Date__c 
                from Account where
                Has_Open_Debt_Case__c = FALSE and
                Day_60_Balance__c > 0 and Credit_Terms__c = 35
            ]);  
        }
        if(Date.today().Day()==creditTermDate45){
            accList.add([
                select Id, Has_Open_Debt_Case__c, Most_Recent_Statement_Date__c 
                from Account where
                Has_Open_Debt_Case__c = FALSE and
                Day_60_Balance__c > 0 and Credit_Terms__c = 45
             ]);  
        }
         accList.add([
            select Id, Has_Open_Debt_Case__c, Most_Recent_Statement_Date__c 
            from Account where
            Has_Open_Debt_Case__c = FALSE and
            credit_hold_date__c != null and (Day_60_Balance__c >0 OR Day_90_Balance__c>0)
                
       ]); 
        
        

     return  accList;
        
    }
    
    
    public static List<Case> CasesRequiringDemand()
    {   
        
        Date demandDay = Date.today().addDays(-30);
        Id DebtRecordType = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Debt Collection').getRecordTypeId();
        
        List<Case> caseList = [
            select Id, Status 
            from Case where
            Status != 'Closed' AND recordTypeId = :DebtRecordType AND createddate  = :demandDay
         ];
        
        return  caseList;
        
    }
    
    
    private static List<Case> casesToCreate(list<Account> accounts)
   {
         List<Case> returnList = new list<Case>();
         Id devRecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Debt Collection').getRecordTypeId();
         List<Group> queueId = [select Id from Group where Name = 'Finance Queue' and Type = 'Queue'  ];
        
        
        
         if(!accounts.isEmpty())
         {
             for(Account acc: accounts)
             {
                 Case newCase = new Case();
                 newCase.AccountId = acc.id;
                 newCase.RecordTypeId = devRecordTypeId;
                 newCase.ownerId = queueId[0].id;
                 returnList.add(newCase);
             }
         }
        
         return returnList;
    }
    
    
   private static List<Case> casesToUpdate(list<Case> cases)
    {
        if(!cases.isEmpty())
        {
            for(Case c: cases)
            {
                c.Status = 'Demand';
            }
       }
        
        return cases;
    }
i am getting List has more than 1 row for assignment to SObject error when schduling the job.

please some one help me on this.
Best Answer chosen by Shobana Ravichandran
Harsh P.Harsh P.
Try this one: 
 
global with sharing class DebtCollection implements Schedulable 
{
    
    global void execute(SchedulableContext sc)
    {
        createDebtCollectionCases();
        updateAccounts();
        updateCasesToDemand();
        //sendReminderEmails(AccountsRequiringReminder());
    }
    private static void createDebtCollectionCases(){
        try{
           insert casesToCreate(AccountsRequiringDebtCase());
        }catch(Exception e){
            
        }
    }
    
   private static void updateAccounts()
    {
        Account[] accsToUpdate = AccountsRequiringDebtCase();
        for(Account acc : accsToUpdate){
            acc.Has_Open_Debt_Case__c = TRUE;
        }
        try {
            update accsToUpdate;
        }catch(Exception e){
            
        }
    }
      
    private static void updateCasesToDemand()
    {
        try {
            update casesToUpdate(CasesRequiringDemand());
        }catch(Exception e){
            
        }
    }
    
    
    public static List<Account> AccountsRequiringDebtCase()
    {   
        //Accounts with credit term of 30 have their 60 day balance checked on the 2nd of each month
        Integer creditTermDate30 = 2;
        //Accounts with credit term of 35 have their 60 day balance checked on the 7th of each month
        Integer creditTermDate35 = 7;
        //Accounts with credit term of 45 have their 60 day balance checked on the 17th of each month
        Integer creditTermDate45 = 17;
        Account[] accList = new Account[]{};
        if(Date.today().Day()==creditTermDate30){
            accList.add([select Id, Has_Open_Debt_Case__c, Most_Recent_Statement_Date__c from Account where Has_Open_Debt_Case__c = FALSE and Day_60_Balance__c > 0 and Credit_Terms__c = 30]);
            system.debug('accList' +accList);
        }
        if(Date.today().Day()==creditTermDate35){
            accList.add([select Id, Has_Open_Debt_Case__c, Most_Recent_Statement_Date__c from Account where Has_Open_Debt_Case__c = FALSE and Day_60_Balance__c > 0 and Credit_Terms__c = 35]);
        }
        if(Date.today().Day()==creditTermDate45){
            accList.add([select Id, Has_Open_Debt_Case__c, Most_Recent_Statement_Date__c from Account where Has_Open_Debt_Case__c = FALSE and Day_60_Balance__c > 0 and Credit_Terms__c = 45]);
        }
         accList.add([select Id, Has_Open_Debt_Case__c, Most_Recent_Statement_Date__c from Account where Has_Open_Debt_Case__c = FALSE and credit_hold_date__c != null and (Day_60_Balance__c >0 OR Day_90_Balance__c>0)]); 
      
     return  accList;
    }
   
    public static List<Case> CasesRequiringDemand()
    {   
        
        Date demandDay = Date.today().addDays(-30);
        Id DebtRecordType = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Debt Collection').getRecordTypeId();
        
        List<Case> caseList = [select Id, Status from Case where Status != 'Closed' AND recordTypeId = :DebtRecordType AND createddate  = :demandDay ];
        
        return  caseList;
        
    }
    
    
    private static List<Case> casesToCreate(list<Account> accounts)
   {
         List<Case> returnList = new list<Case>();
         Id devRecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Debt Collection').getRecordTypeId();
         List<Group> queueId = [select Id from Group where Name = 'Finance Queue' and Type = 'Queue'  ];
        
        
        
         if(!accounts.isEmpty())
         {
             for(Account acc: accounts)
             {
                 Case newCase = new Case();
                 newCase.AccountId = acc.id;
                 newCase.RecordTypeId = devRecordTypeId;
                 newCase.ownerId = queueId[0].id;
                 returnList.add(newCase);
             }
         }
        
         return returnList;
    }
    
    
   private static List<Case> casesToUpdate(list<Case> cases)
    {
        if(!cases.isEmpty())
        {
            for(Case c: cases)
            {
                c.Status = 'Demand';
            }
       }
        
        return cases;
    }
}

Thanks......!
 

All Answers

Harsh P.Harsh P.
Hi Shobhana,

 AccountsRequiringDebtCase() in this class you are adding query data to the accList,but query perform in this list getting the too many rows So thats why you are getting this error.
Generaly, we perform operation like ListName.add(single_record);
Here you are adding like this ListName.add(ListOfRecords);This is not possible.
Recommending to you, filter the query perfectly to get single record.

Thanks .....!
Anant KamatAnant Kamat
You are calling the AccountsRequiringDebtCase() multiple times(createDebtCollectionCases() and updateAccounts()) from within the SchedulableContext, hence it is difficult to identify the exact line of source of error. Can you post the line number which is throwing this error. You can get this from the debug logs.

Also update the updateAccounts() method as below

private static void updateAccounts()   {     
    List < Account > updAccs = new List < Account > ();    
    List < Account > accsToUpdate = AccountsRequiringDebtCase();    
    for (Account acc: accsToUpdate) {      
        acc.Has_Open_Debt_Case__c = TRUE;      
        updAccs.add(acc);    
    }    
    try {      
        update updAccs;    
    } catch (Exception e) {           }  
}

​​​​​​​Let me know if it works.
Shobana RavichandranShobana Ravichandran
Hi,

Still i am getting the same error... caused by: System.QueryException: List has more than 1 row for assignment to SObject
 
External entry point
Class.Debt.updateAccounts: line 55, column 1
Class.Debt.execute: line 25, column 1
caused by: System.QueryException: List has more than 1 row for assignment to SObject
i would like to get the all records using query and the records needs to be shown in a queue. 
Please help me to resolve this issue

Thanks in advance
Shobana RavichandranShobana Ravichandran
@
Harsh P. i want to get the list of records only... how can i achieve using apex code. pleae help me on this.

Thanks,
Shobana
Shobana RavichandranShobana Ravichandran
If i am putting limit 1, yes the job is running and records are creating. but i need to get the list of record
Harsh P.Harsh P.
Try this one: 
 
global with sharing class DebtCollection implements Schedulable 
{
    
    global void execute(SchedulableContext sc)
    {
        createDebtCollectionCases();
        updateAccounts();
        updateCasesToDemand();
        //sendReminderEmails(AccountsRequiringReminder());
    }
    private static void createDebtCollectionCases(){
        try{
           insert casesToCreate(AccountsRequiringDebtCase());
        }catch(Exception e){
            
        }
    }
    
   private static void updateAccounts()
    {
        Account[] accsToUpdate = AccountsRequiringDebtCase();
        for(Account acc : accsToUpdate){
            acc.Has_Open_Debt_Case__c = TRUE;
        }
        try {
            update accsToUpdate;
        }catch(Exception e){
            
        }
    }
      
    private static void updateCasesToDemand()
    {
        try {
            update casesToUpdate(CasesRequiringDemand());
        }catch(Exception e){
            
        }
    }
    
    
    public static List<Account> AccountsRequiringDebtCase()
    {   
        //Accounts with credit term of 30 have their 60 day balance checked on the 2nd of each month
        Integer creditTermDate30 = 2;
        //Accounts with credit term of 35 have their 60 day balance checked on the 7th of each month
        Integer creditTermDate35 = 7;
        //Accounts with credit term of 45 have their 60 day balance checked on the 17th of each month
        Integer creditTermDate45 = 17;
        Account[] accList = new Account[]{};
        if(Date.today().Day()==creditTermDate30){
            accList.add([select Id, Has_Open_Debt_Case__c, Most_Recent_Statement_Date__c from Account where Has_Open_Debt_Case__c = FALSE and Day_60_Balance__c > 0 and Credit_Terms__c = 30]);
            system.debug('accList' +accList);
        }
        if(Date.today().Day()==creditTermDate35){
            accList.add([select Id, Has_Open_Debt_Case__c, Most_Recent_Statement_Date__c from Account where Has_Open_Debt_Case__c = FALSE and Day_60_Balance__c > 0 and Credit_Terms__c = 35]);
        }
        if(Date.today().Day()==creditTermDate45){
            accList.add([select Id, Has_Open_Debt_Case__c, Most_Recent_Statement_Date__c from Account where Has_Open_Debt_Case__c = FALSE and Day_60_Balance__c > 0 and Credit_Terms__c = 45]);
        }
         accList.add([select Id, Has_Open_Debt_Case__c, Most_Recent_Statement_Date__c from Account where Has_Open_Debt_Case__c = FALSE and credit_hold_date__c != null and (Day_60_Balance__c >0 OR Day_90_Balance__c>0)]); 
      
     return  accList;
    }
   
    public static List<Case> CasesRequiringDemand()
    {   
        
        Date demandDay = Date.today().addDays(-30);
        Id DebtRecordType = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Debt Collection').getRecordTypeId();
        
        List<Case> caseList = [select Id, Status from Case where Status != 'Closed' AND recordTypeId = :DebtRecordType AND createddate  = :demandDay ];
        
        return  caseList;
        
    }
    
    
    private static List<Case> casesToCreate(list<Account> accounts)
   {
         List<Case> returnList = new list<Case>();
         Id devRecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Debt Collection').getRecordTypeId();
         List<Group> queueId = [select Id from Group where Name = 'Finance Queue' and Type = 'Queue'  ];
        
        
        
         if(!accounts.isEmpty())
         {
             for(Account acc: accounts)
             {
                 Case newCase = new Case();
                 newCase.AccountId = acc.id;
                 newCase.RecordTypeId = devRecordTypeId;
                 newCase.ownerId = queueId[0].id;
                 returnList.add(newCase);
             }
         }
        
         return returnList;
    }
    
    
   private static List<Case> casesToUpdate(list<Case> cases)
    {
        if(!cases.isEmpty())
        {
            for(Case c: cases)
            {
                c.Status = 'Demand';
            }
       }
        
        return cases;
    }
}

Thanks......!
 
This was selected as the best answer
Shobana RavichandranShobana Ravichandran
No luck, still getting the same error.Please help me to fix this.
Thanks in advance