+ Start a Discussion
A Raj 9A Raj 9 

TestClass for delete button code to delete all records of object

Hi All, plz help me with test class code for this below apex class for a delete button to delete all the records of a sub object(Project_Phase) to a parent object(Project_Phase_Plan)

below is the code
********************

public class PhaseDeleteAllLinesExt
{
    public Project_Phase_Plan__c pplan{get;set;}
    public String deleteWarning{get;set;}
    public Boolean deleteOK{get;set;}
    public Boolean showCancel{get;set;}
    public Boolean showClose{get;set;}
    public Set<String> deleteAllOK  = new Set<String> {'Draft', 'Pre-Release Review', 'Confirmation Pending', 'Confirmed'};
    public PageReference currentPage = ApexPages.currentPage();
    
    public PhaseDeleteAllLinesExt(ApexPages.StandardSetController stdSetController)
    {
        //
        // Get the Order Release Information
        //
        deleteOK = false;
        showCancel = true;
        showClose = false;
        Id id = ApexPages.CurrentPage().getParameters().get('id');
        if (id != null)
        {
            System.debug('OER ID = ' + String.valueOf(id));
            pplan = [SELECT Id, Name FROM Project_Phase_Plan__c WHERE Id = :id LIMIT 1];
        }
        if (pplan == null)
        {
            ApexPages.Message noOEError = new ApexPages.Message(ApexPages.Severity.ERROR,'** ERROR - Unable to retrieve the Project Phase Plan Information');
            ApexPages.addMessage(noOEError);
            showClose = true;
            showCancel = false;         
        }
        else
        {
            
                deleteWarning = 'WARNING: You have requested the deletion of all Project Phase for Project Phase Plan ' + pplan.Name  + '<br>'+
                'Click the Continue button to confirm deletion; click the Cancel button to return to the OProject Phase Plan Without deleting the Lines';
                deleteOK = true;
          
        }
    
    }
    
    public PageReference close()
    {
       return new PageReference('javascript:window.self.close()');
    }
    
    public PageReference deleteAllLines()
    {
        List<Database.DeleteResult> drList = new List<Database.DeleteResult>();
        Boolean hasErrors = false;
        List<Project_Phase__c> linesToDelete = [SELECT Id, Project_Phase_Plan__c FROM Project_Phase__c WHERE Project_Phase_Plan__c = :pplan.Id];
        
        if (linesToDelete.size() > 0)
        {
            ApexPages.Message databaseErrorMsg;
            drList = Database.delete(linesToDelete, true);
            Integer i = 0;      

            for(Database.DeleteResult dr : drList)
            {
                if (!dr.isSuccess())
                {
                    hasErrors = true;
                    for(Database.Error err : dr.getErrors())
                    {
                        databaseErrorMsg = new ApexPages.Message(ApexPages.Severity.ERROR, '*** An error occurred deleting Project Phase ID ' +
                            String.valueOf(linesToDelete[i].id) + '; ' + err.getMessage());
                        ApexPages.addMessage(databaseErrorMsg);
                    }
                }
                i++;
            }
        }
        if (hasErrors)
        {
            deleteWarning = 'Errors occurred while trying to delete the Project Phases; click the Cancel button to return to the Order Entry Release Page';
        }
        else
        {
            deleteWarning = String.valueOf(linesToDelete.size()) + ' Project Phase were successfully deleted.<br>' +
                'Close this window and then refresh the Project Phase Plan page to see the results';
        }
        deleteOK = false;
        showClose = true;
        showCancel = false;
        
        

        return currentPage;
 
        
    }
}

************************
 
Raj VakatiRaj Vakati
Try this
@isTest
public class PhaseDeleteAllLinesExtTest {
    
    static testMethod void MyTest()
    {        
        
		Project_Phase__c phase = new Project_Phase__c(); 
		phase.Name = 'Test' ;
		// add other fields 
		insert phase ; 
		
		
		Project_Phase_Plan__c  plan = new Project_Phase_Plan__c ();
		plan.Name ='Test' ;
		//add other fields 
		insert plan ; 
		
        PageReference pageRef = Page.YOURPAGENAME;
        pageRef.getparameters().put('Id', plan.id);  
        Test.setCurrentPage(pageRef);
        
        Apexpages.StandardController sc = new Apexpages.StandardController(plan);
        PhaseDeleteAllLinesExt ext = new PhaseDeleteAllLinesExt(sc);         
        ext.deleteAllLines();      
	   ext.close();  
    }
	
	 static testMethod void MyTest2()
    {        
        
		Project_Phase__c phase = new Project_Phase__c(); 
		phase.Name = 'Test' ;
		// add other fields 
		insert phase ; 
		
		
		Project_Phase_Plan__c  plan = new Project_Phase_Plan__c ();
		plan.Name ='Test' ;
		//add other fields 
		insert plan ; 
		
        PageReference pageRef = Page.YOURPAGENAME;
         Test.setCurrentPage(pageRef);
        
        Apexpages.StandardController sc = new Apexpages.StandardController(plan);
        PhaseDeleteAllLinesExt ext = new PhaseDeleteAllLinesExt(sc);         
        ext.deleteAllLines();      
	   ext.close();  
    }
}

 
A Raj 9A Raj 9
Thank You for the reply Raj. Plz let me know what need to be added in 'YOURPAGENAME'.
A Raj 9A Raj 9
Hi Raj, I gave VF Name for this YOURPAGENAME, but below is the error that I am getting at line 23 as "Constructor not defined: [PhaseDeleteAllLinesExt].<Constructor>(ApexPages.StandardController)". Plz help me out.
Raj VakatiRaj Vakati
from which visualforce page you are calling this controller ? Give that VF page Name here instead of YOURPAGENAME