+ Start a Discussion
Nick KeehanNick Keehan 

List has no rows, simple IF statement not working if case not found

Hopefully quick question.

Keep getting List has no rows error. Tried a couple of suggestions online like size however my Mycase.Referral_Status__c errors with Method not found. 

Hopefully easy enough to fihure out the issue. The below is under my Save() function. (objacc is referenced on the vf page)
Case MyCase = [Select Id, CaseNumber from Case where CaseNumber = :objacc.Case_Reference__c];
         
 if(MyCase != null){

        MyCase.Referral_Status__c = 'Success';  
        MyCase.Referral_Type__c = 'New';          
        MyCase.VF_Account_Number_C3__c = objacc.Customer_Row_ID__c;         
        update MyCase;
        } 
        else {
           return null;
        }

 
Best Answer chosen by Nick Keehan
Nick KeehanNick Keehan
Thanks for your help.
Figured this one out myself.
Had the query occuring before the IF Statement. That was returning 0 results and erroring. 

instead swapped the query and IF around.
Case MyCase = new Case(); 
 If(objacc.Case_Reference__c != ''){ 
 MyCase = [Select Id, CaseNumber, subject from Case where CaseNumber = :objacc.Case_Reference__c Limit 1];

        MyCase.Referral_Status__c = 'Success';  
        MyCase.Referral_Type__c = 'New';          
        MyCase.VF_Account_Number_C3__c = objacc.Customer_Row_ID__c;         
        update MyCase;
        }

 

All Answers

Raj VakatiRaj Vakati
Try this'
 
Case MyCase = new Case() ;
 MyCase = [Select Id, CaseNumber from Case where CaseNumber = :objacc.Case_Reference__c];
         
 if(MyCase.CaseNumber != null){

        MyCase.Referral_Status__c = 'Success';  
        MyCase.Referral_Type__c = 'New';          
        MyCase.VF_Account_Number_C3__c = objacc.Customer_Row_ID__c;         
        update MyCase;
        } 
        else {
           return null;
        }

 
Nick KeehanNick Keehan
Thanks Raj.

Same Error unfortunatly.

System.QueryException: List has no rows for assignment to SObject
Error is in expression '{!save}' in component <apex:commandButton> in page fibresimplex: Class.AccExtension3.Save: line 124, column 1

Line 124 is the MyCase Query.

My code and your works fine if there is a case number entered, however errors when no case is found.

Any suggestions?
 
Raj VakatiRaj Vakati
Try lik ethis 

 
Case MyCase = new Case(CaseNumber='DummyCaseNumber') ;
 MyCase = [Select Id, CaseNumber from Case where CaseNumber = :objacc.Case_Reference__c];
         
 if(MyCase.CaseNumber != 'DummyCaseNumber'){

        MyCase.Referral_Status__c = 'Success';  
        MyCase.Referral_Type__c = 'New';          
        MyCase.VF_Account_Number_C3__c = objacc.Customer_Row_ID__c;         
        update MyCase;
        } 
        else {
           return null;
        }

 
Nick KeehanNick Keehan
Good idea however the CaseNumber isnt writeable so line 1 causes and error.

Nick


 
Raj VakatiRaj Vakati
you can try with other fields 
Nick KeehanNick Keehan
thanks.

Changes the Case Number checks to Subject which is writeable, no luck.

Same list has no rows error. Ill keep looking online.

thanks
Raj VakatiRaj Vakati
Another one more suggesion for you Nick  .. try this it will work
 
List<Case> MyCase = new list<Case>();
 MyCase.addAll([Select Id, CaseNumber from Case where CaseNumber = :objacc.Case_Reference__c]);
         
 if(MyCase.size()>0){

        MyCase[0].Referral_Status__c = 'Success';  
         MyCase[0].Referral_Type__c = 'New';          
         MyCase[0].VF_Account_Number_C3__c = objacc.Customer_Row_ID__c;         
        update  MyCase[0];
        } 
        else {
           return null;
        }

 
Nick KeehanNick Keehan
Thanks for your help.
Figured this one out myself.
Had the query occuring before the IF Statement. That was returning 0 results and erroring. 

instead swapped the query and IF around.
Case MyCase = new Case(); 
 If(objacc.Case_Reference__c != ''){ 
 MyCase = [Select Id, CaseNumber, subject from Case where CaseNumber = :objacc.Case_Reference__c Limit 1];

        MyCase.Referral_Status__c = 'Success';  
        MyCase.Referral_Type__c = 'New';          
        MyCase.VF_Account_Number_C3__c = objacc.Customer_Row_ID__c;         
        update MyCase;
        }

 
This was selected as the best answer