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
Arya9029Arya9029 

SOQL not working trigger

TRIGGER:
trigger FNR on Fortnightly_Update__c (before insert,before update)
{
    List<Public_Holiday_List__c> FNR =[select id from Public_Holiday_List__c];
    system.debug('FNR Trigger holiday List Count >>> '+FNR.size());
}

TEST CLASS:
@isTest public static void Insert_fortnightly_Already_Exist()
{
        try
    {
         Fortnightly_Update__c f=new Fortnightly_Update__c();
            f.Account__c=[select id from Account where id='0016F00003uavZd'].id;
            insert f;
            
        }catch(DMLException e)
        {
            System.debug('Insert_fortnightly_Already_Exist_Error >>> '+e.getMessage());
        }
}


When the following code is executed in Anonymous Window, it give output 19
List<Public_Holiday_List__c> FNR =[select id from Public_Holiday_List__c];
system.debug('FNR Trigger holiday List Count >>> '+FNR.size());

However, when the same is executed within a trigger as shown above, the output is 0.

I would like to know what is wrong, why the SOQL is not returning the record when excuted within the trigger.
CharuDuttCharuDutt
Hii Arya 
Try Below Test Class
@isTest public static void Insert_fortnightly_Already_Exist()
{
        try
    {
	  Account acc = new Account();
	  Acc.Name = 'Test Account';
	  Insert Acc;
         Fortnightly_Update__c f=new Fortnightly_Update__c();
            f.Account__c = Acc.id;
            /* fill all required Fields*/
            insert f;
            
        }catch(DMLException e)
        {
            System.debug('Insert_fortnightly_Already_Exist_Error >>> '+e.getMessage());
        }
}
Please Mark It As Best Answer If It Helps
Thank You!
Arya9029Arya9029
Hi CharuDutt,

Thank you for your reply,
My problem is with the following line of SOQL code inside the trigger and not the test class.
If I excute the below code in anonymous window or SOQL editor it will fetch 19 records.
However this line of code does not work inside a trigger. 
It always return the count of records inside the FNR list as zero.

List<Public_Holiday_List__c> FNR =[select id from Public_Holiday_List__c];
CharuDuttCharuDutt
Hii Arya
Try Below Test Class
@isTest public static void Insert_fortnightly_Already_Exist()
{
        try
    {
         Public_Holiday_List__c f=new Public_Holiday_List__c();
          /*Fill All Required Fields*/
            insert f;
            
        }catch(DMLException e)
        {
            System.debug('Insert_fortnightly_Already_Exist_Error >>> '+e.getMessage());
        }
}
Please Mark It As Best Answer If It Helps
Thank You!
Arya9029Arya9029
Thank You ! 

But the test class that was provided initially is fine, the test class is only writtern to excute the trigger, which it does, however the SOQL statement within the trigger returns the record count as 0.

List<Public_Holiday_List__c> FNR =[select id from Public_Holiday_List__c];
system.debug('FNR Trigger holiday List Count >>> '+FNR.size());

However, when the same code is executed in Anonymous window, it returns the records count as 19 which is correct.

I would like to know why the SOQL statement is not working inside the trigger, but executes fine inside Anonymous window.
 
Carl Porch 17Carl Porch 17
A lot of time has passed since the issue was posed, but since I ran into a similar issue thought that I would provide an answer.  Basically, when the Trigger is tested -- with the Test Class -- unless the @isTest is annotated with (SeeAllData=True), the trigger will only see information added in the Test Class -- and not what the Anonymous Execution window sees (which is everything).  So if (SeeAllData=True) is added to the Test class, then the Trigger will return a similar # of records.