• Cindy
  • NEWBIE
  • 10 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 2
    Replies
Hi please help me write a test class for a schedulable apex class that sends an email to all users who have a checkbox on their user checked.
 
//Send email to users where receive Grants email = TRUE
public class NoActivityThisQrtr implements Schedulable {
    public void execute(SchedulableContext sc) {
        // Get list of opportunities where the opps> account last activity date not in the current quarter
        List<Opportunity> listOfnoactivityOps = [Select Id, Name, StageName ,Account_Name__c,Accouint_Last_Activity_Date__c
                                             From Opportunity 
                                             Where Accouint_Last_Activity_Date__c != THIS_FISCAL_QUARTER];
        
        // Get user ids in a list
        User adminProfile = [Select Id From User Where Receive_TB_Grant_Emails_Notifications__c = TRUE];
        List<Id> listOfUserIds = new List<Id>();
        for(User usr : [Select Id From User Where ProfileId = :adminProfile.Id]) {
            listOfUserIds.add(usr.Id);
        }
        
        // Get all the opportunities Name, StageName, Acccount and Account Last Activity Date in a String
        String closedOpportunities = 'Opportunity Name' + ' : ' + 'Stage' + ':' +'Account' + ':' +'Account Last Activity Date';
        for(Opportunity op : listOfnoactivityOps) {
            closedOpportunities = ClosedOpportunities + '\n' + op.Name + ' : ' + op.StageName + ':' + op.Account_Name__c +':' + OP.Accouint_Last_Activity_Date__c;
        }
        
        sendmail(listOfUserIds, closedOpportunities);
    }
    
    public void sendmail(List<Id> listOfUserIds, String closedOpportunities) {
        // Creating instance of email and set values
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        email.setToAddresses(listOfUserIds);
        email.setSubject('Accounts without Activity this month');
        email.setPlainTextBody( 'Hello, Here are opportunities whose accounts havent had activity this quarter'+ 
            closedOpportunities);
        
        // Send email
        Messaging.sendEmail(new List<Messaging.SingleEmailMessage> {email});
    }
}

this is the test class i have, no errors but also no coverage
 
@isTest
private class NoActivityThisQrtrTest
{
    static testmethod void schedulerTest() 
    {
        String CRON_EXP = '0 0 0 15 3 ? *';
        
        // Create your test data
        List<Opportunity> listOfnoactivityOps = [Select Id, Name, StageName ,Account_Name__c,Accouint_Last_Activity_Date__c
                                             From Opportunity 
                                             Where Accouint_Last_Activity_Date__c != THIS_FISCAL_QUARTER];
        
        User adminProfile = [Select Id From User Where Receive_TB_Grant_Emails_Notifications__c = TRUE];
        List<Id> listOfUserIds = new List<Id>();
        for(User usr : [Select Id From User Where ProfileId = :adminProfile.Id]) {
            listOfUserIds.add(usr.Id);
        }
        
        Test.startTest();

            String jobId = System.schedule('ScheduleApexClassTest',  CRON_EXP, new NoActivityThisQrtr());
            CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
            System.assertEquals(CRON_EXP, ct.CronExpression);
            System.assertEquals(0, ct.TimesTriggered);

        Test.stopTest();
    }
}

I have tried this test class and only got 17%
 
@istest
public with sharing class NoActivityThisQrtrTest {
    static testmethod void testSample() {
        Test.startTest();
        NoActivityThisQrtr obj = new NoActivityThisQrtr();
        obj.execute( null );
        Test.stopTest();
    }
}

 
  • June 24, 2022
  • Like
  • 0
Please help me write a test class for an apex trigger that sends an email when a contact is created.
 
