+ Start a Discussion
LaaralLaaral 

Problems with apex script

Hi, I got this error message while trying to change Cases Status even though tha case doesn't have a Setup.  --> : CaseResponseTimeTrigger: execution of BeforeUpdate caused by: System.QueryException: List has more than 1 row for assignment to SObject: Class.ServiceAvailability.GetHoursToUse: line 128, column 1


When I check that line 128 :  

 

 public Id GetHoursToUse(Case updatedCase, Setup__c relatedSetup) {
    
      // Get the default business hours
        BusinessHours defaultHours = [select Id from BusinessHours where IsDefault=true];
        
        Account costCenter = [select Id, Name, Account_Top_Parent__c from Account where Id =: relatedSetup.Cost_Center__c];
        System.Debug('CC Id: ' + costCenter.Id);
        System.Debug('CC Name: ' + costCenter.Name);
        
        Account mainAccount = [select Id, Name, V_Account_Number__c from Account where Id =: costCenter.Account_Top_Parent__c];
        System.Debug('MA Id: ' + mainAccount.Id);
        System.Debug('MA Name: ' + mainAccount.Name);
        
        Contract contract = [select Id, AccountNumber__c, Business_Hours__c, Status from Contract where AccountNumber__c =:    mainAccount.Videra_Account_Number__c];
        System.Debug('Contract Id: ' + contract.Id);
        System.Debug('Contract Business Hrs: ' + contract.Business_Hours__c);
        System.Debug('Contract Acc Number: ' + contract.AccountNumber__c);
        
         Id hoursToUse = contract.Business_Hours__c != null ? contract.Business_Hours__c : defaultHours.Id;
        return hoursToUse;
    }  

 

I can't understand why this error occurs?

Best Answer chosen by Admin (Salesforce Developers) 
Avidev9Avidev9
Well you are getting this error because it is returning more than one records. Ideally according to your code there should be only one record returned. Adding "Limit 1" will solve the problem but that is not suggested as it may break the logic.

Try to filter out the additional records by adding more conditions to the where clause.

All Answers

Avidev9Avidev9
Well you are getting this error because it is returning more than one records. Ideally according to your code there should be only one record returned. Adding "Limit 1" will solve the problem but that is not suggested as it may break the logic.

Try to filter out the additional records by adding more conditions to the where clause.
This was selected as the best answer
KrishHariKrishHari

As 'Avidev9' said, your SOQL query returns more than 1 record .To fix this error, use any of the following.

 

  • use 'limit' in your SOQL query. For e.g. Contract contract = [select Id, AccountNumber__c, Business_Hours__c, Status from Contract where AccountNumber__c =:    mainAccount.Videra_Account_Number__c LIMIT 1];
  • use a List<Contract> instead of just contract while assigning. If you are interested in using only one record, particularly the first record , then you can reference the first record as listContracs[0] : For .e.g. List<Contract> listContracts = [select Id, AccountNumber__c, Business_Hours__c, Status from Contract where AccountNumber__c =:    mainAccount.Videra_Account_Number__c];
  • add more conditions to the WHERE clause where you know for sure that it will return only one record. But I always prefer the first two methods as explained above.

Regards,

HK.

LaaralLaaral

Thanks, this helped a lot! ! :)