ABSYZ ABSYZ

  • Home

    Home

  • About us

    Who We Are

  • Our Expertise

    What we Do

  • Our Approach

    How We Do It

  • Products

    What We Made

  • Industries

    Who We Do It For

  • Clients

    Whom We Did It For.

  • Article & Blogs

    What Experts Think

  • Careers

    Join The Team

  • Get In Touch

    Let’s Get Started

ABSYZ

Usage of Spring ’19 component — lightning:unsavedChanges

Home / Article & Blogs / Salesforce / Lightning / Usage of Spring ’19 component — lightning:unsavedChanges

Usage of Spring ’19 component — lightning:unsavedChanges

By sebanasir94 inLightning, Salesforce, Salesforce Releases

Accidents are commonplace! And yeah, Spring  ’19 has come up with a new component lightning:unsavedChanges. Voila!!

We often come across situations where we type in data and then we accidentally refreshes, closes the page or press back button and all the entered data is lost!!  lightning:unsavedChanges  comes in to help here! It notifies the user about the unsaved changes in the page during the above mentioned situations and provides them the choice to continue, discard or save the data and handles the process accordingly. No more frustrations over data loss!!

Before we move ahead, it would be handy to remember that this component requires API version 45.0 or later and is not supported by one.app

In this blog, we demonstrate a sample of how this component works.

[sourcecode language=”java”]
<aura:component controller=”unsavedChangesClass” implements=”force:appHostable, flexipage:availableForAllPageTypes, flexipage:availableForRecordHome, force:hasRecordId, forceCommunity:availableForAllPageTypes, force:lightningQuickAction” access=”global” >
<lightning:unsavedChanges aura:id=”unsavedData”
onsave=”{!c.handleSave}”
ondiscard=”{!c.handleDiscard}”/>
<aura:attribute name=”contactDetail” type=”Contact” default=”{‘sobjectType’ : ‘Contact’}”/>

<lightning:card title=”Create Related Contact”>
<lightning:input aura:id=”firstName” label=”FirstName” value=”{!v.contactDetail.firstName}” onchange=”{!c.handleDataChange}”/><br/>
<lightning:input aura:id=”lastName” label=”LastName” required=”true” value=”{!v.contactDetail.LastName}” onchange=”{!c.handleDataChange}”/><br/>
<lightning:input aura:id=”phone” label=”Phone” value=”{!v.contactDetail.Phone}” onchange=”{!c.handleDataChange}”/><br/>
<lightning:button variant=”brand” label=”Create Contact” onclick=”{!c.saveContact}”/>
</lightning:card>
</aura:component>
[/sourcecode]

CONTROLLER

[sourcecode language=”java”]

({
//Method call on Create Button Click
saveContact : function(component, event, helper){
helper.createContact(component, event);
},

/*This method sets the boolean for unsavedChanges to true,
* so that it indicates the containment that unsaved data is rpesent
* */
handleDataChange: function(component, event, helper) {
var unsavedData = component.find(‘unsavedData’);
unsavedData.setUnsavedChanges(true, {label: ‘creating related contact?’});
},

/**
* This method will be called when user click save button from dialog window
* Save your unsaved changes here
* */
handleSave: function(component, event, helper) {
helper.createContact(component, event);
},

/**
* This method will be called when user clicks ‘discard changes’ button from dialog window
* */
handleDiscard: function(component, event, helper) {
var unsavedData = component.find(‘unsavedData’);
unsavedData.setUnsavedChanges(false);
}
})
[/sourcecode]

HELPER

[sourcecode language=”java”]
({
createContact : function(component, event){
var action = component.get(‘c.createContact’);
action.setParams({‘accId’ : component.get(‘v.recordId’),
‘lName’ : component.find(‘lastName’).get(‘v.value’)});
action.setCallback(this, function(response){
var state = response.getState();
if(state === ‘SUCCESS’){
var unsavedData = component.find(‘unsavedData’);
unsavedData.setUnsavedChanges(false);

var navEvt = $A.get(“e.force:navigateToSObject”); //To navigate
navEvt.setParams({
“recordId”: response.getReturnValue().Id,
});
navEvt.fire();
}else{
console.log(‘error’);
}
});
$A.enqueueAction(action);
}
})
[/sourcecode]

APEX

[sourcecode language=”java”]
public with sharing class unsavedChangesClass {
@AuraEnabled
public static Contact createContact(Id accId, String lName){
Contact con = new Contact(lastname=lName,
accountId = accId);
insert con;
return con;
}
}
[/sourcecode]

