Wednesday 15 September 2021

How to float text in lightning web component ?

 Use Case - Publish message to all users in salesforce in Home Page or any other record page.

                   1. It should be easily configurable.

                   2. We can stop displaying message whenever we want from a configuration. It should have Start Time and End Time.

Here is my approach to achieve the same.

1. Create a custom Object Salesforce_News__c. custom fields - Start_Date__c , End_Date__c, Active__c and Message__c



2. Create record page for  Salesforce_News__c and add the page to application. 

3. Create a permission set "Salesforce News configure"  which will have Create,Edit access to object Salesforce_News__c.

4. Assign permission to user who is going to configure message.

5. Create LWC component 

6. Add LWC component to Home Page.

Here is LWC component code.

<template>
<marquee> 
<div class="slds-clearfix" if:true={showMessage}>
  <div class="slds-float_left">
    <p>
    <b><span style="color:red">Important Note : </span>
    <span style="color:white;font-family: Arial, Helvetica, sans-serif;
 font-size: medium;">{newsTicket.Message__c}
    </span></b>
    </p>
  </div>
</div>
</marquee>
</template>

Javascript
import { LightningElement,api } from 'lwc';
import showMessage from '@salesforce/apex/NewsTickerCls.showMessage';
export default class NewsTicker extends LightningElement {
    showMessage;
     @api newsTicket;
     connectedCallback(){
         this.showMessage = false;
         showMessage()
         .then(res => {
             if(res !=null){
                  this.showMessage = true;
                  this.newsTicket = res;
                  console.log('this.newsTicket--',this.newsTicket);
             }
            
             
         });
         
     }
     
}

XML code
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>52.0</apiVersion>
    <isExposed>true</isExposed>
     <targets>
        <target>lightning__RecordPage</target>
        <target>lightning__AppPage</target>
        <target>lightning__HomePage</target>
    </targets>
</LightningComponentBundle>

Apex class
public with sharing class NewsTickerCls { @AuraEnabled public static Salesforce_News__c showMessage(){ Datetime currTime = System.now(); List<Salesforce_News__c> currentMessag = [Select Message__c
from Salesforce_News__c
where Active__c = true AND start__c <:currTime AND End__c >: currTime limit 1]; If(!currentMessag.isEmpty()){ return currentMessag[0]; } return null; } }
Test class
@isTest
private class NewsTickerClsTest {

   @isTest  
    static void dotest(){
        Salesforce_News__c news = new Salesforce_News__c();
        news.Message__c = 'Test Message';
        news.start__c = System.today()-1;
        news.End__c = System.today()+1;
        news.Active__c = true;
        Insert news;
        test.startTest();
        Salesforce_News__c result = NewsTickerCls.showMessage();
        System.assert(result !=null);
        test.stopTest();
            
    }
}



No comments: