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
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student 

Deletion Extension Test Code

I am attempting to write test code for a deletion extension....The extension is for a delete button inside a pageblock table for an accounts tabbed VF page. I am sure that I have overcomplicated the test as I am trying to refer to everything. Please help, look at this mess of code:

 

@isTest
private class extAccountDel {

static testMethod void extAccountDel(){
        //Attempting to change an old test to suit my new extension

// create a test account that will be use to reference the Fact Finder Record

Account acc = new Account(Name = 'Test Account');
insert acc;

//Now lets create Fact Finder, Destiny Service, Destiny Product, transaction and compliance note record that will be reference for the Standard Account
        Fact_Finder__c facTest = new Fact_Finder__c(Account__c = acc.id);
         Service__c SerTest = new Service__c(Account__c = acc.id);
         Destiny_Products__c ProTest = new Destiny_Products__c(Account__c = acc.id);
         Transaction__c TraTest = new Transaction__c(Account__c = acc.id);
        Notes__c ComTest = new Notes__c(Account__c = acc.id);
        
        
        //call the apepages stad controller
        Apexpages.Standardcontroller stdFac = new Apexpages.Standardcontroller(facTest);
         Apexpages.Standardcontroller stdSer = new Apexpages.Standardcontroller(SerTest);
          Apexpages.Standardcontroller stdPro = new Apexpages.Standardcontroller(ProTest);
           Apexpages.Standardcontroller stdTra = new Apexpages.Standardcontroller(TraTest);
            Apexpages.Standardcontroller stdCom = new Apexpages.Standardcontroller(ComTest);

//now call the class and reference the standardcontroller in the class.
        extAccountDel extFac = new extAccountDel(stdFac);
        extFac.deleteFacRecord();
        
        
        extAccountDel extSer = new extAccountDel(stdSer);
         extSer.deleteSerRecord();
        
        extAccountDel extPro = new extAccountDel(stdPro);
        extPro.deleteProRecord();
        
        extAccountDel extTra = new extAccountDel(stdTra);
        extTra.deleteTraRecord();
        
        extAccountDel extCom = new extAccountDel(stdCom);
extCom.deleteComRecord();
//call the pageReference in the class.
        
       
        
        
        
    }

}

 This is the deletion extension:

 

public class extAccountDel {

    //string delId is used to set the parameter
    public string delId {get;set;}
    public Account acc {get;set;}

    public extAccountDel (ApexPages.StandardController controller) {
        this.acc = (Account)controller.getRecord();
    }
    
    //delete for Fact Finder record and then re-directs back to the VF page with the Fact Finder tab open
    public PageReference deleteFacRecord() {
    
        Fact_Finder__c Fac = [select id from Fact_Finder__c where id =: delId];
        delete Fac;
        
        
        
        PageReference pageRef= new PageReference('/apex/DestinyAccountTest?id='+acc.id+'&Sfdc.override=1');
         pageRef.getParameters().put('tab','FactFinder');
        pageRef.setredirect(true);

       
        return pageRef;
    }
    
    
 //delete for Destiny Products record and then re-directs back to the VF page with the Destiny Products tab open
    public PageReference deleteProRecord() {
    
        Destiny_Products__c Pro = [select id from Destiny_Products__c where id =: delId];
        delete Pro;
        
        
        
        PageReference pageRef= new PageReference('/apex/DestinyAccountTest?id='+acc.id+'&Sfdc.override=1');
         pageRef.getParameters().put('tab','Destiny Products');
        pageRef.setredirect(true);
        
         return pageRef;
}

 //delete for Destiny Services record and then re-directs back to the VF page with the Destiny Services tab open
    public PageReference deleteSerRecord() {
    
        Service__c Ser = [select id from Service__c where id =: delId];
        delete Ser;
        
        
        
        PageReference pageRef= new PageReference('/apex/DestinyAccountTest?id='+acc.id+'&Sfdc.override=1');
         pageRef.getParameters().put('tab','Destiny Services');
        pageRef.setredirect(true);
        
         return pageRef;
}

 //delete for Transactions record and then re-directs back to the VF page with the Transactions tab open
    public PageReference deleteTraRecord() {
    
        Transaction__c Tra = [select id from Transaction__c where id =: delId];
        delete Tra;
        
        
        
        PageReference pageRef= new PageReference('/apex/DestinyAccountTest?id='+acc.id+'&Sfdc.override=1');
         pageRef.getParameters().put('tab','Transactions');
        pageRef.setredirect(true);
        
         return pageRef;
}

 //delete for Compliance Notes record and then re-directs back to the VF page with the Compliance Notes tab open
    public PageReference deleteComRecord() {
    
        Notes__c Com = [select id from Notes__c where id =: delId];
        delete Com;
        
        
        
        PageReference pageRef= new PageReference('/apex/DestinyAccountTest?id='+acc.id+'&Sfdc.override=1');
         pageRef.getParameters().put('tab','Compliance Notes');
        pageRef.setredirect(true);
        
         return pageRef;
}

}
Best Answer chosen by Admin (Salesforce Developers) 
izayizay

