Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Message Submitted
import { getRecord } from 'lightning/uiRecordApi';
import MESSAGE_OBJECT from '@salesforce/schema/Chat_Message__c';
import CONTENT_FIELD from '@salesforce/schema/Chat_Message__c.Content__c';
import USER_FIELD from '@salesforce/schema/Chat_Message__c.User__c';
// ....
const messageInput = this.template.querySelector('.message-input');
messageInput.addEventListener('keydown', (event) => {
if (event.which === 13 && event.shiftKey === false) {
event.preventDefault();
const fields = {};
fields[CONTENT_FIELD.fieldApiName] = messageInput.value;
fields[USER_FIELD.fieldApiName] = this.userId;
const message = { apiName: MESSAGE_OBJECT.objectApiName, fields };
createRecord(message)
.then(() => {
// Reset the form input field.
messageInput.value = '';
// Refresh the message data for other active users.
this._socket.emit('transmit');
// Refresh the message data for the current user.
return refreshApex(this.wiredMessages);
})
.catch(error => {
// eslint-disable-next-line no-console
console.error('error', error);
this.error = 'Error creating message';
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.