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

Test class for before and after update trigger
Hi all,
I am unable to get the code coverage for below trigger. All lines are covered except 8 and 9 lines.
Scinario: Only once record can be Active for a User. Leader__c is in Lookup-Relationship with User. Also if I update one record "Active__c = True" remaining records of that User should be updated (Active__c = False).
My Test Class:
Thankyou.
I am unable to get the code coverage for below trigger. All lines are covered except 8 and 9 lines.
Scinario: Only once record can be Active for a User. Leader__c is in Lookup-Relationship with User. Also if I update one record "Active__c = True" remaining records of that User should be updated (Active__c = False).
trigger OneRecordCanBeActive on Leader__c(after insert, before update) { if(checkRecursive.runOnce()){ List<Leader__c> lList = new List<Leader__c>(); List<Leader__c> gAccs = [select Id, Active__c, User__c from Leader__c where Id not in :Trigger.new and Active__c = True and User__c = :UserInfo.getUserId()]; for(Leader__c l : gAccs){ l.Active__c = False; lList.add(l); } update lList; } }
My Test Class:
@isTest private class OneRecordCanBeActive_Test { private static testMethod void test() { Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; User u = new User(); u.Alias = 'SysAdmin'; u.Email='sysAdmin@testorg.com'; u.EmailEncodingKey='UTF-8'; u.LastName='Testing'; u.LanguageLocaleKey='en_US'; u.LocaleSidKey='en_US'; u.ProfileId = p.Id; u.TimeZoneSidKey='America/Los_Angeles'; u.UserName='gaccuser@testorg.com'; Insert u; Leader__c l1 = new Leader__c(); l1.Active__c = True ; l1.User__c = u.id; Insert l1; Leader__c l2 = new Leader__c(); l2.Active__c = True ; l2.User__c = u.id; Insert l2; l1.Active__c = True ; Update l1; } }
Thankyou.
'and User__c = :UserInfo.getUserId()' Will check that the Leader__c.User__c is the user is the one running the code - not a user that already exists as a Leader.
I think what you are looking for is something like this instead:
This would also make the test work (I'm not sure it's exactly what you want though):
Thanks for your reply.
I updated my test class. Still the lines 8 and 9 of trigger remian uncovered. Provide me me some solution.
Let us know if this will help you