[SalesForce] How to override the delete button on VF page to return to parent

I have a VF page displaying a custom object with it's child objects in repeating apex:detail sections. Each section displays the child with the standard edit, delete,clone buttons that are there like when you open the child object by itself.
enter image description here

Everything works fine, except when clicking delete (to delete one of the child objects), which does delete the child but doesn't return to the parent VF page.

I tried creating a VF + controller to override the std delete button on the child, but the parameter it passes is the parent's one, and I get this error:

Id value a02O000000DdrPj is not valid for the CallCycleActivity__c standard controller

The id is for the parent, and CallCycleActivity__c is the child which is being deleted.

I need to delete the child and to return to the parent. How do I get it do this?

<apex:page standardController="CallCycleActivity__c" extensions="CCADeleteOverrideExt" action="{!customDelete}">

public with sharing class CCADeleteOverrideExt {

private CallCycleActivity__c cca;    
public PageReference pageRef;
public Map<string,string> URLParameters = ApexPages.currentPage().getParameters();

public CCADeleteOverrideExt(ApexPages.StandardController stdController) {

    this.cca = (CallCycleActivity__c)stdController.getRecord();

    //The return URL is passed via the custom button - get this to know where to return after a Save or Cancel
    if(URLParameters.containsKey('retURL')){
        pageRef = new PageReference(URLParameters.get('retURL'));
    }

}

public PageReference customDelete(){
    delete cca;

    // Return whatever PageReference you want here
    pageRef.setRedirect(true);
    return pageRef;       
 }

}

Best Answer

Just set the redirect to URL of the parent via parent's record id, i.e.

public PageReference customDelete(){
  PageReference ret = new PageReference('/' + cca.parent__c); //replace parent__c with appropriate relationship field
  delete cca;
  ret.setRedirect(true);
  return ret;       
}
Related Topic