force:showToast In lightning Controller

Screen Shot 2019-01-01 at 12.43.09 PM.png

A toast displays a message below the header at the top of a view. The message is specified by the messageattribute.

force:showToast is not available on login pages.

Toasts are the simplest form of notification a developer can use to display a notification dynamically.

Toasts are 4 types

  •  info
  • success
  • warning
  • error

Basic Format is

showMyToast : function(component, event, helper) {
    var toastEvent = $A.get("e.force:showToast");
    toastEvent.setParams({
        mode: 'sticky',
        message: 'This is a required message',
        messageTemplate: 'Record {0} created! See it {1}!',
        messageTemplateData: ['Salesforce', {
            url: 'http://www.salesforce.com/',
            label: 'here',
            }
        ]
    });
    toastEvent.fire();
}
  • key :  icon on the toast when type is not defined.
  • title :the title of the toast
  • message :  the message to show the user
  • messageTemplate :  If we want to override the message which is specified in message attribute we can use this attribute along with messageTemplateData. 
  • messageTemplateData : To show the overridden message you must use this attribute.
  • duration : the duration of toast to be show to user. After specified duration toast will automatically disappear.
  •  mode :  To how the toast will disappear. In salesforce there are 3 types of mode.
    • dismissible :  Toast will disappear either by user action or on complete of specified duration.
    • pester :  Toast will disappear on complete of specified duration. Close button will not appear.
    • sticky :  Toast will disappear  by user action .

Example:-

    showInfoToast : function(component, event, helper) {
        var toastEvent = $A.get("e.force:showToast");
        toastEvent.setParams({
            title : 'Some Information',
            message: 'Here is Something you should know',
            messageTemplate: 'Record {0} created! See it {1}!',
            duration:' 5000',
            key: 'info_alt',
            type: 'info',
            mode: 'dismissible'
        });
        toastEvent.fire();
    },
    showSuccessToast : function(component, event, helper) {
        var toastEvent = $A.get("e.force:showToast");
        toastEvent.setParams({
            title : 'Success',
            message: 'Congrats,It works!',
            messageTemplate: 'Record {0} created! See it {1}!',
            duration:' 5000',
            key: 'info_alt',
            type: 'success',
            mode: 'pester'
        });
        toastEvent.fire();
    },
    showErrorToast : function(component, event, helper) {
        var toastEvent = $A.get("e.force:showToast");
        toastEvent.setParams({
            title : 'Error',
            message:'Sorry, It's Failed!',
            messageTemplate: 'Its Failed',
            duration:' 5000',
            key: 'info_alt',
            type: 'error',
            mode: 'pester'
        });
        toastEvent.fire();
    },
    showWarningToast : function(component, event, helper) {
        var toastEvent = $A.get("e.force:showToast");
        toastEvent.setParams({
            title : 'Warning',
            message: 'Becareful or else',
            messageTemplate: 'Becareful or else{1}',
            messageTemplateData: ['Salesforce', {
                url:'https://wordpress.com/view/jayakrishnasfdc.wordpress.com',
                label: 'Click Here',
            }],
            duration:' 5000',
            key: 'info_alt',
            type: 'warning',
            mode: 'sticky'
        });
        toastEvent.fire();
    },
})

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s