You need to sign in to do that
Don't have an account?
Mahesh Pradhan
Facing Error When Writing Test Class of following Trigger:required urgently
trigger Trigger_Task_Send_Email on Task (after insert)
{
Set<Id> ownerIds = new Set<Id>();
for(Task ta: Trigger.New)
ownerIds.add(ta.ownerId);
// --------------------Build a map of all users who are assigned the tasks.-----------------------------------
Map<Id, User> userMap = new Map<Id,User>([select Name, Email from User where Id in :ownerIds ]);
for(Task ta : Trigger.New)
{
User theUser = userMap.get(ta.ownerId);
//-------------------------Mail Messenging-------------------------------------------------------
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {theUser.Email};
if(ta.Email_Bcc__c!=null)
{
string[] BccAddresses =String.valueOf(ta.Email_Bcc__c).Split(',');
mail.setBccAddresses(BccAddresses);
}
if(ta.Email_To__c!=null)
{
string[] toaddress =String.valueOf(ta.Email_To__c).Split(',');
mail.setToAddresses(toaddress);
}
if(ta.Email_CC__c!=null)
{
string[] ccAddresses =String.valueOf(ta.Email_CC__c).Split(',');
mail.setCcAddresses(ccAddresses);
mail.setSubject('New Task Created by '+ Userinfo.getName() +'');
}
//---------------------------------------- Creating Template----------------------------------
String template = 'New Task \n\nTo: '+theUser.Name+',\n\n '+ Userinfo.getName() +' has assigned you the following task: \n\n';
template+= 'Subject - {1}\n';
template+= 'Due Date - {2}\n';
template+= 'Priority - {3}\n';
String duedate = '';
if (ta.ActivityDate==null)
duedate = '';
else
duedate = ta.ActivityDate.format();
if(ta.Description!=null)
{
template+='Description - '+ta.Description+'\n';
}
List<String> args = new List<String>();
args.add(theUser.Name);
args.add(ta.Subject);
args.add(duedate);
args.add(ta.Priority);
// -----------------------For Creating Task Link--------------------------
template+= '\n\nFor more details, click the following link: \n\n';
template+= URL.getSalesforceBaseUrl().toExternalForm() + '/' + ta.Id;
mail.setSaveAsActivity(false);
mail.setPlainTextBody(template);
// Here's the String.format() call.
String formattedHtml = String.format(template, args);
mail.setPlainTextBody(formattedHtml);
Messaging.SendEmail(new Messaging.SingleEmailMessage[] {mail});
}
}
Test class
---------------
@isTest
public class Test_Trigger_Task_Send_Email{
static testmethod void createTask(){
task t = new task();
t.priority = 'low';
t.status = 'In Progress';
//t.ActivityDate='Due date';
t.description = 'testing description';
t.Email_To__c = 'a@b.com';
t.Email_Bcc__c = 'a@b.com';
t.Email_CC__c = 'a@b.com';
test.startTest();
insert t;
test.stopTest();
}
}
when i am deploy to server its throwing error like Invalid field Email_Bcc__c for SObject Task
i tried but i can't able to find out the error.how to solve it
{
Set<Id> ownerIds = new Set<Id>();
for(Task ta: Trigger.New)
ownerIds.add(ta.ownerId);
// --------------------Build a map of all users who are assigned the tasks.-----------------------------------
Map<Id, User> userMap = new Map<Id,User>([select Name, Email from User where Id in :ownerIds ]);
for(Task ta : Trigger.New)
{
User theUser = userMap.get(ta.ownerId);
//-------------------------Mail Messenging-------------------------------------------------------
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {theUser.Email};
if(ta.Email_Bcc__c!=null)
{
string[] BccAddresses =String.valueOf(ta.Email_Bcc__c).Split(',');
mail.setBccAddresses(BccAddresses);
}
if(ta.Email_To__c!=null)
{
string[] toaddress =String.valueOf(ta.Email_To__c).Split(',');
mail.setToAddresses(toaddress);
}
if(ta.Email_CC__c!=null)
{
string[] ccAddresses =String.valueOf(ta.Email_CC__c).Split(',');
mail.setCcAddresses(ccAddresses);
mail.setSubject('New Task Created by '+ Userinfo.getName() +'');
}
//---------------------------------------- Creating Template----------------------------------
String template = 'New Task \n\nTo: '+theUser.Name+',\n\n '+ Userinfo.getName() +' has assigned you the following task: \n\n';
template+= 'Subject - {1}\n';
template+= 'Due Date - {2}\n';
template+= 'Priority - {3}\n';
String duedate = '';
if (ta.ActivityDate==null)
duedate = '';
else
duedate = ta.ActivityDate.format();
if(ta.Description!=null)
{
template+='Description - '+ta.Description+'\n';
}
List<String> args = new List<String>();
args.add(theUser.Name);
args.add(ta.Subject);
args.add(duedate);
args.add(ta.Priority);
// -----------------------For Creating Task Link--------------------------
template+= '\n\nFor more details, click the following link: \n\n';
template+= URL.getSalesforceBaseUrl().toExternalForm() + '/' + ta.Id;
mail.setSaveAsActivity(false);
mail.setPlainTextBody(template);
// Here's the String.format() call.
String formattedHtml = String.format(template, args);
mail.setPlainTextBody(formattedHtml);
Messaging.SendEmail(new Messaging.SingleEmailMessage[] {mail});
}
}
Test class
---------------
@isTest
public class Test_Trigger_Task_Send_Email{
static testmethod void createTask(){
task t = new task();
t.priority = 'low';
t.status = 'In Progress';
//t.ActivityDate='Due date';
t.description = 'testing description';
t.Email_To__c = 'a@b.com';
t.Email_Bcc__c = 'a@b.com';
t.Email_CC__c = 'a@b.com';
test.startTest();
insert t;
test.stopTest();
}
}
when i am deploy to server its throwing error like Invalid field Email_Bcc__c for SObject Task
i tried but i can't able to find out the error.how to solve it
Does the errors occurs when you run test or just during deployment ?
If it is just during deployment, maybe, if you use change sets, you must deploy all the fields used in your class / trigger
Hope this helps
Gil
Question Solved ? Please mark as the best answer to help other users !