You need to sign in to do that
Don't have an account?
How to write Test class for the following code....help me
Hi,
Please help me to write test class for the following code......
global class TaskRemainder implements Schedulable
{
global void execute(SchedulableContext ctx)
{
List<Task> a = [SELECT id,CreatedDate,LastModifiedDate,Status,Subject FROM Task where Status='Not Started' and CreatedDate <: System.now()-2];
String tmpmarkEmail='karanrajforu@gmail.com';
String[] markEmail=tmpmarkEmail.split(',');
for(Integer i=0;i<a.Size();i++)
{
Task t=[SELECT id,CreatedDate,LastModifiedDate,Status,OwnerId,WhoId,Subject,Priority FROM Task where Id =: a[i].Id];
Lead l=[Select Id,OwnerId,Email,Phone,Description,Title,LastName,FirstName,Company From Lead where Id =:t.WhoId];
User u=[Select FirstName,LastName from User where Id=:l.OwnerId];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(t.OwnerId);
mail.setSubject('Remainder for Task');
if(t.CreatedDate<System.now()-4)
{
mail.setCcAddresses(markEmail);
}
mail.setPlainTextBody('This is Remainder Mail\n\n\nThe Following Task is assigned For you\n\n\t Subject : '+t.Subject+'\n'+'\n'+'\t Priority : '+t.Priority
+'\n'+'\n'+'\t Contact Name : '+l.LastName+' '+l.FirstName+'\n'+'\n'+'\t Email : '+l.Email+'\n'+'\n'+'\t Phone : '+l.Phone+'\n'+'\n'+'\t Title : '+l.Title+'\n'+'\n'+'\t Describtion : '+l.Description
+ '\n\n\n');
mail.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
}
I dont have any idea to write test class for this code..please help me
This is a very good link where you can study how you can write a test class for schedule class.
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm
Just try and paste your test class here if you won't able to make it, will help you for sure.
Thanks
Ankit Arora
Blog | Facebook | Blog Page
All Answers
This is a very good link where you can study how you can write a test class for schedule class.
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm
Just try and paste your test class here if you won't able to make it, will help you for sure.
Thanks
Ankit Arora
Blog | Facebook | Blog Page
Thanks for your help....I wrote Test class for my code and run it in my sandbox test,it successfully ran,.,the code coverage says that 100%,..Now when i try to deploy it in the production environment,it gives me error as 'the code should cover atleast 75 %,your code is only 0 %....where is the problem?when i check in test environment it showing 100 % in code coverage.
Make sure only your main class and its test class is included in the change set and not all the irrelevant components.
Please make sure error is in the class which you are sending you can check the failure logs. If all these are correct then check if you have create some data dependent to your sandbox org like using hard coded ids of records, as those records may not exists on production.
Thanks
Ankit Arora
Blog | Facebook | Blog Page
we have to include our test class also in the change set?i dint include it in the change set?...i think it only my problem?...i will check it and post here.
Ohh yes you need to include your test class also in the change set.
Thanks
Ankit Arora
Blog | Facebook | Blog Page
my test class passed in sandbox,but it failed in production environment......am getting error in
Lead l=[Select Id,OwnerId,Email,Phone,Description,Title,LastName,FirstName,Company From Lead where Id =:t.WhoId];
which is in my apex class....help me for this error..
Please let me know the error.
Thanks
Ankit Arora
Blog | Facebook | Blog Page
This is my error message,.....the same code successfully ran in sadbox,while deploy in production environment am getting this error message....
The Line number 12 in my TaskRemainder class is
You must be using some data which is org dependent that is why your code is running successfully in sandbox and not in production... Make sure data you are using in your test class is created in test class before using it.. as data created in test class won't affect the actual database.
I used try catch in my apex class,it solved my issue...and successfully deployed in production environment.
That's good, but using try catch is not the best practice. But anyways in the end your problem is resolved :)
ya..u r correct , Am new to salesforce,just only 2 weeks,by now am learning how to write apex class and test method in better way.Hope will learn soon.
Indeed !!! Just go through all apex and visualforce documents provided by salesforce, this is not a big deal.
Best of luck.
Hi Karan Raj can u past ur code below tehn i can get understand how to write test class for scheduler
Thanks in Advance