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
sfdc13sfdc13 

can some one help me how to test this query

I am getting Error:System.QueryException: List has no rows for assignment to SObject
public String save(String ids){
            Case caseObject = [SELECT Subject, Description, ContactId, Origin, Priority, Status, OwnerId, RecordTypeId, ParentId,  Product_Code__c,Product_Description__c, Lot_Number__c, GMP_Quality__c, Trademark__c, Business_Unit__c,  ProdCert_Topic__c,Rega_Responsibility__c,ProdCert_Request__c,Business_Line_Description__c,Additional_Request_Info__c,Applied_ProdCert_Rule__c,RAAnswer__c FROM Case where Id =: ids];
 
Best Answer chosen by sfdc13
Ajay K DubediAjay K Dubedi
Hi,
Here is the dummy data which I am providing in this test class, I have provided all these standards fields but the custom fields which you are using in your SOQL query you need to create  records for those custom fields by using the same you can cover your 'save' method SOQL query using test class:
@IsTest
public class SaveCase {
        @IsTest
        public status void saveTest(){
        Contact con =new Contact();
        con.LastName='TestContact';
        Insert con;
        Id caseRecordTypeId =Schema.SObjectType.Case.getRecordTypeInfosByName().get('RecordTypeName').getRecordTypeId();
               
        Case CaseObj = new Case();
        CaseObj.Subject = 'Test';
        CaseObj.ContactId=con.Id;
        CaseObj.Description = 'This is test case for Nigeria';
        CaseObj.Status = 'New';
        CaseObj.Origin = 'Web';
        CaseObj.Priority='Medium';
        CaseObj.RecordTypeId = caseRecordTypeId;
        insert CaseObj;
 ApexClassName classInstance=new ApexClassName();
        classInstance.save(CaseObj.Id);
        }
    }

Note:- 'ApexClassName' this is going to be your Name of Apex class.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi

All Answers

Vikash Kumar MandalVikash Kumar Mandal
Hi sfdc13
Use the below code
 
Case caseObject = new Case(); 
List<Case> CaseObj = [SELECT Subject, Description, ContactId, Origin, Priority, Status, OwnerId, RecordTypeId, ParentId, Product_Code__c,Product_Description__c, Lot_Number__c, GMP_Quality__c, Trademark__c, Business_Unit__c, ProdCert_Topic__c,Rega_Responsibility__c,ProdCert_Request__c,Business_Line_Description__c,Additional_Request_Info__c,Applied_ProdCert_Rule__c,RAAnswer__c FROM Case where Id =: ids];
 if(CaseObj.size() > 0) { 
caseObject = CaseObj[0];
 } else { 
caseObject = null; 
}

Try this code it will work.
Thanks!
​​​​​​​Vikash
Deepali KulshresthaDeepali Kulshrestha
Hi,

- I implemented it in my Org and code is running fine. Please use the below code  : 

   
List<Case> caseObject =new List<Case>();
caseObject = [SELECT Subject, Description, ContactId, Origin, Priority, Status, OwnerId, RecordTypeId, ParentId,  Product_Code__c,Product_Description__c, Lot_Number__c, GMP_Quality__c, Trademark__c, Business_Unit__c,  ProdCert_Topic__c,Rega_Responsibility__c,ProdCert_Request__c,Business_Line_Description__c,Additional_Request_Info__c,Applied_ProdCert_Rule__c,RAAnswer__c FROM Case where Id =: ids Limit 1];


 
    
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.
Ajay K DubediAjay K Dubedi
Hi,
Your query is not bulkify that why it throws this exception 'Error:System.QueryException: List has no rows for assignment to SObject'.Try the following code it may helpful for you and for more info about this exception follow the given link-https://help.salesforce.com/articleView?id=000328824&type=1:
 
public String save(String ids){
           
            List<Case> caseList =new List<Case>();
            caseList = [SELECT Subject, Description, ContactId, Origin, Priority, Status, OwnerId, RecordTypeId, ParentId,  Product_Code__c,Product_Description__c, Lot_Number__c, GMP_Quality__c, Trademark__c, Business_Unit__c,ProdCert_Topic__c,Rega_Responsibility__c,ProdCert_Request__c,
            Business_Line_Description__c,Additional_Request_Info__c,Applied_ProdCert_Rule__c,RAAnswer__c FROM Case where Id =: ids LIMIT 10000];
           
            system.debug('caseList->'+caseList);
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
sfdc13sfdc13
Hi,
thanks to everyone, Actually I am getting an error when I run my test class I need to cover that query in a test class Please give me  any suggestions
Ajay K DubediAjay K Dubedi
Hi,
Here is the dummy data which I am providing in this test class, I have provided all these standards fields but the custom fields which you are using in your SOQL query you need to create  records for those custom fields by using the same you can cover your 'save' method SOQL query using test class:
@IsTest
public class SaveCase {
        @IsTest
        public status void saveTest(){
        Contact con =new Contact();
        con.LastName='TestContact';
        Insert con;
        Id caseRecordTypeId =Schema.SObjectType.Case.getRecordTypeInfosByName().get('RecordTypeName').getRecordTypeId();
               
        Case CaseObj = new Case();
        CaseObj.Subject = 'Test';
        CaseObj.ContactId=con.Id;
        CaseObj.Description = 'This is test case for Nigeria';
        CaseObj.Status = 'New';
        CaseObj.Origin = 'Web';
        CaseObj.Priority='Medium';
        CaseObj.RecordTypeId = caseRecordTypeId;
        insert CaseObj;
 ApexClassName classInstance=new ApexClassName();
        classInstance.save(CaseObj.Id);
        }
    }

Note:- 'ApexClassName' this is going to be your Name of Apex class.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
This was selected as the best answer