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 Code which when clicked Saves the object and redirects to the parent Object

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  CustomField1__c master-detail relation to CustomObject1__c and Similarly  CustomField2__c master-detail relation to CustomObject2__c. 

 

I had Customized the new edit page to my own VF page i.e. VF1 n used StandardController=”JunctionObject__c ”. 

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:sectionHeader title="JunctionObject Edit"  subtitle="New JunctionObject" />
<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">  <br/>
  <apex:pageBlockSectionItem >
   <apex:outputLabel value="CusomField1"/>
     <apex:inputField  value="{!var.CustomObject1__c}" />
    </apex:pageBlockSectionItem> <br/> 
    <apex:pageBlockSectionItem > 
   <apex:outputLabel value="CusomField2"/> 
    <apex:inputField value="{!var.CustomObject2__c}" /> 
   </apex:pageBlockSectionItem> 
   </apex:pageBlockSectionItem> 
   </apex:pageBlockSection> 
  </apex:pageBlock>
 </apex:form>
</apex:page>

 And my TestController is as Follows:: 

 

public class testController {

public JunctionObject__c var {get;set;}
 
  public testController(ApexPages.StandardController controller) {
    var= new JunctionObject__c();
  }
 
}

 

I didn't write any code for save Method, Now Please suggest the code for Save Method 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.

Would be very helpful, if provided the code.

 

Thankyou.

 

Best Answer chosen by Admin (Salesforce Developers) 
amilawamilaw

PageReference viewtPage = new ApexPages.StandardController([SELECT Id FROM CustomObject1__c WHERE Id                                                                                                                                                                                              =:var.CustomObject1__c ]).view();
viewtPage.setRedirect(true);
return viewtPage;

All Answers

Avidev9Avidev9

Well the code should be very straight forward.

 

To save you will have to do a DML update/upsert/insert 

 

so

 

public class testController {

public JunctionObject__c var {get;set;}
 
  public testController(ApexPages.StandardController controller) {
    var= new JunctionObject__c();
  }

public Pagereference save(){
upsert var;
return new Pagereference('/'+var.ParentField__c);
//sorry didnt read your full example but the underlying logic is to send a pagerefernce of the detail page. And the link to the detail page of the parent record would be /<Parent_Record_Id>
} }
amilawamilaw

PageReference viewtPage = new ApexPages.StandardController([SELECT Id FROM CustomObject1__c WHERE Id                                                                                                                                                                                              =:var.CustomObject1__c ]).view();
viewtPage.setRedirect(true);
return viewtPage;

This was selected as the best answer
Arun MKArun MK

 

Hi,

 

Add the parameter saveURL in the URL and pass the CustomObject1 or CustomObject2 record ID.

This will take you to the respective parent page.

 

Regards,

Arun.

developerTeamdeveloperTeam

Thanks for your Reply, But with the my Intial Code am falling to Pre-Populate the value of Parent i.e. CustomObject1__c's  Customfield1__c in the JunctionObject__c.

So I had Treid with my new code for Same Requirement , Plesae check the following Code:

VF 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">  <br/>
  <apex:pageBlockSectionItem >
   <apex:outputLabel value="CusomField1"/>
     <apex:inputField  value="{!JunctionObject__c.CustomObject1__c}" />  // this helps me in pre-populating the value from Parent 
    </apex:pageBlockSectionItem> <br/> 
    <apex:pageBlockSectionItem > 
   <apex:outputLabel value="CusomField2"/> 
    <apex:inputField value="{!JunctionObject__c.CustomObject2__c}" /> 
   </apex:pageBlockSectionItem> 
   </apex:pageBlockSectionItem> 
   </apex:pageBlockSection> 
  </apex:pageBlock>
 </apex:form>
</apex:page>

 and the Controller 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 (); 
        }
    }
}

 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.