0
    const defaultValues = encodeDefaultFieldValues({ 
        StartDateTime: '2022-08-22 20:00:00',
    });
    
    this[NavigationMixin.Navigate({
    
    type: 'standard_objectPage",
    
    attributes: {    
        objectApiName: 'Event", 
        actionName: 'new'
    },
    
    state : {    
       navigationLocation: 'RELATED_LIST', 
       defaultFieldValues: defaultValues,
    }

 });

I am building a Lightning Web Component in which I am try to make a functionality, on a click of button it navigate to new record page of Event object. Here I am trying to provide default value to StartDateTime field of Event Object but it isn't accepting this value.

2

1 Answer 1

2

The datetime string must be in ISO format with T between date and time with the time offest of UTC: Z.
This means that if you want to see 20:00:00 in UI, so in your time zone, unless you're in GMT+0 you should add or subtract your time zone.

I'm at GMT+2 so I have to subtract 2 hours:

const defaultValues = encodeDefaultFieldValues({
    StartDateTime: '2022-08-22T18:00:00Z'
});

Anyway it's better option creating a Date object and call toISOString(), which returns always zero UTC offset datetime string, handling the time zone for you:

const startDate = new Date('2022-08-22T20:00:00'); // please note the T
const defaultValues = encodeDefaultFieldValues({
    StartDateTime: startDate.toISOString()
});

Date object has also a constructor that takes (year, monthIndex, day, hours, minutes, seconds), so you can build it this way:

const startDate = new Date(2022, 7, 22, 20, 0, 0);

Please note that the value for August is 7, since it's a zero-based index so zero indicates the first month of the year

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .