Created
July 24, 2021 00:24
-
-
Save lukaszhanusik/6118730a244fe018e86c77f8306efb4c to your computer and use it in GitHub Desktop.
LWC LDS Wire Apex Refresh Notify
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { LightningElement, api, wire } from 'lwc'; | |
import { getRecord, getFieldValue, getRecordNotifyChange, updateRecord } from 'lightning/uiRecordApi'; | |
import { ShowToastEvent } from 'lightning/platformShowToastEvent'; | |
import { refreshApex, getSObjectValue } from '@salesforce/apex'; | |
import getTemplateRecord from '@salesforce/apex/TemplateBuilderController.getTemplateRecord'; | |
import updateTemplateRecord from '@salesforce/apex/TemplateBuilderController.updateTemplateRecord'; | |
const ENTITY_FIELDS = { | |
"574" : { | |
"name" : "QuickText.Name", | |
"body" : "QuickText.Message" | |
}, | |
// FIX: EmailTemplate unsupported by UI API | |
"00X" : { | |
"name" : "EmailTemplate.Name", | |
"body" : "EmailTemplate.HtmlValue" | |
} | |
} | |
export default class TemplateMessageBuilder extends LightningElement { | |
@api recordId; | |
template; | |
templateApex; | |
get entityFields() { | |
let entityPrefix = this.recordId.substring(0, 3); | |
return ENTITY_FIELDS[entityPrefix]; | |
} | |
get templateFields() { | |
return [this.entityFields.name, this.entityFields.body]; | |
} | |
get templateName() { | |
return getFieldValue(this.template, this.entityFields.name); | |
} | |
get templateBody() { | |
return getFieldValue(this.template, this.entityFields.body); | |
} | |
get templateBodyApex() { | |
return getSObjectValue(this.templateApex, this.entityFields.body); | |
} | |
handleWireError(error) { | |
let message = 'Unknown error'; | |
if (Array.isArray(error.body)) { | |
message = error.body.map(e => e.message).join(', '); | |
} else if (typeof error.body.message === 'string') { | |
message = error.body.message; | |
} | |
this.dispatchEvent( | |
new ShowToastEvent({ | |
title: 'Error loading record', | |
message, | |
variant: 'error', | |
}), | |
); | |
} | |
@wire(getRecord, { recordId: '$recordId', fields: '$templateFields' }) | |
wiredRecord({ error, data}) { | |
if (error) { | |
this.handleWireError(error); | |
} else if (data) { | |
this.template = data; | |
} | |
} | |
wiredTemplateRecordValue; | |
@wire(getTemplateRecord, { recordId: '$recordId' }) | |
wiredTemplateRecord(value) { | |
this.wiredTemplateRecordValue = value; | |
const { error, data } = value; | |
if (error) { | |
this.handleWireError(error); | |
} else if (data) { | |
this.templateApex = data; | |
} | |
} | |
updateTemplate(event) { | |
const fields = { "Id": this.recordId }; | |
fields['Message'] = 'Updated with UI API'; | |
const recordInput = { fields }; | |
updateRecord(recordInput) | |
.then(() => { | |
refreshApex(this.wiredTemplateRecordValue); | |
this.dispatchEvent( | |
new ShowToastEvent({ | |
title: 'Success', | |
message: 'Template updated', | |
variant: 'success' | |
}) | |
); | |
}) | |
.catch(error => { | |
this.dispatchEvent( | |
new ShowToastEvent({ | |
title: 'Error creating record', | |
message: error.body.message, | |
variant: 'error' | |
}) | |
); | |
}); | |
} | |
updateTemplateApex(event) { | |
const methodParams = { | |
recordId: this.recordId, | |
templateBody: 'Updated with Apex' | |
}; | |
updateTemplateRecord(methodParams) | |
.then(() => { | |
refreshApex(this.wiredTemplateRecordValue) | |
.then(() => { | |
// do something with the refreshed data in this.opptiesOverAmount | |
}); | |
getRecordNotifyChange([{recordId: this.recordId}]); | |
this.dispatchEvent( | |
new ShowToastEvent({ | |
title: 'Success', | |
message: 'Template updated', | |
variant: 'success' | |
}) | |
); | |
}) | |
.catch((error) => { | |
this.dispatchEvent( | |
new ShowToastEvent({ | |
title: 'Error updating record', | |
message: error.errorCode + ', ' + error.body.message, | |
variant: 'error' | |
}) | |
); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment