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
NiknitNiknit 

When do we use LIMIT and where clause with ID in SOQL query?

@testSetup
    static void testData()
    {
           Department__c  newdept = new department__c();   
           newdept.name='CSE';
           insert newdept;
    }

    @isTest
    static void validateupdatechange() 
    {

       Department__c newDept = [SELECT Id, no_of_students__c FROM 
       Department__c LIMIT 1];

       student_details__c newstudent = new Student_details__c();

       newstudent.Name='From TestClass';   
       newstudent.department__c = newDept.Id;     

       System.debug('before@@trigger: ' + newDept.no_of_students__c);
       test.startTest();

       insert newstudent;

       test.stopTest();

       newstudent = [SELECT department__c FROM student_details__c WHERE Id 
              =:newstudent.Id];


       newDept = [SELECT no_of_students__c FROM Department__c LIMIT 1];
       System.debug('after@@trigger: ' + newDept.no_of_students__c);


       System.assertEquals(1 , newDept.no_of_students__c);
    }

This code is working, but I don't understand why are we using The LIMIT and WHERE Id =:newstudent.Id. if there is only one record I am inserting then why LIMIT and use where clause with ID.The test fails if I don't use this.
Amit Chaudhary 8Amit Chaudhary 8
// It look you want to fatch only one record. That is why limit is added as 1 
// NOTE:- You alrady created Department__c in testData() method and fatching same record here 
// you can not use newdept.id here as newdept is not in scope in this method.
Department__c newDept = [SELECT Id, no_of_students__c FROM Department__c LIMIT 1];


You are right you added only one record then without limit also you can get record. but if your code contain SeeAllData=true in that case your code will fail as that will return list of record and you are storing same in single object. In that case you code should be like below

List<Department__c> newDept = [SELECT Id, no_of_students__c FROM Department__c ];

If you are using code like below  then try to add limit 1 as you can story only one record.

Department__c newDept = [SELECT Id, no_of_students__c FROM Department__c LIMIT 1];

 
@testSetup
    static void testData()
    {
           Department__c  newdept = new department__c();   
           newdept.name='CSE';
           insert newdept;
    }

    @isTest
    static void validateupdatechange() 
    {
		// It look you want to fatch only one record. That is why limit is added as 1	
		// NOTE:- You alrady created  Department__c in testData() method and fatching same record here
		// you can not use newdept.id here as newdept is not in scope in this method.

       Department__c newDept = [SELECT Id, no_of_students__c FROM Department__c LIMIT 1];

       student_details__c newstudent = new Student_details__c();

       newstudent.Name='From TestClass';   
       newstudent.department__c = newDept.Id;     

       System.debug('before@@trigger: ' + newDept.no_of_students__c);
       test.startTest();

       insert newstudent;

       test.stopTest();

	   // Here to validate the result for department__c field you are fatching same student_details__c record again with the help of where 
       newstudent = [SELECT department__c FROM student_details__c WHERE Id =:newstudent.Id];


       newDept = [SELECT no_of_students__c FROM Department__c LIMIT 1];
       System.debug('after@@trigger: ' + newDept.no_of_students__c);


       System.assertEquals(1 , newDept.no_of_students__c);
    }

let us know if you need more detail