HI,
I am trying to do a LWC that countdowns until the Publication Date field, I want to update the record Status field to "Publish"when the count down end. If possible I have to also make sure that my other date field "Deadline" when current date/time is greater then this I can update the Status to "Closed".
My Current LWC is focused on countdown but think I need to do lots of changes to update the record, but not sure how to?
HTML:
<template>
<lightning-card title="Days remaining to tender deadline:">
<lightning-layout-item flexibility="auto" alignment-bump="right">
<div class="slds-var-m-around_medium">
<p>Remaining Time: {timer} </p>
<!--<h1>Current ID: {recordId} </h1>
<h2>Current Close Date: {deadline} </h2>-->
</div>
</lightning-layout-item>
</lightning-card>
</template>
JS:
import { LightningElement, api, wire } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import PUBLICATION_FIELD from '@salesforce/schema/NL_Tender_Opportunity__c.Publication_Date__c';
import STATUS_FIELD from '@salesforce/schema/NL_Tender_Opportunity__c.Status__c';
export default class OppCloseCountDown extends LightningElement {
@api recordId;
data;
error;
deadline;
timer = 'Timer to start !!';
setTimeInterval;
@wire(getRecord, { recordId: "$recordId", fields: [PUBLICATION_FIELD], fields:[STATUS_FIELD] })
wireMilestone({ data,error}) {
if(data)
{
this.deadline = getFieldValue(data, PUBLICATION_FIELD);
clearInterval(this.setTimeInterval);
this.setTimeInterval = setInterval(() => {
// If the count down is finished, reset to Zero
let day = new Date(this.deadline);
let ddate = day.getTime();
//console.log(">>>ddate getTime() " + ddate); //getTime() output date in this format "1653955200000"
let currentDateTime = new Date().getTime();
//console.log("currentDateTime getTime " + currentDateTime);
let timeDifference = ddate - currentDateTime;
this.timer = 'I m timeDifference ' + timeDifference;
//console.log("diff deadlineDate - now " + timeDifference);
if (timeDifference < 0) {
clearInterval(this.setTimeInterval);
this.timer = 'Tender not Active';
} else {
let days = Math.floor(timeDifference / (1000 * 60 * 60 * 24));
let hours = Math.floor(
(timeDifference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
);
let minutes = Math.floor((timeDifference % (1000 * 60 * 60)) / (1000 * 60));
let seconds = Math.floor((timeDifference % (1000 * 60)) / 1000);
this.timer =
days + " days " + hours + " hrs " + minutes + " mins " + seconds + " secs ";
}
}, 1000)
}
else if(error)
{
this.error = error;
//console.log("Error: " + JSON.stringify(error));
this.ddate = undefined;
//console.log('Error')
}
}
}
META.XML
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>61.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__RecordPage</target>
<target>lightning__AppPage</target>
<target>lightning__HomePage</target>
</targets>
<targetConfigs>
<targetConfig targets="lightning__RecordPage">
<objects>
<object>NL_Tender_Opportunity__c</object>
</objects>
</targetConfig>
<targetConfig targets="lightningCommunity__Default">
<property name="recordId" type="String"
label="Record ID" description="The value should be {!recordId}."/>
</targetConfig>
</targetConfigs>
</LightningComponentBundle>
Thanks
@* Salesforce Developers *@Lightning Web Components
Does this expires within few minutes? What happens if the user is not in the LWC page when the timer expires? Why not do a flow with a schedule path?