function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mayuri NehulMayuri Nehul 

how to show radio button when two fields have same value in lwc

Hi All,

I am new to Salesforce. I am having one requirement where there are 2 date fields if the values of these date fields are the same then show the radio button using LWC.
For eg: if date1 field value = 19/04/2021 and date2 field value = 19/04/2021 then show radio button.

Can anyone please help me with how to achieve it?

Thanks in advance.

 

CharuDuttCharuDutt
Hii Mayuri Nehul
Try The Below Code
LWC
<Input type="date" value={Date1} data-id="Date1" onchange={handleChange}></Input>
<Input type="date" value={Date2} data-id="Date2" onchange={handleChange}></Input>
<template if:true={showRadioBtn}>
<lightning-radio-group name="radioGroup"
label="Radio Group"
options={options}
value={value}
type="radio"></lightning-radio-group>
</template>

JS
 showRadioBtn = false;
  Date1;
  Date2;
  value = '';

  get options() {
      return [
          { label: 'Sales', value: 'option1' },
          { label: 'Force', value: 'option2' },
      ];
  }
  handleChange(event){

    if(event.target.dataset.id === 'Date1'){
        this.Date1 = event.target.value;
        alert(this.Date1);
    }else if(event.target.dataset.id === 'Date2'){
        this.Date2 = event.target.value;
        alert(this.Date2);
    }
    
    if(this.Date1 == this.Date2){
      this.showRadioBtn = true;
    }else{
      this.showRadioBtn = false;
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
Mayuri NehulMayuri Nehul

Hi Charu,

Thank you so much, it worked as per my requirement.

I have one more question 

how can I use the values of this radio button in my record?

For eg: I have the same dates and I am using the radio button for selecting Half day or full day. If I have selected half-day then it should give me 0.5 as the value in my number of days leaves applied field = 0.5.
Can you please tell me how can I do that

Thanks in advance
 

CharuDuttCharuDutt
Hii Mayuri Nehul
Try Below Code I've Made Some Change
LWC

Number OF Days : {days}
Remaining Number OF Days : {rdays}
<Input type="date" value={Date1} data-id="Date1" onchange={handleChange}></Input>
<Input type="date" value={Date2} data-id="Date2" onchange={handleChange}></Input>
<template if:true={showRadioBtn}>
<lightning-radio-group name="radioGroup"
label="Radio Group"
options={options}
value={value}
type="radio"
onchange={handleRadioChange}></lightning-radio-group>
</template>

Js

 showRadioBtn = false;
  Date1;
  Date2;
  days;
  rdays;
  value = '';

  get options() {
      return [
          { label: 'Half-day', value: 'Halfday' },
          { label: 'Full-day', value: 'Fullday' },
      ];
  }
  handleChange(event){
    if(event.target.dataset.id === 'Date1'){
        this.Date1 = event.target.value;      
    }else if(event.target.dataset.id === 'Date2'){
        this.Date2 = event.target.value;
    }
    if(this.Date1 == this.Date2){
      this.showRadioBtn = true;
    }else{
      this.showRadioBtn = false;
    }

    var daydate1 = Number(this.Date1.substring(8));
    var daydate2 = Number(this.Date2.substring(8));
    var NumberOfDays = (daydate2-daydate1)+1;
    this.days = Number(NumberOfDays);
}
  handleRadioChange(event) {
    var selectedOption = event.detail.value;
    this.value = selectedOption;
    if(selectedOption == 'Halfday'){
      this.rdays = (this.days/2);
    }else if(selectedOption == 'Fullday'){
      this.rdays = (this.days-this.days);
    }
  }
Let Me Know If This Helps And Don't Forget To Like And Mark It As Best Answer So It Can Help Other
Thank You!

 
Mayuri NehulMayuri Nehul
Hi Charu,

I have tried this code but it is not working as per my requirement. If I have selected half-day still it is giving me no. of days applied=1.

I have one doubt as I have used no. of days leave applied as a formula field because as per my requirement I need to exclude weekends while calculating days after that I am passing the value of this field in trigger for calculating balance leaves and leaves already taken so my concern is if I used radio buttons value it will impact calculation?

no_days =
 (IF(AND((5 - (CASE(MOD( fromDate__c- DATE(1900, 1, 6), 7), 0, 0, 1, 5, 2, 5, 3, 4, 4, 3, 5, 2, 6, 1, 0)) < (CASE(MOD(  toDate__c  - DATE(1900, 1, 6), 7), 0, 0, 1, 0, 2, 1, 3, 2, 4, 3, 5, 4, 6, 5, 0)) ),
((( toDate__c  -   fromDate__c) + 1) < 7)),
((CASE(MOD(  toDate__c  - DATE(1900, 1, 6), 7), 0, 0, 1, 0, 2, 1, 3, 2, 4, 3, 5, 4, 6, 5, 0)) - (5 - (CASE(MOD(  fromDate__c  - DATE(1900, 1, 6), 7), 0, 0, 1, 5, 2, 5, 3, 4, 4, 3, 5, 2, 6, 1, 0)))),
(((FLOOR((( toDate__c  -  fromDate__c ) - (CASE(MOD(  fromDate__c  - DATE(1900, 1, 6), 7), 0, 0, 1, 6, 2, 5, 3, 4, 4, 3, 5, 2, 6, 1, 0))) / 7)) * 5) +
(CASE(MOD(  fromDate__c  - DATE(1900, 1, 6), 7), 0, 0, 1, 5, 2, 5, 3, 4, 4, 3, 5, 2, 6, 1, 0)) +
(CASE(MOD(  toDate__c  - DATE(1900, 1, 6), 7), 0, 0, 1, 0, 2, 1, 3, 2, 4, 3, 5, 4, 6, 5, 0)))))

this is my formula field. Please let me know if I am missing anything here.

Thanks in advance