function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
BegginerBegginer 

Help me with writing the test class

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();

}
BegginerBegginer
Error while running the test :
Error MessageSystem.QueryException: List has more than 1 row for assignment to SObject
Stack TraceClass.batchcs1test.TestOppurtunity: line 5, column 1
George ArshakyanGeorge Arshakyan
Hello Begginer!

The issue you've posted is all about this statement:
RecordType rec = [select id,name from RecordType where name = 'Type1'];
RecordType rec = [select id,name from RecordType where name = 'Type2'];
a) I don't know why do you need to select record type and redefine variable rec
b) Even if you do you should do something like this (still can throw an error with index out of bounds):
 
RecordType typeOneRecordType = [select id,name from RecordType where name = 'Type1' LIMIT 1][0];
RecordType typeTwoRecordType = [select id,name from RecordType where name = 'Type2' LIMIT 1][0];


Also, please notice in you scheduled job there's an logic based on Tasks, and you have to insert some tasks to have 100% coverage
 

Thanks
George