0

Thanks for reading, I'm running into an odd occurrence while trying to debug some code that was built out-of-house, and I'm totally stumped.

We're setting an apex variable, ID, to String.valueOf(Datetime.now()), and when I test it on the next line, it gives me 2021-09-01 14:56:21.

ID = String.valueOf(Datetime.now()); 
System.debug('ID Value: ' + ID);

14:56:21:027 USER_DEBUG [203]|DEBUG|ID Value: 2021-09-01 14:56:21

Which is what I expected to see.

In our LWC, we're displaying a table to our users which has buttons/checkboxes on each row, so we're setting the ID parameter on these lightning-inputs to the ID variable captured earlier. See table code below.

<template for:each={Invoices} for:item="invoice" class="">
                    <tr key={invoice.Id}>
                        <td class="tablecell "><lightning-input class="smallItem" id={invoice.Id} type="checkbox" onchange={ipChanged} checked={invoice.ImmediatePayment} disabled={invoice.IpDisabled}></lightning-input></td>
                        <td class="tablecell"><lightning-input id={invoice.Id} type="date" value={invoice.PaymentDate} onchange={dateChanged} disabled={invoice.DateDisabled}></lightning-input></td>
                        <td class="tablecell"><lightning-input id={invoice.Id} type="Number" step=".01" value={invoice.PaymentAmt} disabled={invoice.AmtDisabled} onchange={amtChanged}></lightning-input></td>
                        <td class="tablecell ">
                            <lightning-combobox id={invoice.Id} name="progress"  value={invoice.Selected_Payment}  placeholder="Select A Payment Method" options={ccOptions}  onchange={ccChanged} disabled={invoice.CcDisabled}></lightning-combobox>
                        </td>

                    </tr>
                </template>

When a user triggers the first lightning-input's onChange event, ipChanged(event), the following code fires:

ipChanged(event)
    {
        console.log('in IpChanged');
        console.log(event.target.checked); 
        var id = event.target.id;
        console.log('var ID: '+ id);

and var ID: 2021-09-01-565 14:56:21-565 is returned.

My ultimate question is where is the extra -565 coming from at the end of the date and time portions of this datetime? The code is equating this later on and its throwing an error because it can't find the datetime ID from before. It's double strange because we set the initial ID as a String value of a datetime, so shouldn't this value not change?

Is this a biproduct of setting a datetime as an id on a lightning-input?

3

Id values are modified by the runtime in order to be globally unique. Do not use id for any purpose whatsoever, it may be unreliable. Instead, attach a data-* attribute to your data:

<lightning-input data-id={invoice.Id} ...>
</lightning-input>

(Remaining attributes omitted for legibility)

Then you can read it in your handler:

var id = event.target.dataset.id;

You can read more about dataset.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.