• George Arshakyan
  • NEWBIE
  • 0 Points
  • Member since 2015
  • Sr. Salesforce Developer
  • CoreValue Services, LLC


  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
Hi all,

Im new to salesforce and currently trying to understand test classes. could anyone help me with the test class for below code.
I wrote a bit of  test code and unable to complete the code coverage.

Thanks in Advance.

global class batchcs1 implements  Schedulable {

global void execute(SchedulableContext sc) {
  List<Task> tskLst = [select id,status,ActivityDate,whatid,what.name from Task where what.type='Opportunity' and  What.recordType.name in ('type1', 'type2') and subject like 'test Value%' and status != 'Lost' and ActivityDate >: Date.today().addDays(30)];   
  if(tskLst != null && !tskLst.isEmpty())
  {
    List<batchs__c>  emailLst = [select id,Emails__c from batchs__c];
           List<string> toAddressLst = new List<string>();
            if(emailLst != null && emailLst.size() >0)
             toAddressLst = emailLst[0].Emails__c.split(',');
             
  DateTime d = Date.Today()+1;
  String dateStr =  d.format('MM/dd/yyyy') ;  
   
  List<Messaging.SingleEmailMessage> msgLst = new List<Messaging.SingleEmailMessage>();
  Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
  semail.setToAddresses(toAddressLst);
  semail.setSubject(' Reminder Test ');
  String htmlBody = '<html><body> Date :'+dateStr+' <br/> <p>This is a reminder :</p> <br/> tblStr <br/><p></p><br/></body></html>';
       
  String table =  '<table border="1" width="60%"><tr style="background-color:#4BACC6;color:white;"><td width="20%" align="center">Opportunity Name</td><td width="14%" align="center">Due Date</td></tr>';
  for(Task tsk : tskLst)
 {
   DateTime dt1 = tsk.ActivityDate+1;   
  table += '<tr style="background-color: #D8D8D8">';  
  table += '<td align="left"><a href="'+URL.getSalesforceBaseUrl().toExternalForm() + '/' +tsk.whatid+'">'+tsk.what.name+'</a></td>';
  table += '<td align="left">' + dt1.format('MM/dd/yyyy') + '</td>';
  table += '</tr>';
 }
  table  = table + '</table>';
   htmlBody = htmlBody.replace('tblStr', table);
   semail.setHtmlBody(htmlBody);
   msgLst.add(semail);
   if(msgLst != null && !msgLst.isEmpty())
    Messaging.sendEmail(msgLst);
      
     }    
    }
 
}

--------------------------------------------------------------------------------------

@isTest
private class batchcs1test
{
    public static testmethod void TestOppurtunity()
     {
             RecordType rec = [select id,name from RecordType where name = 'Type1'];
             RecordType rec = [select id,name from RecordType where name = 'Type2'];
  Opportunity opty = new Opportunity(account='test',name='test oppty', SalesStage__c='Closed',status__c = 'Expired',closedate=System.Today());
            insert opty;

     test.startTest();
      
        batchcs1 mar = batchcs1 new ();
         String sch = '0 0 13 30 11 ?';
         system.schedule('Scheduled Job', sch, mar);
     
         test.stopTest();

}
hi everyone,  i have just completed Recruiting Application free developer edition now i wanted to check it into sandbox. so what i have to do???