You need to sign in to do that
Don't have an account?
Arya9029
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.
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.
Try Below Test Class Please Mark It As Best Answer If It Helps
Thank You!
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];
Try Below Test Class Please Mark It As Best Answer If It Helps
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.