+ Start a Discussion
Ola BamideleOla Bamidele 

javascript code not saving or redirecting

Hi Gurus, 

I have this Javascript code that is suppose to save a form on to my custom object is Salesforce and then redirect the user to my second visualforce page, which is a "ÿou have successfully saved the form" page. 

However when I add the code to my VF page, the form doesnt save or get redirected.

Please if someone knows what is wrong with the code, please let me know.



My code for the save and redirection to second page:

<apex:actionFunction name="savefromJS" action="{!save}" />


<script>
    function saverecordandredirect(){
       savefromJS();
       window.location('/apex/ThankYouPage');
    }
</script>


<apex:commandButton value="Save it" onclick="saveandredirtect() ;"/>


 
SHAIK MOHAMMAD YASEENSHAIK MOHAMMAD YASEEN
Hi Ola Bamidele,

Instead of redirecting the page through javascript you can redirect it through controller like below. 

<apex:commandButton value="Save Record"  action="{!Saverecord}" /><!--Page button code-->

//controller return method
public PageReference Saverecord() 
    {
        /* code to inserting the record*/
        PageReference newPage = page.yourPageName;
        newPage.setRedirect(true);
        return  newPage ;  
    }
 
Timothy Gentet O'BrienTimothy Gentet O'Brien
If you want to use Apex +1 to the above, it's a better way of doing it, however, if you want to use pure JS for this you can use the following:

<apex:actionFunction name="saverecordandredirect" action="{!save}" oncomplete="location.href = '{! $Page.ThankYouPage }'" />
<apex:commandButton value="Save it" onclick="saveandredirtect() ;"/>

Using the Global Merge Field syntax of $Page, Salesforce will auto recognise where you are logged in from and auto define the relevant URL for you, so if you are in Classic, Lightning, Mobile, or SF1, it will still work!
Ola BamideleOla Bamidele
Hi Timothy Gentet O'Brien, 

Thanks for the reply. I tried the Javascript code however it didnt save and it didnt redirect onto teh next page that I sent. 

Do you possible know why this is? Thanks very much
Timothy Gentet O'BrienTimothy Gentet O'Brien
@Ola Bamidele - can you post the updated Visualforce and Controller code? There may be something else that is causing an issue here, which we cannot see unless we have the full page :)
Ola BamideleOla Bamidele
@Timothy Gentet O'Brien,

I dont have an extension page. I am trying to do the redirection without having to use an entension controller,if that makes sense.

I want to simply have one visualforce page which will perform the save and redirection to my second visualforce page.

This is my code after I have added the script you suggested:

<apex:page standardController="Turndown__c" sidebar="false" showHeader="true" >
    <apex:form >
           <apex:pageBlock title="Documented Turndowns" id="turndown_list">
                
             <apex:pageBlockSection columns="1" >
                 
                
                 <style type="text/css">
                     .bPageHeader {display: none;}
                     </style>
              
                   
             
            <apex:inputfield value="{! Turndown__c.Account__c }"/>
            <apex:inputfield value="{! Turndown__c.First_Name__c }"/>
            <apex:inputfield value="{! Turndown__c.Last_Name__c }"/>     

            <apex:inputTextarea value="{! Turndown__c.Comment__c }" style="width:350px; height: 70px;" />

            </apex:pageBlockSection>
            
   
             
          <apex:actionFunction name="saverecordandredirect" action="{!save}" oncomplete="location.href = '{! $Page.VisualForceTurndownTestingPage2 }'" />
                    <apex:commandButton value="Save it" onclick="saveandredirtect() ;"/>
               
          
                </apex:pageBlock>
    </apex:form>
    
</apex:page>


I also tried the code you sent with the "script" tag and it didnt work.

Thanks very much!
 
Timothy Gentet O'BrienTimothy Gentet O'Brien
Ahhhhhhh okay, I know what you are trying to achieve now!

Just as a note showHeader="true" <-- this is not needed, showHeader defaults to true, so you don't need to specify it!
<apex:page standardController="Turndown__c" sidebar="false" showHeader="true">
    <apex:form>
        <apex:pageBlock title="Documented Turndowns" id="turndown_list">
            <apex:pageBlockSection columns="1" >
                <style type="text/css">
                    .bPageHeader {
                        display: none;
                    }
                </style>
                
                <apex:inputfield value="{! Turndown__c.Account__c }"/>
                <apex:inputfield value="{! Turndown__c.First_Name__c }"/>
                <apex:inputfield value="{! Turndown__c.Last_Name__c }"/>
                <apex:inputTextarea value="{! Turndown__c.Comment__c }" style="width: 350px; height: 70px;" />
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton action="{! save }" value="Save it" oncomplete="location.href = '{! $Page.VisualForceTurndownTestingPage2 }'" />
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>
With the use of the apex:pageBlockButtons, with the location set to bottom, you can use only a commandButton without having to use an apex:actionFunction as well!

I believe the behaviour you are seeing is due to the fact that when you click on a button (commandButton) with an onclick function, it doesn't try and submit the form by refreshing the page, it completes the action then goes to the onclick. So after that the actionFunction is then trying to execute, but the onclick is still running so it refreshes the page before you are able to actually complete the onclick action.

With the use of just the button, it will ignore having to exectue a few actions and hopefully won't get confused and should work correctly.

Try this and let me know if it works!
Ola BamideleOla Bamidele
@ Timothy Gentet O'Brien, 

Thanks again for the reply. Yes I also understand what your are saying.

I just tried the code and it didnt save onto my Salesforce object. However, it did redirect to the second page!


Thanks