The process is majorly handled by the function setUnsavedChanges within the lightning component.  This method holds two arguments  –

  • A Boolean argument to indicate if usaved data is present in the page or not. (True if unsaved content is present, false otherwise)
  • An optional object argument.

Upon data entry, we set the setUnsavedChanges to true; And therefore, when the user takes any action that loses data, it notifies the containment that unsaved data is present in the page and the dialogue window pops up as shown in the screenshot. Now, depending on the user’s decision, the handleSave  or handleDiscard custom methods will be invoked. The onsave attribute controls the action to  handle saving unsaved content  and the  ondiscard attribute  controls the action to handle discarding unsaved content. Also, keep in mind that, these methods should call the setUnsavedChanges() method again to return control back to the Lightning UI.

[wpvideo 8Z6jekSz]

Lightning componentLightning Updates in Spring'19 release notes
76
Like this post
3 Posts
sebanasir94

Search Posts

Archives

Categories

Recent posts

10 Amazing Benefits of Salesforce Marketing Cloud

10 Amazing Benefits of Salesforce Marketing Cloud

How to Set SMART Goals for Employee Performance?

How to Set SMART Goals for Employee Performance?

Difference Between ERP and CRM? What Is Their Importance?

Difference Between ERP and CRM? What Is Their Importance?

What You Need To Know About India Salesforce Days 2022 – Platform Accredited Professional

What You Need To Know About India Salesforce Days 2022 – Platform Accredited Professional

<strong>Top digital transformation challenges affecting the businesses in 2022</strong>

Top digital transformation challenges affecting the businesses in 2022

  • Previous PostHighlights of Spring'19 Release: Part II
  • Next PostContinuous Integration in Salesforce using Jenkins and Bitbucket

Related Posts

What You Need To Know About India Salesforce Days 2022 – Platform Accredited Professional
Salesforce

What You Need To Know About India Salesforce Days 2022 – Platform Accredited Professional

What Is Salesforce Commerce Cloud?
Commerce Cloud Salesforce

What Is Salesforce Commerce Cloud?

Why Do We Use Lightning in Salesforce?
Lightning Salesforce

Why Do We Use Lightning in Salesforce?

<strong>Salesforce announces NFT Cloud pilot with far-reaching impact</strong>
Salesforce

Salesforce announces NFT Cloud pilot with far-reaching impact

3 Comments

  1. Usage of Spring ’19 component — lightning:unsavedChanges — ABSYZ – SutoCom Solutions
    Reply
    9 March 2019
    Reply
  2. Usage of Spring ’19 component — lightning:unsavedChanges — ABSYZ – SutoCom Solutions
    Reply
    9 March 2019
    Reply
  3. Alex Sch.
    Reply
    30 April 2020

    Hey 🙂 I’m having an issue, exactly the other way around.
    I am working on an app where I need to skip the standard UnsavedChanges prompt. When a user clicks save on a record I catch this by addError() method, I then want to make the user interact with a modal I show and when they click save on my modal, I want it to simply close the edit view or reload the page to show the record page without edit mode. Do you know if there is a way to do so?

    thanks and best regards,
    Alex

    Reply

Leave a Reply (Cancel reply)

Your email address will not be published. Required fields are marked *

*
*

ABSYZ Logo
  • About us
  • Article & Blogs
  • Careers
  • Get In Touch
  • Our Expertise
  • Our Approach
  • Products
  • Industries
  • Clients
  • White Papers

ABSYZ Software Consulting Pvt. Ltd.
USA: 49197 Wixom Tech Dr, Wixom, MI 48393, USA
M: +1.415.364.8055

HYD – India: 6th Floor, SS Techpark, PSR Prime, DLF Cyber City, Gachibowli, Hyderabad, Telangana – 500032
M: +91 79979 66174

2nd Floor, 91Springboard, Padmavathi Complex, 80 Feet Rd, Koramangala, 8th Block, Bengaluru, Karnataka-560095.
+91 8886332345

Copyright ©2020 Absyz Inc. All Rights Reserved.

youngsoft
Copy
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “ACCEPT ALL”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent. Privacy Policy
Cookie SettingsREJECT ALLACCEPT ALL
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled

Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.

CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.

Functional

Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.

Performance

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

Analytics

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.

Advertisement

Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.

Others

Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.

SAVE & ACCEPT