+ Start a Discussion
Best Answer chosen by Gadivemula Krishna
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Krishna,

Messaging is just the Namespace and we have SingleEmailMessage class in it. 

Let me know if you need any further information.

If this solution helps, Please mark it as best answer.

Best Answer chosen by Gadivemula Krishna
SwethaSwetha (Salesforce Developers) 
HI Krishna,
SingleEmailMessage class contains methods for sending emails.

SingleEmailMessage contains methods listed in 

SingleEmailMessage Constructors:

If this information helps, please mark the answer as best. Thank you
Arthur Almeida 12Arthur Almeida 12 
I have a Unit Test Apex, and I would like to schedule this test for execution every day in a specific hour
how to make this?
Best Answer chosen by Arthur Almeida 12
Arthur Almeida 12Arthur Almeida 12
I solve my problem following this article: https://www.forcetree.com/2019/12/scheduling-run-all-apex-tests-and.html
global class SelfTest implements Schedulable {
    global void execute(SchedulableContext ctx){        
        List<ApexClass> testClasses = [SELECT Id FROM ApexClass WHERE Name = 'NAME_TEST_CLASS'];
        List<ApexTestQueueItem> queueItems = new List<ApexTestQueueItem>();
        for (ApexClass testClass : testClasses) { 
            queueItems.add(new ApexTestQueueItem(ApexClassId=testClass.Id)); 
        if(!Test.isRunningTest()) insert queueItems;    

System.schedule('Self Test', cron, new SelfTest());
Best Answer chosen by santhu kumar
PriyaPriya (Salesforce Developers) 
Hey Santhu,

Here is the sample code to prevent account record. You can modify, replace Account with Opportunity and implement.
trigger AccountDuplicate on Account (before insert)

	Set<String> setName = new Set<String>();
	For(Account acc : trigger.new)
	if(setName.size() > 0 )
		List<Account> lstAccount = [select name ,id from account where name in :setName ];
		Map<String ,Account> mapNameWiseAccount = new Map<String,Account>();
		For(Account acc: lstAccount)
			mapNameWiseAccount.put(acc.name ,acc);
		For(Account acc : trigger.new)
				acc.Name.addError('Name already Exist ');

Kindly mark it as the best answer if it helps.

Thank You,
Priya Ranjan
rohit anand 40rohit anand 40 
I have created a report in lightning experience it had saved in the public folder, one of my users is trying to access it facing the error "insufficient privileges", the user had access to reports and dashboards, unable to figure out the reason.
Best Answer chosen by rohit anand 40
Hi Rohit,

When you create a Report/Dashboards and then save it it some folder. You need to give access to that folder to the users who will be using those reports. Also, View Dashboards in Public Folders and View Reports in Public Folders these two checks should be enabled on the users profile to make sure users have access for reports & dashboards.

Please mark as Best answer if solve your query and close it
Shruthi SinghShruthi Singh 
i want to include this error info into my test class now my test class is covring 91% but i need 100% coverage.

for(Database.Error error : errors) {
                errorStrings = errorStrings + ' ' + error.getMessage();
        return errorStrings;
Best Answer chosen by Shruthi Singh
PriyaPriya (Salesforce Developers) 

Hey Shruthi,

In the test class, You have to create a condition that causes the error, such as leaving a required field (name, for example) blank.

Kindly mark it as the best asnwer if it works for you.

Also refer this where similar discussion has done :- 

Thanks & Regards,

Priya Ranjan

Neha Arora 50Neha Arora 50 
Hello Ohana,

I am getting an error 'Variable does not exist: varName' when I tried to access my global variable in the finish method of the batch class.
Point to Note:
1. Neither I am using a stateful class, not any static variable. So in any case values will be retained in my variable.
2. According to the salesforce article global variables declared in the batch class are accessible in all 3 methods.
Steps already performed:
1. The variable is initiated in the constructor with the parameter passed to the batch class.
2. The same variable is used in the start method to build my query.
When I tries to access in finish method got an error saying the variable does not exist.
Below is the code snippet:

global class clsName implements DataBase.Batchable{
  global string var;
  public void clsName(string param){
     this.var = param;
  public Database.queryLocator start(....){
       return Databse.getQueryLocator('SELECT Id from Account where ID =: 'var');
  public static void execute(....){
     //Some Logic
  public static void finish(....){
     System.debug(var);   //Error variable does not exist: var

Let me know your suggestion or if I am making any mistake.
Thanks in advance.
Best Answer chosen by Neha Arora 50
Garima Agrawal 31Garima Agrawal 31
@Neha Arora 50

Define your variable as static then you can access it to the finish method. This way you can resolve your issue.
global class clsName implements DataBase.Batchable<sObject>{
  global static string var;
  public void clsName(string param){
     var = param;
  public Database.queryLocator start(Database.BatchableContext bc){
       return Database.getQueryLocator('SELECT Id from Account where ID =: '+var);
  public static void execute(Database.BatchableContext BC, List<SObject> sobjects){
     //Some Logic
  public static void finish(Database.BatchableContext bc){
     System.debug(var);   //Error variable does not exist: var
fiona gentryfiona gentry 
Here there is a Trigger which calls Trigger handler and in turn Trigger handler calls the apex class,this  apex class is to create tasks on Case record 
and the goal here is to prevent a Task from being created for 2 scenarios
  • First  Task with the Action__c field being ‘airtel account contact Research’ and once user creates a Task with Action__c as "‘airtel account contact Research’"
  • then they should only  able to create second  Task with Action__c as '1st airtel contact'',

Now after these 2 tasks creation is done then user can create any task with any picklist value in Action__c field

Here is Trigger which calls TaskTriggerHandler
trigger TaskTrigger on Task (before insert) {

Here is TaskTriggerHandler 
public class TaskTriggerHandler {
    public static void beforeInsert(List<Task> tasks){
        final string ERTTRECORDTYPENAME = 'ERTT_Task_Record_Type'; // Filter String to get the ERTT Task REcordType Id
        Id recordTypeId_ERT = [select id, DeveloperName from RecordType where sobjecttype = 'Task' AND developername =: ERTRECORDTYPENAME].Id;
        List<Task> ERTTasks = new List<Task>();
        Set<Id> caseIdset = new Set<Id>();
        for(Task taskProcess : tasks){
            // Functionality Added to Validate only the ERTT_Task_Record_Type Record type. 
            if(taskProcess.RecordTypeId == recordTypeId_ERT && taskProcess.Action__c != 'airtel account contact Research' )
                if(taskProcess.WhatId != null)
        // Making sure there is data in the ERTT Tasks and What Id of the Tasks 
        if(!ERTTasks.isEmpty() && !CaseIdset.isEmpty())

Here is Apex class with Logic written
public class ERTT_TaskCreationValidation {
     public static void isairtelAccountResearchcompleted_New(List<Task> ERTTTasks,Set<Id> caseIdset)    {
        list<Task> lstTAsk=new list<Task>();
		map<Id, Case> mapCaseWithTaskList = new  map<Id, Case>([select id, (Select id, Action__c from Tasks )  from Case where Id in: caseIdset]); 
        for(Task t : ERTTTasks)
            Boolean validationFlag = true;
            Boolean validationFlag2 = true;
            System.debug('mapCaseWithTaskList.gett.WhatId.tasks'+ mapCaseWithTaskList.get(t.WhatId).tasks);
            for(Task t1:  mapCaseWithTaskList.get(t.WhatId).tasks){
                if(t1.Action__c == 'airtel account contact Research')
                    validationFlag = false;
                t.addError('Please Create a task for airtel account contact Research before creating any other Task');
System.debug(' lstTAsk' +lstTAsk);
            if(lstTAsk.size()==2 && t.Action__c == '2nd field'){
                    t.addError('Please Create a task for 2nd field before creating any other Task');


Now the problem is when Task is getting created at Case ,the second Error message is thrown which is "Please Create a task for 2nd field before creating any other Task" whereas requirment is to first show the first error message 'Please Create a task for airtel account contact Research before creating any other Task' and then the second error message
User-added image

'Please Create a task for 2nd field before creating any other Task'

Any idea what is going wrong in the apex class ?

Your reply with  resolution is appreciated



Best Answer chosen by fiona gentry
Lukesh KarmoreLukesh Karmore
Hi fiona gentry, 
I got your point.
you used   below two boolean variables,
Boolean validationFlag = true;
Boolean validationFlag2 = true;

when you create first record  validationFlag the\is variable become false but  validationFlag2 this remail true  thats the reason.

for(Task t1: mapCaseWithTaskList.get(t.WhatId).tasks){
if(t1.Action__c == 'airtel account contact Research') {
validationFlag = false;
validationFlag2 = false;

replace  above code  with your  code in ERTT_TaskCreationValidation class.
and let me know .
If it helps please mark the best answer it will helps other.
Thanks ,


I'm having difficulty in writing a test class for my apex class that creates a new lead record based on whether an existing lead has met the conditions.  So if a lead has Web to Lead = True and H M!=null and WTL G!=null, it will take the values from that record and create a new one.  Please see below class:


public class CreateNewLead {
   public static void createnewrecords(List<Lead> leadMap){
        List<Lead> insertList = new List<Lead>();

        for (Lead rec:LeadMap){
               if ((rec.WTL_G__c != null) && (rec.H_M__c != null) && (rec.Web_to_Lead__c==TRUE))
                      // Creating new record
                       Lead newlist = new Lead();      
                       newlist.WTL_M__c = rec.H_M__c;                  
                       newlist.FirstName = rec.FirstName;
                       newlist.LastName = rec.LastName;
        insert  insertList;  



private class CreateNewLead_Test {
        static  testMethod void UPDATEMPRNGRPN_Test_TestMethod1(){
                       List<Lead> leadMap = new List<Lead>();
                       List<Lead> insertList = new List<Lead>();
                       Lead Ld = new Lead(FirstName='Name2', Web_To_Lead__c=TRUE, H_M__c='12345678901', WTL_G__c='1234567');
                       insert Ld;

I'm getting 0% coverage on this class however I'm not sure on what I'm doing wrong.  Can someone please help?

Best Answer chosen by RadDude89
Antoninus AugustusAntoninus Augustus
If you're getting 0% coverage for this class, then most likely the class is not even being called. I recommend you check the debug logs. Your insert DML operation in the test class might be failing, just a hunch.
Best Answer chosen by Raja Gopal Mondal
AnkaiahAnkaiah (Salesforce Developers) 
Hi Raja Gopal,

Please note that Questions about how to pass Trailhead challenges are not on topic, because these challenges are intended to be independent demonstrations of your abilities.

Trailhead Help (https://trailhead.salesforce.com/en/help?support=home) can provide assistance for situations where Trailhead does not appear to be functioning correctly. You can reach out to them if this is the case.
Please close the thread by selected as Best Answer so that we can keep our community clean