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
TechnosTechnos 

How to resolve the error If query where condtion has no data (List has now rows)

Hi All !

 

This is my code

 

 

-----------------------------------------------------VF Page----------------------------------------------------------------------------------------------------

<apex:page controller="GenerateCaseNumber" >
<apex:form >
   <apex:pageblock title="GenerateCaseNumber" >
  <apex:pageBlockSection title="Case Information" >
                 <apex:selectList required="true" multiselect="false" size="1" label="Case(s)"  value="{!selectedValue}" style="align:left">
                      <apex:actionSupport event="onchange" action="{!GenerateCaseNumber}" reRender="detail"/>
                     <apex:selectOptions value="{!CaseTypeOption }"/>
                 </apex:selectList>
             </apex:pageBlockSection>
             <apex:pageBlockSection id="detail">
              <apex:outputLabel value="{!TestFinalCaseNumber}"></apex:outputLabel>
             </apex:pageBlockSection>
             </apex:pageblock>
             </apex:form>
</apex:page>

 

-----------------------------------------------------------------------------------Controller--------------------------------------------------------------------------------------------

public with sharing class GenerateCaseNumber
{
    public string selectedvalue {get;set;}
    public CaseType__c objCaseType;
    public Apcase__c objApcase;
    public string TestFinalCaseNumber{get;set;}   
    public string TestCaseNumber {get;set;}
    string CaseNumber = null;
   
    public GenerateCaseNumber ()
     {
       objcaseType = new CaseType__c ();
       objApcase= new Apcase__c();
    }
  
    public  List<selectOption> CaseTypeOption
     {
         get
         {
             List<selectOption> CaseTypeName =new List<selectOption>();
             CaseTypeName.add(new selectOption('None', 'None'));
             for (CaseType__c cn :[select  Name , CaseTypeName__c from CaseType__c])
             CaseTypeName.add(new selectOption(cn.ID, cn.CaseTypeName__c));
             return CaseTypeName;
         }
         private set;
     }
     public PageReference GenerateCaseNumber()
     {
         string casetypeId = selectedValue;
         string DesignatorCode;
         objcaseType = [select  Name,DesignatorCode__c  from CaseType__c where ID =:casetypeId  ];
         DesignatorCode = objcaseType.DesignatorCode__c;
         Date objDate = Date.today(); //returns currnt date
         integer CurrentYear = objDate.Year(); // Returns year from the date (Return type integer)
       
        string trimyear =string.valueof(CurrentYear);  //Convert int to String
        string strtrimyear =  trimyear.substring(2, 4); // returns subtring from year
        string LikeDesignatorCode  = '';
        LikeDesignatorCode = DesignatorCode  + strtrimyear + '-';
       
        string LikeCondition = LikeDesignatorCode + '%';
        
     objApcase = [ SELECT Name,CaseNo__c FROM Apcase__c WHERE CaseNo__c  like : LikeCondition ORDER BY CaseNo__c  DESC LIMIT 1];
       CaseNumber  = objApCase.CaseNo__c;
      
      
        if( CaseNumber != Null )
        {
        string strOnlyCaseDigit = CaseNumber.RemoveStart(LikeDesignatorCode );
      
         string Combineconcatinate = '';

        for(integer i = 0 ; i<strOnlyCaseDigit.length();i++)
        {
          string concatinate = strOnlyCaseDigit.substring(i,i+1);
          if( concatinate == '0')
          {
            Combineconcatinate = concatinate + Combineconcatinate ;
          }
          else
          {
            break;
          }
        }
       
        TestCaseNumber  = Combineconcatinate ;
        integer intOnlyCaseDigit = integer.Valueof(strOnlyCaseDigit);
        integer incrementCaseDigit = intOnlyCaseDigit +1;
        string strincrementCaseDigit  = string.valueof(incrementCaseDigit );
       
        TestFinalCaseNumber = DesignatorCode  + strtrimyear + '-'+Combineconcatinate  + strincrementCaseDigit; 
        }
        else
        {
        TestFinalCaseNumber = LikeDesignatorCode +'000001';
        }
    
         return null;
     }

}

 

By this code I  am binding on drop down list, on the selection of this drop down I am gettting one autogenerated CaseNumber according to selection of CaseType. But problem is just supopose if I did not get the data according to where condition in

  objApcase = [ SELECT Name,CaseNo__c FROM Apcase__c WHERE CaseNo__c  like : LikeCondition ORDER BY CaseNo__c  DESC LIMIT 1];

 

than it throw an exception " List has no rows to assign the Sobject". I know that there is no data in table according to condition but how to solve this issue  if where condition did not satisfy. Please  tell me. Thanks in Advance !!

 

Regards

Raman

 

Best Answer chosen by Admin (Salesforce Developers) 
Avidev9Avidev9

You can grab the data in a list, and it will not throw the error, instead it will return a empty list

 

