+ Start a Discussion
StenderStender 

Save a Record Without a Page Refresh

Hello,

 

Here is my goal:  I want to be able to display an alert to users the first time they see specific records (cases in this instance).  What I am intending to do is on page load see if the case meets the primary criteria and also make sure that their user ID does not appear in a hidden field.  Then, if the above is true, the message will be displayed and then their user ID added to the hidden field and the record saved.  I want to avoid using customer controllers if possible, but if its necessary I will do it.  

 

The message is popping up fine but for some reason the record is either not being saved, or the field is not being updated before the save.

 

<apex:page standardController="Case" rendered="{!NOT(ISBLANK(Case.Pilot_Participation__c))}" id="thePage">
<apex:form rendered="{!NOT(CONTAINS(case.zTEST__c, LEFT($User.Id, 15)))}">
    <apex:actionFunction name="autosave" action="{!save}" id="autosave" >
        <apex:param assignTo="{!case.zTEST__C}" value="{!case.zTEST__c + ', ' + $User.Id}" name="testUpdate" id="testUpdate" />
    </apex:actionFunction>    
    <script type="text/javascript">
        {
            window.alert("Hello World!");                      
        }
    </script>
    <script>          
          function callAutoSave()
          {              
              autosave();
          }    
    </script>
    </apex:form>
</apex:page>

 

 

Here is what I have, please let me know how to change it (can I just assign the parameter in the java before I call the save function?

 

 

 

 

Thanks,
Jeremy Stender

Peter_sfdcPeter_sfdc

You are declaring a function callAutoSave, but I don't see you invoking it anywhere. Try just doing 

<script>
autosave();
</script>

 That will just call the action function called autosave on page load. 

 

Also, you'll want to bind the action function to quicksave. Binding to save would typically redirect you back to the detail, quicksave does no redirect. Like this: 

 

<apex:actionFunction name="autosave" action="{!quicksave}" id="autosave" >

 Just one thought: I might also investigate doing this save functionality in a workflow field update. In your solution everything is tied to the user looking at the record in this page. If someone creates a different UI in the future, it will introduce a new factor which could result in this logic being ignored. 

GSBassoGSBasso

Aside from the previous poster noting that you don't actually invoke autosave I'm not really sure why what you've written does not work.

 

I managed to get it to work by doing the following:

 

<apex:page standardController="Case" rendered="{!NOT(ISBLANK(Case.Pilot_Participation__c))}" id="thePage">
<apex:form rendered="{!NOT(CONTAINS(case.zTEST__c, LEFT($User.Id, 15)))}">

    <apex:actionFunction name="autosave" action="{!save}" id="autosave" immediate="false" />
    
    <apex:inputHidden id="hiddenValue" value="{!Case.zTEST__c}" />
    
    <script type="text/javascript">
        function callAutoSave()
        {
            var newValue = '{!Case.zTEST__c}' + ', ' + '{!$User.Id}';
            var e = document.getElementById('{!$Component.hiddenValue}');
            e.value = newValue;
            autosave();
        }
        window.sfdcPage.appendToOnloadQueue(callAutoSave);
    </script>
    </apex:form>
</apex:page>

 

StenderStender

I had experienced the same thing with the quicksave, the parameter wasn't being assigned and then the save happened, page reloaded, pop-up happened, saved, reloaded, etc.

 

Anyhow, I don't know why I was so against a custom controller before.  I went ahead and created a quick one that would do the rendered evaluation for me as well as perform the save without a page refresh.  This also avoids the issue you can run into where formulas in rendered attributes not evaluating correctly (I think its IF() functions in AND() functions always come out false or something like that.

 

I also just moved the call of the method in the controller to be in the action attribute on the <apex:page> element, which does exactly what I need.

 

Thanks for the help!

Jeremy Stender

Suresh ThakkarSuresh Thakkar
Thanks Solution worked for me. 

<apex:page standardController="Seat__c" rendered="{!NOT(ISBLANK(Seat__c.First_Name__c))}" id="thePage">
      <apex:form id="appForm" rendered="{!NOT(CONTAINS(Seat__c.Status__c, "Attended"))}">
          <apex:actionFunction name="autosave" action="{!save}" id="autosave" immediate="false" />
          
          <apex:inputHidden id="hiddenValue" value="{!Seat__c.Status__c}" />
          
              <script type="text/javascript">
        function callAutoSave()
        {
            var newValue = "Attended";
            var e = document.getElementById('{!$Component.thePage.appForm.hiddenValue}');
            e.value = newValue;
            autosave();
        }
        window.sfdcPage.appendToOnloadQueue(callAutoSave);
           </script>
        </apex:form>
</apex:page>