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 we write a test method for delete event?

Hiii

stcforcestcforce

general form:

 

static testMethodvoid myUnitTest()

{

//possibly create object for deletion

//delete object here

//perform testing for expected resutls using system.assert()

}

Mani PenumarthiMani Penumarthi

hi,

inorder to write delete,update we want to get the id that you are passing in vfpage.

for example,

 

account aa =new account();

aa.name='xyz';

insert aa;

 

//to update the value

System.currentpagereference().getparameters().put('ur vf page id',aa.id);

aa.name='abc';

update aa;

 

same for delte also.

 

if you got the sollution just tick over the sollution so that it may be useful to others

ShravanKumarBagamShravanKumarBagam

 

will you please write a code for delete for below 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];
    }

}

Mani PenumarthiMani Penumarthi

hi,

in the test test class with out inserting a value how can u delete it?

 

first insert some value, later try to delete it by passing the record id.

 

it will work for sure

ShravanKumarBagamShravanKumarBagam

I'm created a dummy object in test method,where can i get  the record id.

 

Please tell how can i write a code for  this case. 

 

@isTest

private class testparamcls{


static testmethod void method(){

account a=new account();
a.name='test';
insert a;



paramcls obj=new paramcls();
obj.delrec();

}

}