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
MadhulendraMadhulendra 

How to write test Method for delete statement

hi can anybody help me to write Test Method for given below code ,

 

 

 

public PageReference deleteRecord()

{ String customerId=null;

try { customerId=ApexPages.currentPage().getParameters().get('customerId');

if(customerId!=null)

{

List demo = new List(); demo = [Select id,Customer_Id__c From CustomerDemographicsDetail__c where Customer_Id__c=:customerId]; delete demo; List insuredParty = new List(); insuredParty = [Select id,Customer_Id__c From CustomerFinancialDetail__c where Customer_Id__c=:customerId]; delete insuredParty; List survivor = new List(); survivor = [Select id,Customer_Id__c From Customer_Financial_Detail1__c where Customer_Id__c=:customerId]; delete survivor; List retirement = new List(); retirement = [Select Customer_Id__c From Customer_Retirement_Detail__c where Customer_Id__c=:customerId]; delete retirement; List education = new List(); education = [Select Customer_Id__c From Customer_Educational_Goal__c where Customer_Id__c=:customerId]; delete education; List survivorNeeds = new List(); survivorNeeds = [Select Customer_Id__c From Customer_Survivor_Needs__c where Customer_Id__c=:customerId]; delete survivorNeeds; List survivorIncomeProv = new List(); survivorIncomeProv = [Select Customer_Id__c From Survivor_Income_Provision__c where Customer_Id__c=:customerId]; delete survivorIncomeProv; } } catch(Exception e) { ApexPages.addMessages(e); } return null; }

 

Thanks in advance

 

Madhulendra

dmchengdmcheng
To make your code readable in a posting, you need to click the Insert Code icon on the toolbar ribbon when you're writing a message.  This will pop-up a window where you can paste your code and then it will be inserted into your message in a readable format.
deepzdeepz

Ive written for the first few lines of the code. Repeat the same for the rest. Hope it fits your requirement

 

static testMethod void testdeleteRecord() { Customer__c custId = [select Id from Customer__c LIMIT 1]; CustomerDemographicsDetail__c cust1 = new(CustomerDemographicsDetail__c(Customer_Id__c = custId.Id); try{insert cust1;} catch(Exception e){} CustomerFinancialDetail__c insure1 = new(CustomerFinancialDetail__c(Customer_Id__c = custId.Id); try{insert insure1;} catch(Exception e){} Customer_Retirement_Detail__c retire1 = new(Customer_Retirement_Detail__c(Customer_Id__c = custId.Id); try{insert retirek1;} catch(Exception e){} Test.startTest(); delete cust1; delete insure1; delete retire1; Test.stopTest(); }

 

MadhulendraMadhulendra

hi

 

when I tried to write code it's give an error "Missing id at index : 0"

when I click on error then controller goes to delete cust1;

 

what I should do next.....

 

 

Thanks

deepzdeepz
Could you please paste the test code your executing
MadhulendraMadhulendra

 this is my test method that getting error while I run Test

 

static testMethod void testdeleteRecord()

{

CustomerContactDetail__c custId = [Select c.Id From CustomerContactDetail__c c LIMIT 1];

CustomerDemographicsDetail__c cust1 = new CustomerDemographicsDetail__c(Customer_Id__c = custId.Id);

try{insert cust1;} catch(Exception e){}

CustomerFinancialDetail__c insure1 =new CustomerFinancialDetail__c(Customer_Id__c = custId.Id);

try{insert insure1;} catch(Exception e){}

Customer_Retirement_Detail__c retire1 = new Customer_Retirement_Detail__c(Customer_Id__c = custId.Id);

try{insert retire1;} catch(Exception e){}

Test.startTest();

delete cust1;

delete insure1;

delete retire1;

Test.stopTest();

}