Skip to content

Instantly share code, notes, and snippets.

@jamigibbs
Last active March 30, 2020 20:57
Show Gist options
  • Save jamigibbs/559e3d39a185e1d1abc797e40d4c89cf to your computer and use it in GitHub Desktop.
Save jamigibbs/559e3d39a185e1d1abc797e40d4c89cf to your computer and use it in GitHub Desktop.
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