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
developerTeamdeveloperTeam 

Save Button's Method in Child Page which when clicked, Saves n redirects to Parent Object's detail.

Hi,

Am new to salesforce, I had customized my org with my requirements, But am facing some problem:

 

I have a Custom JunctionObject__c  which has 2 fileds

1. CustomField1__c master-detail relation to CustomObject1__c and Similarly  

2. CustomField2__c master-detail relation to CustomObject2__c. 

 

I had Customized the new edit page to VF1 n used StandardController=”JunctionObject__c ” Extension="testController"

And overriden the "New" button of JunctionObject__c  to VF1.

 

So when we go from CustomObject1__c (PARENT) record we can see the related block JunctionObject__c , NOW when I click the "NEW" button the VF! appears Good. and I insert the data and click Save Button am going to detail page of JunctionObject__c,

But I need to be redirected to the CustomObject1__c detail page of that id. 

Here's below is the VF1 page :

<apex:page standardController="JunctionObject__c" extensions="TestController">
<apex:form id="myForm">  
 <apex:pageBlock title="JunctionObject Edit" mode="edit">
   <Apex:pageBlockButtons >
    <apex:commandButton action="{!save}" value="Save"/>   
    <apex:commandButton action="{!cancel}" value="Cancel"/>   
   </Apex:pageBlockButtons>
   <apex:pageBlockSection title="JunctionObject Information" columns="1">
     <apex:inputField  value="{!JunctionObject__c.CustomObject1__c}" />  // this helps me in pre-populating the value from Parent. 
    <apex:inputField value="{!JunctionObject__c.CustomObject2__c}" /> 
   </apex:pageBlockSection> 
  </apex:pageBlock>
 </apex:form>
</apex:page>

and Controller i.e. Extension is:

public class testController {

    public JunctionObject__c record;
    ApexPages.StandardController con;

    public testController (ApexPages.StandardController controller) {
    this.con = Controller; 
    system.debug( con.getRecord() );
        try { 
        this.record = [select id,Name,CustomField1__c,CustomField2__c from JunctionObject__c  where id = :con.getRecord().id limit 1];
        } 
        catch( Exception ee) { 
            this.record = new JunctionObject__c (); 
        }
    }
}

I didn't write any code for save Method, Please suggest me the code for Save Method which saves the object and redirects it to the parent CustomObject1__c.

 

Please help me in Save Method i.e. Code which saves the object and redirects it to the parent CustomObject1__c.

i.e. a Save button when clicked saves the inputvalues to the JunctionObject and Redirectes straight away to the previous CustomObject1 from where it came.

 

-ThankYou.

Best Answer chosen by Admin (Salesforce Developers) 
Arun MKArun MK

Hi,

 

Try this code to redirect the page to CustomObject1__c always.

 

   // Controller

public class testController {

 

    public JunctionObject__c record;    

 

    public testController (ApexPages.StandardController controller) {

        record = (JunctionObject__c)controller.getRecord();

    }

    

    public PageReference saveRecord(){

        try{

            insert record;

        }

        catch(exception e){

            // handle the exception

        }

        return new PageReference('/'+record.CustomField1__c);

    }

}

 

// Visual Force Page

 

 <apex:commandButton action="{!saveRecord}" value="Save"/>  

 

Let me know, if this should rediercet to CustomObject2__c when you try to create JunctionObject__c from CustomObject2__c page. Bcoz, that needs a bit of code modification.

 

Regards,

Arun.