You need to sign in to do that
Don't have an account?
Sakthidasan
How to cover the Unit Test
I'm new to salesforce.How to write test class for follwing Apex class.it function is get unique ownerId from the object
and find out sum of outcome,howmany event occur in the current month and send the email to the owner.
Apex class:
public class oppOwnerOutcome {
//Remove debug statements
public void executeLogic(){
Map<String,List<Meeting__c>> outCmeMap=new Map<String,List<Meeting__c>>();
Map<String,Decimal> ownerOutComeMap =new Map<String,Decimal>();
Map<String,String> ownerNameMap =new Map<String,String>();
Map<String,String> ownerEmailMap =new Map<String,String>();
Set<Id> ownerIdSet = new Set<Id>();
for(Meeting__c mData:[SELECT id, OutCome__c, Expected_Close_Date__c, AccountMaster__r.Ownerid, AccountMaster__r.Owner.Name FROM Meeting__c WHERE OutCome_Flag__c = false AND Expected_Close_Date__c = THIS_MONTH]){
if(!outCmeMap.containsKey(mData.AccountMaster__r.Owner.id)){
List<Meeting__c> meetingList=new List<Meeting__c>();
meetingList.add(mData);
outCmeMap.put(mData.AccountMaster__r.Owner.id,meetingList);
}
else{
List<Meeting__c> meetingList=new List<Meeting__c>();
//Use OwnerId
meetingList=outCmeMap.get(mData.AccountMaster__r.Owner.id);
meetingList.add(mData);
outCmeMap.put(mData.AccountMaster__r.Owner.id,meetingList);
}
}
for(String owrId:outCmeMap.keySet()){
Decimal totalOutcomePerOwner = 0;
for(Meeting__c mtg: outCmeMap.get(owrId)){
totalOutcomePerOwner = mtg.OutCome__c+totalOutcomePerOwner ;
//System.debug('summary'+res1.OutCome__c);
}
ownerOutComeMap.put(owrId, totalOutComePerOwner);
}
List<User> owRecList = new List<User>();
owRecList = [SELECT Id, Name, Email FROM User WHERE Id IN: outCmeMap.keySet()];
for(User uRec:owRecList){
ownerNameMap.put(uRec.Id, uRec.Name);
ownerEmailMap.put(uRec.Id, uRec.Email);
}
List<EmailObj__c> emailRecList = new List<EmailObj__c>();
//Move hardcoded values to custom labels or settings
for(String owrId:outCmeMap.keySet()){
EmailObj__c emailRec = new EmailObj__c();
emailRec.send_Address__c = ownerEmailMap.get(owrId);
String emailBody = 'Dear '+ownerNameMap.get(owrId)+'<br></br>' +
'Total Opportunity Outcome expected for this month is '+ownerOutComeMap.get(owrId)+'<br></br>' +
'Regards'+'<br></br>' +
'Sales Team';
emailRec.emailBody__c = emailBody;
emailRecList.add(emailRec);
}
//Use database.insert instead of insert statement
//database.saveResult[] srList = database.insert(emailRecList, false);
//for(Database.saveResult sr : srList) {
insert emailRecList;
//System.debug('summary:'+summary);
}
}
Apex Test Class:
@isTest
private class oppOwnerOutcomeTest {
static testMethod void testoutCome(){
Account acc=new Account(Name='TestMethod');
insert acc;
Opportunity opp=new Opportunity(Name='TestCaseOpp',StageName='Prospecting',CloseDate=system.today());
insert opp;
Opportunity opp1=new Opportunity(Name='TestCaseOpp',StageName='Prospecting',CloseDate=system.today());
insert opp1;
Meeting__c mRec = new Meeting__c( AccountMaster__c = acc.id, OutCome_Flag__c = false );
insert mRec ;
Meeting__c mRec1 = new Meeting__c( AccountMaster__c = acc.id, OutCome_Flag__c = false );
insert mRec1 ;
oppOwnerOutcome tt=new oppOwnerOutcome();
tt.executeLogic();
}
}
and find out sum of outcome,howmany event occur in the current month and send the email to the owner.
Apex class:
public class oppOwnerOutcome {
//Remove debug statements
public void executeLogic(){
Map<String,List<Meeting__c>> outCmeMap=new Map<String,List<Meeting__c>>();
Map<String,Decimal> ownerOutComeMap =new Map<String,Decimal>();
Map<String,String> ownerNameMap =new Map<String,String>();
Map<String,String> ownerEmailMap =new Map<String,String>();
Set<Id> ownerIdSet = new Set<Id>();
for(Meeting__c mData:[SELECT id, OutCome__c, Expected_Close_Date__c, AccountMaster__r.Ownerid, AccountMaster__r.Owner.Name FROM Meeting__c WHERE OutCome_Flag__c = false AND Expected_Close_Date__c = THIS_MONTH]){
if(!outCmeMap.containsKey(mData.AccountMaster__r.Owner.id)){
List<Meeting__c> meetingList=new List<Meeting__c>();
meetingList.add(mData);
outCmeMap.put(mData.AccountMaster__r.Owner.id,meetingList);
}
else{
List<Meeting__c> meetingList=new List<Meeting__c>();
//Use OwnerId
meetingList=outCmeMap.get(mData.AccountMaster__r.Owner.id);
meetingList.add(mData);
outCmeMap.put(mData.AccountMaster__r.Owner.id,meetingList);
}
}
for(String owrId:outCmeMap.keySet()){
Decimal totalOutcomePerOwner = 0;
for(Meeting__c mtg: outCmeMap.get(owrId)){
totalOutcomePerOwner = mtg.OutCome__c+totalOutcomePerOwner ;
//System.debug('summary'+res1.OutCome__c);
}
ownerOutComeMap.put(owrId, totalOutComePerOwner);
}
List<User> owRecList = new List<User>();
owRecList = [SELECT Id, Name, Email FROM User WHERE Id IN: outCmeMap.keySet()];
for(User uRec:owRecList){
ownerNameMap.put(uRec.Id, uRec.Name);
ownerEmailMap.put(uRec.Id, uRec.Email);
}
List<EmailObj__c> emailRecList = new List<EmailObj__c>();
//Move hardcoded values to custom labels or settings
for(String owrId:outCmeMap.keySet()){
EmailObj__c emailRec = new EmailObj__c();
emailRec.send_Address__c = ownerEmailMap.get(owrId);
String emailBody = 'Dear '+ownerNameMap.get(owrId)+'<br></br>' +
'Total Opportunity Outcome expected for this month is '+ownerOutComeMap.get(owrId)+'<br></br>' +
'Regards'+'<br></br>' +
'Sales Team';
emailRec.emailBody__c = emailBody;
emailRecList.add(emailRec);
}
//Use database.insert instead of insert statement
//database.saveResult[] srList = database.insert(emailRecList, false);
//for(Database.saveResult sr : srList) {
insert emailRecList;
//System.debug('summary:'+summary);
}
}
Apex Test Class:
@isTest
private class oppOwnerOutcomeTest {
static testMethod void testoutCome(){
Account acc=new Account(Name='TestMethod');
insert acc;
Opportunity opp=new Opportunity(Name='TestCaseOpp',StageName='Prospecting',CloseDate=system.today());
insert opp;
Opportunity opp1=new Opportunity(Name='TestCaseOpp',StageName='Prospecting',CloseDate=system.today());
insert opp1;
Meeting__c mRec = new Meeting__c( AccountMaster__c = acc.id, OutCome_Flag__c = false );
insert mRec ;
Meeting__c mRec1 = new Meeting__c( AccountMaster__c = acc.id, OutCome_Flag__c = false );
insert mRec1 ;
oppOwnerOutcome tt=new oppOwnerOutcome();
tt.executeLogic();
}
}
Please try the below code:
Regards,
Mahesh
Please let me know what is the actual issue you are getting for your test class.
Regards,
Mahesh
After copying my code till what line the code got covered?
Also provide the % code coverage.
Regards,
Mahesh
Regards,
Mahesh
Also paste the latest Test Class.
Regards,
Mahesh