Development Best Practices
  • Home
  • Clean Code
  • Apex
    • Naming Conventions
    • Triggers
    • Unit Testing
    • Principles
      • KISS
      • DRY
      • Single Responsibility
      • Open/Closed
      • YAGNI
    • Patterns
    • SOQL
    • Exception Handling
    • Magic Strings
  • Lightning Components
    • Aura
    • LWC
  • Automation
    • Process Builder
  • Salesforce DX and VSCode
  • Git Source Control
    • Install Git
    • Using Git
    • Development Workflow
      • The Rules
    • Feature Branch Workflow
Powered by GitBook
On this page
  • Call Apex Method using Promise
  • Call Apex Method using Callback
  • Show Toast
  • Handle Errors
  1. Lightning Components

Aura

Call Apex Method using Promise

promiseAction: function (cmp, methodName, params) {
        var self = this;
        return new Promise(function (resolve, reject) {
            var action = cmp.get(methodName);
            action.setParams(params);
            action.setCallback(this, function (response) {
                var state = response.getState();
                if (cmp.isValid() && state === 'SUCCESS') {
                    var result = JSON.parse(response.getReturnValue());
                    resolve(result);
                } else if (state === 'ERROR') {
                    var errors = response.getError();
                    self.handleErrors(errors);
                    reject(errors);
                }
            });
            $A.getCallback(function () {
                $A.enqueueAction(action);
            })();
        });
    },

Call Apex Method using Callback

    callAction: function (cmp, methodName, params, callback) {
        var action = cmp.get(methodName);
        action.setParams(params);
        action.setCallback(this, function (response) {
            var state = response.getState();
            if (cmp.isValid() && state === 'SUCCESS') {
                var result = response.getReturnValue();
                if (callback) callback(result);
            } else if (state === 'ERROR') {
                this.handleErrors(cmp, response.getError());
            }
        });
        $A.getCallback(function () {
            $A.enqueueAction(action);
        })();
    },

Show Toast

    showToast: function (title, message, type) {
        var toastEvent = $A.get("e.force:showToast");
        toastEvent.setParams({
            title: title,
            message: message,
            type: type,
        });
        toastEvent.fire();
    },

Handle Errors

    handleErrors: function (cmp, errors) {
        let toastParams = {
            title: 'Error',
            message: 'Unknown error',
            type: 'error'
        };
        if (errors) {
            if (errors[0] && errors[0].message) {
                console.log(errors[0].message);
                toastParams.message = errors[0].message;
            }
        }
        let toastEvent = $A.get('e.force:showToast');
        toastEvent.setParams(toastParams);
        toastEvent.fire();
    },
})

PreviousLightning ComponentsNextLWC

Last updated 5 years ago