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
SFDC 2017SFDC 2017 

Test class fail due to system.Query exception:invalid id field :Null

Class code for particular Method:
 public pagereference SearchManifests(){
        string sProjName = MAdminRecTemp.Project_Name__c;
        string sProjIteration = MAdminRecTemp.Project_Iteration__c;
        string sStory_Issue = MAdminRecTemp.Story_Issue__c;        
        string soql = 'select id, name,Project_Name__c,Project_Iteration__c,Story_Issue__c,Comments__c,CreatedDate  from Manifest_Administration__c';
        string sSearchSpecAdditional = ' where RecordTypeId !=\''+sRecTypeId+'\''; // get only Dev To QA Manifests
        
        if(string.isNotEmpty(sProjName)){
            sSearchSpecAdditional += ' AND Project_Name__c = \''+sProjName+'\'';
        }
        if(string.isNotEmpty(sProjIteration)){
            sSearchSpecAdditional += ' AND Project_Iteration__c = \''+sProjIteration+'\'';
        }        
        if(string.isNotEmpty(sStory_Issue)){
            sSearchSpecAdditional += ' AND Story_Issue__c = \''+sStory_Issue+'\'';
        }
        soql = soql + sSearchSpecAdditional;
        
        MAdminSearchResults = new list<Manifest_Administration__c>();
        MAdminSearchResults = database.Query(soql);
        wrapMAdminSearchResultList = new list<wrapManifestAdminRecord>();
        for(Manifest_Administration__c ma : MAdminSearchResults){
            wrapMAdminSearchResultList.add(new wrapManifestAdminRecord(ma));
        }        
        bAddbtnDisable = (wrapMAdminSearchResultList !=null && wrapMAdminSearchResultList.size()>0 ? false : true);
        ClearSearchFields();
        return null;
    }

My Test class:

@isTest
public class INTC_ManifestAdminNew_CXTest
{
    public static testMethod void Test_INTC_ManifestAdminNew_CX()
     {
      
      String strRecordTypeId = [Select Id From RecordType Where SobjectType = 'Manifest_Administration__c' LIMIT 1].Id;
      
      Test.StartTest();
     //create new Manifest Administration Record
     Manifest_Administration__c ma=new Manifest_Administration__c(Project_Name__c = 'Customer First - Release 1',Project_Iteration__c = 'Sprint 2',
                                                                  Story_Issue__c = 'Prod Migration',
                                                                  Comments__c = 'Test',RecordTypeId=strRecordTypeId);
     insert ma;
     
       List<Manifest_Item__c> MI=new List<Manifest_Item__c>();
       Manifest_Item__c mit=new Manifest_Item__c();
       mit.Manifest_Name__c=ma.id;
       mit.Metadata_Item_Name__c='CRM Developer';
       mit.Metadata_Type__c='Profile';
       
       insert mit;

     ApexPages.StandardController controller = new ApexPages.StandardController(ma);
     INTC_ManifestAdminNew_CX MANC=new INTC_ManifestAdminNew_CX (controller);
     system.assert(MANC != null); 
     INTC_ManifestAdminNew_CX.wrapManifestAdminRecord Wrapvar = new INTC_ManifestAdminNew_CX.wrapManifestAdminRecord (ma); 
           
     MANC.SearchManifests();
     MANC.ClearSearchFields();
         
     MANC.AddRecords();
     MANC.RemoveSelectedSearchRecords();
     MANC.BuildPackage();
     MANC.RemoveSelectedAddedRecords();
     Test.stopTest();
     }
}

If i comment the method for Search Manifests it is covering 33%


Please anyone help me to cover this method 
Best Answer chosen by SFDC 2017
Anupama SamantroyAnupama Samantroy
Hi ,

You need to set the variable sRecTypeId of the controller class in the test class. That is used in the where condition and is null.
So may be what you should do is
MANC.sRecTypeId = strRecordTypeId;
MANC.SearchManifests();

Thanks
Anupama

All Answers

FearNoneFearNone
hi archu,

insert a record first in RecordType-object...
@isTest
public class INTC_ManifestAdminNew_CXTest
{
	public static testMethod void Test_INTC_ManifestAdminNew_CX()
	{
		RecordType rt=new RecordType(SobjectType='Manifest_Administration__c');
		insert rt;

		String strRecordTypeId = [Select Id From RecordType Where SobjectType = 'Manifest_Administration__c' LIMIT 1].Id;
		...

 
Anupama SamantroyAnupama Samantroy
Hi ,

You need to set the variable sRecTypeId of the controller class in the test class. That is used in the where condition and is null.
So may be what you should do is
MANC.sRecTypeId = strRecordTypeId;
MANC.SearchManifests();

Thanks
Anupama
This was selected as the best answer
Anupama SamantroyAnupama Samantroy
Hi Archu, 

The record ma in the test class is of the same recordtype which you are searching? I can see you have put LIMIT 1 in query. Can you fetch the exact recordType which you want. That line is not getting covered because database.query is not returning any rows.Setup your test data properly and it should work.

Thanks
Anupama