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
ShravanKumarBagamShravanKumarBagam 

how can i write test method delete event?

  I write a test method for below class code coverage is 60% can any body tell how can i write a testmethod for the delete event...


Class TestMethod

@isTest
 
 private class testparamcls{
 
 
     static testmethod void method(){
 
    paramcls obj=new paramcls();
      obj.delrec();
     
      }
      
          
 
 }



Class

  public with sharing class paramcls {

    public PageReference delrec() {
        System.debug('----------------RecordId---------------'+recordId);
        Account acc = [Select id from account where id=:recordId];
        delete acc;
        
        pagereference ref = new pagereference('/apex/param');
        ref.setredirect(true);
        return ref;
    }

    public string recordId{get; set;}
    public List<Account> lst {get; set;}
    
    public paramcls(){
    
        lst = [Select id, name, phone from Account];
    }

}

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

Try the below code snippet as reference:

 

@isTest

 

 private class testparamcls{

 

 

     static testmethod void method(){

 

    paramcls obj=new paramcls();

    Account acc=new Account();

    acc.name='test';

    insert acc;

    obj.recordId=acc.id;

      obj.delrec();

    

      }

     

          

 }

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

Try the below code snippet as reference:

 

@isTest

 

 private class testparamcls{

 

 

     static testmethod void method(){

 

    paramcls obj=new paramcls();

    Account acc=new Account();

    acc.name='test';

    insert acc;

    obj.recordId=acc.id;

      obj.delrec();

    

      }

     

          

 }

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

This was selected as the best answer
ShravanKumarBagamShravanKumarBagam

Hi Navatar,

 

  Thank you .Please let me know for where can get the best practices for the apex code development &Visualforce