Ok, after further review I see that the test is passinf the other objects as param to the class instead of the account istelf which is what the class is expecting. Try inserting the facTest, SerTest, TraTest, and ComTest... then changing the code in the test class to pass the account instead of the other objects... And set the delII to the test record Ids. It should look something like this:

 

 

@isTest
private class extAccountDelTest {

    static testMethod void extAccountDelTest(){
        // create a test account that will be use to reference the Fact Finder Record

        Account acc = new Account(Name = 'Test Account');
        insert acc;

        test.startTest();

        //Now lets create Fact Finder, Destiny Service, Destiny Product, transaction and compliance note record that will be reference for the Standard Account
        Fact_Finder__c facTest = new Fact_Finder__c(Account__c = acc.id);
        Service__c SerTest = new Service__c(Account__c = acc.id);
        Destiny_Products__c ProTest = new Destiny_Products__c(Account__c = acc.id);
        Transaction__c TraTest = new Transaction__c(Account__c = acc.id);
        Notes__c ComTest = new Notes__c(Account__c = acc.id);
       

        insert facTest;

        insert SerTest;

        insert ProTest;

        insert TraTest;

        insert ComTest;


        PageReference editFactFinder = Page.DestinyAccountTest;//Page reference  
        Test.setCurrentPage(editFactFinder);//Set the current page 

        //create the apexpages stad controller for account
        Apexpages.Standardcontroller s = new Apexpages.Standardcontroller(acc);

 

        //now call the class and reference the standardcontroller in the class.
        extAccountDel extAccDel  = new extAccountDel(s);

       

        extAccDel.delId = facTest.Id;
        extAccDel.deleteFacRecord();
       
        extAcctDel.delId = SerTest.Id;
        extAccDel.deleteSerRecord();
       

        extAcctDel.delId = ProTest.Id;
        extAccDel.deleteProRecord();
       

        extAcctDel.delId = TraTest.Id;
        extAccDel.deleteTraRecord();
       

        extAcctDel.delId = ComTest.Id;
        extAccDel.deleteComRecord();
        
        test.stopTest(); 

 

    }

 

}

All Answers

Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

I will not be offended if my test code is deemed completely useless....:)

izayizay

Hi,

 

You are close... The only things that I see missing are test.startTest(), test.stopTest() (Not required but good practice), and setting the vfpage as current page... See code. Hope this helps!

 

@isTest
private class extAccountDel {

static testMethod void extAccountDel(){
        //Attempting to change an old test to suit my new extension

// create a test account that will be use to reference the Fact Finder Record

Account acc = new Account(Name = 'Test Account');
insert acc;


    test.startTest();


//Now lets create Fact Finder, Destiny Service, Destiny Product, transaction and compliance note record that will be reference for the Standard Account
        Fact_Finder__c facTest = new Fact_Finder__c(Account__c = acc.id);
         Service__c SerTest = new Service__c(Account__c = acc.id);
         Destiny_Products__c ProTest = new Destiny_Products__c(Account__c = acc.id);
         Transaction__c TraTest = new Transaction__c(Account__c = acc.id);
        Notes__c ComTest = new Notes__c(Account__c = acc.id);
        
        PageReference editFactFinder = Page.<<YouVFPageName>>;//Page reference  
        Test.setCurrentPage(editFactFinder);//Set the current page 


        //call the apepages stad controller
        Apexpages.Standardcontroller stdFac = new Apexpages.Standardcontroller(facTest);
         Apexpages.Standardcontroller stdSer = new Apexpages.Standardcontroller(SerTest);
          Apexpages.Standardcontroller stdPro = new Apexpages.Standardcontroller(ProTest);
           Apexpages.Standardcontroller stdTra = new Apexpages.Standardcontroller(TraTest);
            Apexpages.Standardcontroller stdCom = new Apexpages.Standardcontroller(ComTest);

//now call the class and reference the standardcontroller in the class.
        extAccountDel extFac = new extAccountDel(stdFac);
        extFac.deleteFacRecord();
       
       
        extAccountDel extSer = new extAccountDel(stdSer);
         extSer.deleteSerRecord();
       
        extAccountDel extPro = new extAccountDel(stdPro);
        extPro.deleteProRecord();
       
        extAccountDel extTra = new extAccountDel(stdTra);
        extTra.deleteTraRecord();
       
        extAccountDel extCom = new extAccountDel(stdCom);
extCom.deleteComRecord();
//call the pageReference in the class.
        
    test.stopTest();   
        
        
    }

}

Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

Thanks for the reply. I tried the code and it failed with this error.

 

Error Message System.TypeException: Invalid conversion from runtime type SOBJECT:Fact_Finder__c to SOBJECT:Account Stack Trace Class.extAccountDel.<init>: line 8, column 1
Class.extAccountDelTest.extAccountDelTest: line 32, column 1

 

I changed the name of the test to extaccountdeltest. Did i change in the right places?

 

