+ Start a Discussion
saleforce beesaleforce bee 

Autosave record in lightning

I am trying to achieve Autosave of the record every 5 minutes in Lightning.

Here is the code for Common component which can be included any page that record has to be auto-saved without user intervention

Everything works as expected. Only issue is that I can't kill autosave recursion loop when I move to other lightning page. Is there a way to clear the autosave loop , I am using cleartimeout in the code.


autoSave : function(component,event)
    {
        
        var thisHelper=this;
        var windowHelper=window;
        var timeValue=component. get("v. timer"); 
                
        component. set("v. timerValue", window. setTimeout(     
            
            $A. getCallback(function(){
                // To avoid auto save method call if component becomes invalid
                if(component. isValid()){
                    //Fire component event to access child component methods
                    var autoSaveEventValue = component. getEvent("autoSaveEvent");
                    autoSaveEventValue. fire();               
                    
                    thisHelper. autoSave(component);
                }
                // Clear the timer if component becomes invalid
                else{
                    windowHelper. clearTimeout( component. get("v. timerValue") );
                    
                }
                
                
            }
                          ),timeValue));
        
        
        
    },
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi, Let us know if it helps.

Please mark it as best answer if the information is informative.so that question is removed from an unanswered question and appear as a proper solution.

Thanks
Rahul Kumar
 
Zachary Alexander 22Zachary Alexander 22
Found a way! Our good buddy Jitendra has a detailed description here:
http://www.jitendrazaa.com/blog/salesforce/calling-apex-method-at-regular-interval-from-lightning-component/ (http://www.jitendrazaa.com/blog/salesforce/calling-apex-method-at-regular-interval-from-lightning-component/" id="ext-gen103" rel="nofollow" target="_blank)

My functional code looks like this:
({
    doInit : function(component, event, helper) {

        //Initiate Auto-saves.
        helper.pollAutoSave(component, event, helper);
    },
})
({
    pollAutoSave : function(component, event, helper) { 
        
        //execute callSaveMethod() again after 60 sec each
        window.setInterval(
            $A.getCallback(function() { 
                helper.callSaveMethod(component, event, helper);
            }), 60000
        );      
    },
    
    callSaveMethod : function (component, event, helper){   
        if(helper.validateContactForm(component)) {
            
            // Prepare the action to create the new contact
            var saveUpdateAction = component.get("c.saveUpdateRecord");
            saveUpdateAction.setParams({
                "theSaveRecord": component.get("v.theRecord")
            });
            
            // Configure the response handler for the action
            saveUpdateAction.setCallback(this, function(response) {
                var state = response.getState();
                if(state === "SUCCESS") {
                    //Actions can be added here if desired.
                }
                else if (state === "ERROR") {
                    console.log('Problem saving contact, response state: ' + state);
                }
                    else {
                        console.log('Unknown problem, response state: ' + state);
                    }
            });
            
            // Send the request to do the update.
            $A.enqueueAction(saveUpdateAction);
            
        } 
        
    },
    
})