You need to sign in to do that
Don't have an account?

Why does this trigger work when testing from the UI, but doesn't fire from the test class?
Hi Everyone,
I have a fairly simple trigger on a custom junction object Case_FSE_Assignments between Case and Field_Service_Engineers. When a new Case_FSE_Assignments is inserted, the user ID associated with the Field_Service_Engineer is automatically added as a follower of the case. The trigger works just fine when a Case_FSE_Assignment row is inserted from the UI. However, when inserting from a test class, the trigger doesn't get triggered. The trigger doesn't fail, it simply doesn't trigger. The test class fails with a query exception "List has no rows for assignment to SObject" at the EntitySubscription es assignment statement. Help!
caseFSEFollow Trigger:
trigger caseFSEFollow on Case_FSE_Assignment__c (after insert) {
Map<Id, Field_Service_Engineer__c> allFSEUsers = new Map<ID, Field_Service_Engineer__c>(
[select Id, User__c from Field_Service_Engineer__c]);
for(Case_FSE_Assignment__c caseFSE : Trigger.new)
ChatterUtils.addFollower(allFSEUsers.get(caseFSE.Field_Service_Engineer__c).User__c, CaseFSE.Case__c);
}
CaseFSEFollowTest Class:
@isTest
private class CaseFSEFollowTest {
static testMethod void Test1() {
User UserAMS = [select Id from User where profileId in
(select Id from profile where name = 'Cryo AMS') limit 1];
User UserFSE = [select Id from User where Id in
(select User__c from Field_Service_Engineer__c) limit 1];
Field_Service_Engineer__c FSE = [select Id from Field_Service_Engineer__c
where User__c = :UserFSE.Id];
Case testCase = [select Id from Case limit 1];
System.RunAs(UserAMS) {
Case_FSE_Assignment__c CaseFSE = new Case_FSE_Assignment__c(
Case__c = testCase.Id,
Field_Service_Engineer__c = FSE.Id,
Start_Date__c = system.today());
insert CaseFSE;
EntitySubscription es = [select subscriberID from EntitySubscription where parentID = :testCase.Id
and subscriberID = :UserFSE.Id limit 1];
System.assertEquals(es.subscriberID, UserFSE.Id);
}
}
}
Thanks everyone for helping me with this issue. The ChatterUtils methods are all @future methods, so they did not execute until the test class was finished. The issue was solved by wrapping the test inserts between Test.startTest(); and Test.stopTest() methods. The @future methods in ChatterUtils then executed at the Test.stopTest() command, which allowed Entity Subscription es to be appropriately populated thereafter with the subscription data.
All Answers
Hi,
You are getting this error because may be you are getting the value for UserAMS, UserFSE or FSE value as null i.e. no relevant data for this test method. So I would suggest you that instead of making the query on user object create the user inside the test method. Similarly you have to check the EntitySubscription es=[select subscriberID from EntitySubscription where parentID = :testCase.Id and subscriberID = :UserFSE.Id limit 1]; that it is returing any value or not. If nor then comment the system.asserteqals method inside the test method.
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
Thank you for looking at this, S Jain.
The values for UserAMS, UserFSE, and FSE are not null. They are loaded correctly with the exact same values that work correctly when entered through the UI. The Entity Subscription SOQL returns no values, verifying that the trigger was not activated by the insert statement. I can indeed simply comment out the system.assert, but then the test becomes irrelevant.
The main issue is that the insert statement does not fire the trigger in the test class, even though the same trigger is fired when inserting the same data through the UI. It's definitely a brain teaser!
For testCase, you are assuming that a record already exists in the database. You know this is not best practice, right? It is better if you insert a case record for testCase and use that for your EntitySubscription creation.
Good point, dmcheng. I am new to Apex, so all coding tips are greatly appreciated. I'll try inserting new test Cases, Case_FSE_Assignments, Field_Service_Engineers, and Users and let you know if that solves the problem. Stay tuned!
Here are some links on writing units:
http://wiki.developerforce.com/page/How_to_Write_Good_Unit_Tests
http://wiki.developerforce.com/page/Writing_Unit_Tests
Thanks everyone for helping me with this issue. The ChatterUtils methods are all @future methods, so they did not execute until the test class was finished. The issue was solved by wrapping the test inserts between Test.startTest(); and Test.stopTest() methods. The @future methods in ChatterUtils then executed at the Test.stopTest() command, which allowed Entity Subscription es to be appropriately populated thereafter with the subscription data.