trigger EmailContact on Contact (after insert) {
List<Messaging.SingleEmailMessage> emailList= new List<Messaging.SingleEmailMessage>();
EmailTemplate emailTemplate = [Select Id,Subject,Description,HtmlValue,DeveloperName,Body from EmailTemplate where name='Contract Signed Thank you'];
for(Contact conObj:Trigger.new){
if (conObj.Email != null) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(conObj.Id);
mail.setSenderDisplayName('System Administrator');
mail.setUseSignature(false);
mail.setBccSender(false);
mail.setSaveAsActivity(true);
mail.setTemplateID(emailTemplate.Id);
mail.toAddresses = new String[]{conObj.Email};
emailList.add(mail);
}
}
if(emailList.size()>0){
Messaging.SendEmailResult[] results = Messaging.sendEmail(emailList);
if (results[0].success)
{
System.debug('The email was sent successfully.');
} else {
System.debug('The email failed to send: '+ results[0].errors[0].message);
}
}
}


 
This is what i have so far. i get an error that lines 9&15 are missing ','
@isTest private class EmailContactTestClass { static testMethod void validateEmailContact() { Contact c = new Contact(LastName='Test',Email='test@test.org'); System.debug(c.LastName); insert c; System.assertEquals(0, Limits.getEmailInvocations(); c = [SELECT id FROM Contact WHERE LastName ='Test' limit 1]; System.assertEquals(1, Limits.getEmailInvocations(); } }

 
  • May 15, 2022
  • Like
  • 0
I am new to apex and i am attempting to create an apex trigger that when a user enters a follow up date on the log a call action, create a task where the status is "not started" and the due date is the Follow up date on the call.

This is what i have so far and it is creating 2 tasks. What should i change to resolve this?
 
trigger CreateFollowUpTask on Task (after insert, after update) {
	Task[] oTasks = new List<Task>();
    for (Task C : trigger.new ) {
        if (C.Follow_Up_Date_Time__c !=null){
            oTasks new Task(Subject='Follow up', ActivityDate=C.Follow_Up_Date_Time__c,Status='Not Started',OwnerId=C.OwnerId,WhatId=C.WhatId,WhoId=C.WhoId));
        }
    }
   insert oTasks;

}


 
  • April 14, 2022
  • Like
  • 0
Please help me write a test class for an apex trigger that sends an email when a contact is created.
 
trigger EmailContact on Contact (after insert) {
List<Messaging.SingleEmailMessage> emailList= new List<Messaging.SingleEmailMessage>();
EmailTemplate emailTemplate = [Select Id,Subject,Description,HtmlValue,DeveloperName,Body from EmailTemplate where name='Contract Signed Thank you'];
for(Contact conObj:Trigger.new){
if (conObj.Email != null) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(conObj.Id);
mail.setSenderDisplayName('System Administrator');
mail.setUseSignature(false);
mail.setBccSender(false);
mail.setSaveAsActivity(true);
mail.setTemplateID(emailTemplate.Id);
mail.toAddresses = new String[]{conObj.Email};
emailList.add(mail);
}
}
if(emailList.size()>0){
Messaging.SendEmailResult[] results = Messaging.sendEmail(emailList);
if (results[0].success)
{
System.debug('The email was sent successfully.');
} else {
System.debug('The email failed to send: '+ results[0].errors[0].message);
}
}
}


 
This is what i have so far. i get an error that lines 9&15 are missing ','
@isTest private class EmailContactTestClass { static testMethod void validateEmailContact() { Contact c = new Contact(LastName='Test',Email='test@test.org'); System.debug(c.LastName); insert c; System.assertEquals(0, Limits.getEmailInvocations(); c = [SELECT id FROM Contact WHERE LastName ='Test' limit 1]; System.assertEquals(1, Limits.getEmailInvocations(); } }

 
  • May 15, 2022
  • Like
  • 0
I am new to apex and i am attempting to create an apex trigger that when a user enters a follow up date on the log a call action, create a task where the status is "not started" and the due date is the Follow up date on the call.

This is what i have so far and it is creating 2 tasks. What should i change to resolve this?
 
trigger CreateFollowUpTask on Task (after insert, after update) {
	Task[] oTasks = new List<Task>();
    for (Task C : trigger.new ) {
        if (C.Follow_Up_Date_Time__c !=null){
            oTasks new Task(Subject='Follow up', ActivityDate=C.Follow_Up_Date_Time__c,Status='Not Started',OwnerId=C.OwnerId,WhatId=C.WhatId,WhoId=C.WhoId));
        }
    }
   insert oTasks;

}


 
  • April 14, 2022
  • Like
  • 0