@isTest
private class extAccountDelTest {

static testMethod void extAccountDelTest(){
        //Attempting to change an old test to suit my new extension

// create a test account that will be use to reference the Fact Finder Record

Account acc = new Account(Name = 'Test Account');
insert acc;

    test.startTest();

//Now lets create Fact Finder, Destiny Service, Destiny Product, transaction and compliance note record that will be reference for the Standard Account
        Fact_Finder__c facTest = new Fact_Finder__c(Account__c = acc.id);
         Service__c SerTest = new Service__c(Account__c = acc.id);
         Destiny_Products__c ProTest = new Destiny_Products__c(Account__c = acc.id);
         Transaction__c TraTest = new Transaction__c(Account__c = acc.id);
        Notes__c ComTest = new Notes__c(Account__c = acc.id);
        
        PageReference editFactFinder = Page.DestinyAccountTest;//Page reference  
        Test.setCurrentPage(editFactFinder);//Set the current page 

        //call the apepages stad controller
        Apexpages.Standardcontroller stdFac = new Apexpages.Standardcontroller(facTest);
         Apexpages.Standardcontroller stdSer = new Apexpages.Standardcontroller(SerTest);
          Apexpages.Standardcontroller stdPro = new Apexpages.Standardcontroller(ProTest);
           Apexpages.Standardcontroller stdTra = new Apexpages.Standardcontroller(TraTest);
            Apexpages.Standardcontroller stdCom = new Apexpages.Standardcontroller(ComTest);

//now call the class and reference the standardcontroller in the class.
        extAccountDel extFac = new extAccountDel(stdFac);
        extFac.deleteFacRecord();
       
       
        extAccountDel extSer = new extAccountDel(stdSer);
         extSer.deleteSerRecord();
       
        extAccountDel extPro = new extAccountDel(stdPro);
        extPro.deleteProRecord();
       
        extAccountDel extTra = new extAccountDel(stdTra);
        extTra.deleteTraRecord();
       
        extAccountDel extCom = new extAccountDel(stdCom);
extCom.deleteComRecord();
//call the pageReference in the class.
        
    test.stopTest();   
        
        
    }

}

 

 

Cheers for your help mate. :)

izayizay

Based on the error message the problem is on line 8 of the extAccountDel class. So, the code in line 32 of this class is firing the code from the extAccountDel class and it is failing on line 8. Also the error says that there is a problem with the object type assignment.

 

 

The standard controller of the page is Fact_Finder__c but you are asigning the record to the Account.

 

In the test class you weren't passing the Account standard controller.

 

public extAccountDel (ApexPages.StandardController controller) {
        this.acc = (Account)controller.getRecord();
 }

izayizay

Ok, after further review I see that the test is passinf the other objects as param to the class instead of the account istelf which is what the class is expecting. Try inserting the facTest, SerTest, TraTest, and ComTest... then changing the code in the test class to pass the account instead of the other objects... And set the delII to the test record Ids. It should look something like this:

 

 

@isTest
private class extAccountDelTest {

    static testMethod void extAccountDelTest(){
        // create a test account that will be use to reference the Fact Finder Record

        Account acc = new Account(Name = 'Test Account');
        insert acc;

        test.startTest();

        //Now lets create Fact Finder, Destiny Service, Destiny Product, transaction and compliance note record that will be reference for the Standard Account
        Fact_Finder__c facTest = new Fact_Finder__c(Account__c = acc.id);
        Service__c SerTest = new Service__c(Account__c = acc.id);
        Destiny_Products__c ProTest = new Destiny_Products__c(Account__c = acc.id);
        Transaction__c TraTest = new Transaction__c(Account__c = acc.id);
        Notes__c ComTest = new Notes__c(Account__c = acc.id);
       

        insert facTest;

        insert SerTest;

        insert ProTest;

        insert TraTest;

        insert ComTest;


        PageReference editFactFinder = Page.DestinyAccountTest;//Page reference  
        Test.setCurrentPage(editFactFinder);//Set the current page 

        //create the apexpages stad controller for account
        Apexpages.Standardcontroller s = new Apexpages.Standardcontroller(acc);

 

        //now call the class and reference the standardcontroller in the class.
        extAccountDel extAccDel  = new extAccountDel(s);

       

        extAccDel.delId = facTest.Id;
        extAccDel.deleteFacRecord();
       
        extAcctDel.delId = SerTest.Id;
        extAccDel.deleteSerRecord();
       

        extAcctDel.delId = ProTest.Id;
        extAccDel.deleteProRecord();
       

        extAcctDel.delId = TraTest.Id;
        extAccDel.deleteTraRecord();
       

        extAcctDel.delId = ComTest.Id;
        extAccDel.deleteComRecord();
        
        test.stopTest(); 

 

    }

 

}

This was selected as the best answer
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

Hey,

 

Thank you so much for your help. I got it working using your code and it passed 100%. It just failed a few times because of required fields in the transactions object. Which were amount and data of payment, which I inserted. Thank you so much for your help.