public with sharing class GenerateCaseNumber {
    public string selectedvalue {
        get;
        set;
    }
    public CaseType__c objCaseType;
    public Apcase__c objApcase;
    public string TestFinalCaseNumber {
        get;
        set;
    }
    public string TestCaseNumber {
        get;
        set;
    }
    string CaseNumber = null;

    public GenerateCaseNumber() {
        objcaseType = new CaseType__c();
        objApcase = new Apcase__c();
    }

    public List < selectOption > CaseTypeOption {
        get {
            List < selectOption > CaseTypeName = new List < selectOption > ();
            CaseTypeName.add(new selectOption('None', 'None'));
            for (CaseType__c cn: [select Name, CaseTypeName__c from CaseType__c])
                CaseTypeName.add(new selectOption(cn.ID, cn.CaseTypeName__c));
            return CaseTypeName;
        }
        private set;
    }
    public PageReference GenerateCaseNumber() {
        string casetypeId = selectedValue;
        string DesignatorCode;
        objcaseType = [select Name, DesignatorCode__c from CaseType__c where ID = : casetypeId];
        DesignatorCode = objcaseType.DesignatorCode__c;
        Date objDate = Date.today(); //returns currnt date
        integer CurrentYear = objDate.Year(); // Returns year from the date (Return type integer)

        string trimyear = string.valueof(CurrentYear); //Convert int to String
        string strtrimyear = trimyear.substring(2, 4); // returns subtring from year
        string LikeDesignatorCode = '';
        LikeDesignatorCode = DesignatorCode + strtrimyear + '-';

        string LikeCondition = LikeDesignatorCode + '%';

        List<Case> cases = [SELECT Name, CaseNo__c FROM Apcase__c WHERE CaseNo__c like: LikeCondition ORDER BY CaseNo__c DESC LIMIT 1];
        if(!cases.isEmpty()){
 			objApCase = cases[0];
 			CaseNumber = objApCase.CaseNo__c;
        }
        

        if (CaseNumber != Null) {
            string strOnlyCaseDigit = CaseNumber.RemoveStart(LikeDesignatorCode);

            string Combineconcatinate = '';
            for (integer i = 0; i < strOnlyCaseDigit.length(); i++) {
                string concatinate = strOnlyCaseDigit.substring(i, i + 1);
                if (concatinate == '0') {
                    Combineconcatinate = concatinate + Combineconcatinate;
                } else {
                    break;
                }
            }

            TestCaseNumber = Combineconcatinate;
            integer intOnlyCaseDigit = integer.Valueof(strOnlyCaseDigit);
            integer incrementCaseDigit = intOnlyCaseDigit + 1;
            string strincrementCaseDigit = string.valueof(incrementCaseDigit);

            TestFinalCaseNumber = DesignatorCode + strtrimyear + '-' + Combineconcatinate + strincrementCaseDigit;
        } else {
            TestFinalCaseNumber = LikeDesignatorCode + '000001';
        }

        return null;
    }
}

 

All Answers

Avidev9Avidev9

You can grab the data in a list, and it will not throw the error, instead it will return a empty list

 

public with sharing class GenerateCaseNumber {
    public string selectedvalue {
        get;
        set;
    }
    public CaseType__c objCaseType;
    public Apcase__c objApcase;
    public string TestFinalCaseNumber {
        get;
        set;
    }
    public string TestCaseNumber {
        get;
        set;
    }
    string CaseNumber = null;

    public GenerateCaseNumber() {
        objcaseType = new CaseType__c();
        objApcase = new Apcase__c();
    }

    public List < selectOption > CaseTypeOption {
        get {
            List < selectOption > CaseTypeName = new List < selectOption > ();
            CaseTypeName.add(new selectOption('None', 'None'));
            for (CaseType__c cn: [select Name, CaseTypeName__c from CaseType__c])
                CaseTypeName.add(new selectOption(cn.ID, cn.CaseTypeName__c));
            return CaseTypeName;
        }
        private set;
    }
    public PageReference GenerateCaseNumber() {
        string casetypeId = selectedValue;
        string DesignatorCode;
        objcaseType = [select Name, DesignatorCode__c from CaseType__c where ID = : casetypeId];
        DesignatorCode = objcaseType.DesignatorCode__c;
        Date objDate = Date.today(); //returns currnt date
        integer CurrentYear = objDate.Year(); // Returns year from the date (Return type integer)

        string trimyear = string.valueof(CurrentYear); //Convert int to String
        string strtrimyear = trimyear.substring(2, 4); // returns subtring from year
        string LikeDesignatorCode = '';
        LikeDesignatorCode = DesignatorCode + strtrimyear + '-';

        string LikeCondition = LikeDesignatorCode + '%';

        List<Case> cases = [SELECT Name, CaseNo__c FROM Apcase__c WHERE CaseNo__c like: LikeCondition ORDER BY CaseNo__c DESC LIMIT 1];
        if(!cases.isEmpty()){
 			objApCase = cases[0];
 			CaseNumber = objApCase.CaseNo__c;
        }
        

        if (CaseNumber != Null) {
            string strOnlyCaseDigit = CaseNumber.RemoveStart(LikeDesignatorCode);

            string Combineconcatinate = '';
            for (integer i = 0; i < strOnlyCaseDigit.length(); i++) {
                string concatinate = strOnlyCaseDigit.substring(i, i + 1);
                if (concatinate == '0') {
                    Combineconcatinate = concatinate + Combineconcatinate;
                } else {
                    break;
                }
            }

            TestCaseNumber = Combineconcatinate;
            integer intOnlyCaseDigit = integer.Valueof(strOnlyCaseDigit);
            integer incrementCaseDigit = intOnlyCaseDigit + 1;
            string strincrementCaseDigit = string.valueof(incrementCaseDigit);

            TestFinalCaseNumber = DesignatorCode + strtrimyear + '-' + Combineconcatinate + strincrementCaseDigit;
        } else {
            TestFinalCaseNumber = LikeDesignatorCode + '000001';
        }

        return null;
    }
}

 

This was selected as the best answer
TechnosTechnos

Thanks this worked

 

Cheers

Raman