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
Aurora Ganguly 10Aurora Ganguly 10 

test class for DeleteWithCheckboxController

Hi All,
Can anyone help me out in writing this test class for this apex class.
public with sharing class DeleteWithCheckboxController {
 
 @AuraEnabled
 public static list < contact > fetchContact() {
  list < contact > returnConList = new List < contact > ();
 
  List < contact > lstCon = [SELECT Name, firstName, LastName, Department, MobilePhone From contact LIMIT 50];
  // play for loop on lstCon and add each contact to returnConList List.
  for (contact c: lstCon) {
   returnConList.add(c);
  }
 // return the List of contacts
  return returnConList;
 }
 
 
 @AuraEnabled
 public static List < String > deleteRecords(List < String > lstRecordId) {
  // for store Error Messages  
  List < String > oErrorMsg = new List < String > ();
  // Query Records for delete where id in lstRecordId [which is pass from client side controller] 
  List < contact > lstDeleteRec = [select Id from contact where id IN: lstRecordId];
  
  // delte contact list with Database.DeleteResult[] database class.
  // It deletes some queried contacts using <samp class="codeph apex_code">Database.<span class="statement">delete</span></samp> 
  // with a false second parameter to allow partial processing of records on failure.
  // Next, it iterates through the results to determine whether the operation was successful or not
  // for each record. and check if delete contact successful so print msg on debug, 
  // else add error message to oErrorMsg List and return the list  
  Database.DeleteResult[] DR_Dels = Database.delete(lstDeleteRec, false);
  // Iterate through each returned result
  for (Database.DeleteResult dr: DR_Dels) {
   if (dr.isSuccess()) {
      system.debug('successful delete contact');
     // Operation was successful
   } else {
    // Operation failed, so get all errors   
    oErrorMsg.add('');
    for (Database.Error err: dr.getErrors()) {
     // add Error message to oErrorMsg list and return the list
     oErrorMsg.add(err.getStatusCode() + ': ' + err.getMessage());
    }
   }
  }
  return oErrorMsg;
 
 }
}
Looking for your help.
Aurora.
 
Suraj TripathiSuraj Tripathi

Hi Aurora,
Please try this piece of code. hope it will help you.
 

@isTest
public class DeleteWithCheckboxControllerTest 
{
    @isTest static void TestDeleteWithCheckboxControllerTest_1()
    {
        List<contact> lstCon = new List<contact>();
        for(Integer i=0; i<=50; i++)
        {
            contact c = new contact();
            c.FirstName = 'FCon'+i;
            c.LastName = 'LCon'+i;
            c.MobilePhone = '9897996867';
            lstCon.add(c);
        }
        insert lstCon;
        List <String> idStrList = new List <String>();
        for(Contact con : lstCon){
            idStrList.add(con.Id);
        }
        DeleteWithCheckboxController.fetchContact();
        DeleteWithCheckboxController.deleteRecords(idStrList);
     }
}

If this code helps you, Please mark it as best to close this Question.

Regards,
Suraj​
Aurora Ganguly 10Aurora Ganguly 10
Thanks Suraj 

I will check n let u know ..
Thanks for the help
Alo SenAlo Sen
Is there a way to force a test for the else logic?