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
MY VF and ApexMY VF and Apex 

After deleting child record page should redirect to master record...

hi friends,

i have created two object named as vendor and vendor contact(master-detail relationship)...

i have used visualforce page (to create,to edit,detail page etc) for both vendor as well as vendor contact too...

whenever i delete any vendorcontact record from its detail page,after deletion it goes to list view of vendor contact... here i want that it should get back to respected master record.... for this i have created class to override delete button but getting error like this..

 


System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Vendor_Contact__c.Vendor__c
Class.DeleteVendorContactExtension.CheckDelete: line 19, column 14 External entry point 

 

 

here is my code

 

vf page

 

<apex:page standardController="Vendor_Contact__c" extensions="DeleteVendorContactExtension" action="{!CheckDelete}">
 
</apex:page>

 

extension

 

 

public class DeleteVendorContactExtension
{
    private Id JobId;
    private final Vendor_Contact__c objVC;
    public DeleteVendorContactExtension(ApexPages.StandardController controller)
    {
      this.objVC=(Vendor_Contact__c)controller.getrecord();   
    }
   
     public PageReference CheckDelete()
     {
   
     JobId = objVC.Vendor__c;
    
        try
        {
     //   String strid;
      //  strid = objVC.Vendor__c; 
     
       Database.delete(objVC);   
        }
        catch(System.DMLException e)
        {
        return null;
        }
        PageReference p =  new Pagereference('/apex/VendorListView?id'+JobId);
       p.setRedirect(true);
      
       return p;  
     }

}



 

Thanks in advance

 

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

missed such small thing

 

try this

public DeleteVendorContactExtension(ApexPages.StandardController controller)
    {
      
      this.objVC = [Select Vendor__c from  Vendor_Contact__c where id =: controller.getrecord().id;  }

 OR

 

public DeleteVendorContactExtension(ApexPages.StandardController controller)
    {
      Vendor_Contact__c objVCTemp=(Vendor_Contact__c)controller.getrecord();  
      this.objVC = [Select Vendor__c from  Vendor_Contact__c where id =: objVCTemp.id;  }

 

All Answers

Shashikant SharmaShashikant Sharma

Change constructor to this

 

public DeleteVendorContactExtension(ApexPages.StandardController controller)
    {
      this.objVC=(Vendor_Contact__c)controller.getrecord();  
      this.objVC = [Select Vendor__c from  Vendor_Contact__c where id =: this.objVC.id;  }

 

MY VF and ApexMY VF and Apex

Thanks Shashi, but now i am getting error like this

 

System.FinalException: Final variable has already been initialized
Class.DeleteVendorContactExtension: line 4, column 37 External entry point 

Shashikant SharmaShashikant Sharma

Ohh I missed that its declared as final

 

try this

public DeleteVendorContactExtension(ApexPages.StandardController controller)
    {
      
      this.objVC = [Select Vendor__c from  Vendor_Contact__c where id =: controller.getrecord();  }

 

MY VF and ApexMY VF and Apex
 Error: Compile Error: Invalid bind expression type of SObject for column of type Id at line 9 column 72

 This is the error i am getting right now

Shashikant SharmaShashikant Sharma

missed such small thing

 

try this

public DeleteVendorContactExtension(ApexPages.StandardController controller)
    {
      
      this.objVC = [Select Vendor__c from  Vendor_Contact__c where id =: controller.getrecord().id;  }

 OR

 

public DeleteVendorContactExtension(ApexPages.StandardController controller)
    {
      Vendor_Contact__c objVCTemp=(Vendor_Contact__c)controller.getrecord();  
      this.objVC = [Select Vendor__c from  Vendor_Contact__c where id =: objVCTemp.id;  }

 

This was selected as the best answer
MY VF and ApexMY VF and Apex

Thank you Shashi!!!