top of page
Search

High Priority Toast Notification on Campaign

  • abhyash
  • Nov 12, 2022
  • 2 min read

v1 Prepared By: Abhyash Timsina - 12 November 2022


Motivation: Sometimes you need to notify users about urgent, high priority or important records. In this example, we go over having a checkbox that managers can tick for Campaigns. When users open up the record, they see a toast notification that implies it’s a High Priority Campaign. It’s a simple Lightning Web Component that’s hidden on a record page that triggers the toast upon load.



My Technical Components




End Outcome:



Apex Class with annotation:


/**
 * Created by Abhyash Timsina on 12/11/2022.
 */

public with sharing class GetCampaign {

// Aura enabled required for apex to communicate with lwc
    @AuraEnabled
    
    // Method that returns boolean and accepts id as parameter
    public static Boolean getHighPriorityValue(String campaignId){
    // SOQL query to return value of high priority field from the record id
        List<Campaign> highPriority = [SELECT High_Priority__c FROM Campaign WHERE Id =: campaignId LIMIT 1];
        return highPriority[0].High_Priority__c;
    }

}



LWC – Html:


<!--
 - Created by Abhyash Timsina on 12/11/2022.
 -->

// Nothing here as we want our front end (what user sees) to be blank and hidden
<template>
</template>


LWC – JS:


/**
 * Created by Abhyash Timsina on 12/11/2022.
 */

// Added api (for recordid and track for the high priority variable)
import {LightningElement, api, track} from 'lwc';
// Show toast event required for its methods below
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
// Referencing the apex class 'Get Campaign' here
import getHighPriorityValue from '@salesforce/apex/GetCampaign.getHighPriorityValue';

export default class showToast extends LightningElement {
// recordId will be passed from the record page where this component is added
    @api recordId;
    // creation of tracking variable
    @track highPriorityValue;

// Happens on page load
    connectedCallback(){
    
    // Calling apex imperatively by passing lwc record id
        getHighPriorityValue({campaignId:this.recordId})
         .then(result=>{
         // store the result value in the tracking variable
             this.highPriorityValue=result;
             // checks if the checkbox is true
             if (this.highPriorityValue === true) {
             // Calls method to show the toast message
                 this.showWarningToast();
             }
         })
         .catch(error=>{
         // if error, catch it into the tracking variable
             this.highPriorityValue=undefined;
         })
    }

// Method to show the on screen message
    showWarningToast() {
        const evt = new ShowToastEvent({
            title: 'High Priority Campaign',
            message: 'Please take extra care as this a High Priority Campaign',
            // see below link for other options you can use
            variant: 'warning',
            mode: 'sticky'
        });
        this.dispatchEvent(evt);
    }



}




LWC – Metadata


<?xml version="1.0" encoding="UTF-8"?>

<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>56.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
    // only exposed to a record page - in this case - campaign
        <target>lightning__RecordPage</target>
    </targets>

</LightningComponentBundle>


Package Link:


If you need help in installing and getting this working – please contact me on LinkedIn

 
 
 

Comments


bottom of page