Can someone please give me a test class which can cover the below method, I have tried all possible ways and not able to increase code coverage. Basically I am querying the Opportunities which are not attended in last 5 days (not field changes, no related record creation/edition in last 5 days). So when it comes to Test class I am not able to set the Last modified date field to less than 5 days. Below is my code.
@AuraEnabled(cacheable=true)
public static Integer getUnattendedOpportunitiesCount() {
Id userId = UserInfo.getUserId();
User loggedInUser =[SELECT ContactId FROM User WHERE Id = :userId LIMIT 1];
if(loggedInUser.ContactId == null){
return 0;
}
if(Test.isrunningtest()){
System.debug('testmodeactive');
List<Opportunity> potentialUnattendedOpps = [
SELECT Id FROM Opportunity
WHERE StageName NOT IN ('Handover','Closed Won','Closed Lost')
];
return potentialUnattendedOpps.size();
}
else{
System.debug('running actual logic');
Contact userContact = [SELECT Id, FordSiteCode__c FROM Contact WHERE Id = :loggedInUser.ContactId LIMIT 1];
// Step 2: Get the franchise related to the user
List<String> franchiseNames = getFranchiseName(
);
if (franchiseNames.isEmpty()) {
return 0;
}
List<String> excludedStages = new List<String>{'Handover', 'Closed Lost', 'Closed Won'};
DateTime fiveDaysAgo = System.now().addDays(-5);
List<Opportunity> potentialUnattendedOpps = [
SELECT Id FROM Opportunity
WHERE StageName NOT IN :excludedStages
AND LastModifiedDate < :fiveDaysAgo AND SNP_GVC_Franchise_Account__r.Name IN:franchiseNames
];
if (potentialUnattendedOpps.isEmpty()) {
return 0;
}
Set<Id> oppIds = new Set<Id>();
for (Opportunity opp : potentialUnattendedOpps) {
oppIds.add(
);
}
Set<Id> attendedOppIds = new Set<Id>();
for (Task t : [SELECT WhatId FROM Task WHERE WhatId IN :oppIds AND LastModifiedDate >= :fiveDaysAgo]) {
attendedOppIds.add(t.WhatId);
}
for (Quote q : [SELECT OpportunityId FROM Quote WHERE OpportunityId IN :oppIds AND LastModifiedDate >= :fiveDaysAgo]) {
attendedOppIds.add(q.OpportunityId);
}
for (ServiceAppointment o : [
SELECT Opportunity__c FROM ServiceAppointment
WHERE Opportunity__c IN :oppIds
AND LastModifiedDate >= :fiveDaysAgo
]) {
attendedOppIds.add(o.Opportunity__c);
}
oppIds.removeAll(attendedOppIds);
return oppIds.size();
}
}
Please help me as I am not able to find any possible test class which covers above method. This is a bit urgent so any help is highly appreciated. Thank you
@* Sales Cloud - Getting Started *@* Experience Cloud *@* Sales Cloud - Best Practices *@*Experience Cloud Developers*@LWCUsing Last Modified Date to see whether an Opportunity has been "attended to" is a bad idea--it's not a meaningful indicator, but it's up to you if you want to continue that road obviously.
You could possibly get coverage by using Test.isrunningtest()--essentially set your 'fiveDaysAgo' variable to today if you're running a test, and today()-5 if not. Your query should get results then.
I dislike these kinds of hacks to get tests passed, but offering guidance anyway.