Skip to content

Instantly share code, notes, and snippets.

@matt-newell
Created April 4, 2019 14:15
Show Gist options
  • Save matt-newell/efd663474be0a4fb4329a611d66410dc to your computer and use it in GitHub Desktop.
Save matt-newell/efd663474be0a4fb4329a611d66410dc to your computer and use it in GitHub Desktop.
Dynamic sObject LWC
const ACCOUNT_FIELDS = [
'Account.Name',
'Account.LastModifiedDate'
];
const ACCOUNT_OPTIONAL_FIELDS = [
];
const CONTACT_FIELDS = [
'Contact.Name',
'Contact.Email',
'Contact.LastModifiedDate'
];
const CONTACT_OPTIONAL_FIELDS = [
];
const LEAD_FIELDS = [
'Lead.Name',
'Lead.LastModifiedDate'
];
const LEAD_OPTIONAL_FIELDS = [
];
export {ACCOUNT_FIELDS, ACCOUNT_OPTIONAL_FIELDS, CONTACT_FIELDS, CONTACT_OPTIONAL_FIELDS, LEAD_FIELDS, LEAD_OPTIONAL_FIELDS};
<template>
<c-status-account record-id={recordId} if:true={showAccount}></c-status-account>
<c-status-contact record-id={recordId} if:true={showContact}></c-status-contact>
<c-status-lead record-id={recordId} if:true={showLead}></c-status-lead>
</template>
import { LightningElement, api } from 'lwc';
import { loadStyle } from 'lightning/platformResourceLoader';
import staticResource from '@salesforce/resourceUrl/STATIC_RESOURCE';
export default class SobjectContainer extends LightningElement {
@api recordId;
@api objectApiName;
get showAccount(){
return this.objectApiName === 'Account' ? true : false;
}
get showContact(){
return this.objectApiName === 'Contact' ? true : false;
}
get showLead(){
return this.objectApiName === 'Lead' ? true : false;
}
// StaticResource: css and fonts assets. The CSS
// points to a relative path for assets
// i.e.
// @font-face {
// font-family: "custom-font";
// src:url("fonts/custom-font.eot");
// src:url("fonts/custom-font.eot?#iefix") format("embedded-opentype"),
// url("fonts/custom-font.woff") format("woff"),
// url("fonts/custom-font.ttf") format("truetype"),
// url("fonts/custom-font.svg#custom-font") format("svg");
// font-weight: normal;
// font-style: normal;
// }
renderedCallback(){
loadStyle(this, `${staticResource}/assets/styles.css`).then();
}
}
import { LightningElement, api, track, wire } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import {ACCOUNT_FIELDS, ACCOUNT_OPTIONAL_FIELDS} from 'c/services';
export default class StatusAccount extends LightningElement {
@api recordId;
@api objectApiName;
@api sObject;
@wire(getRecord, {recordId: '$recordId', fields: ACCOUNT_FIELDS, optionalFields: ACCOUNT_OPTIONAL_FIELDS})
wireRecord({error, data}){
if(error){
console.log('Dude: ', error);
}else if(data){
this.sObject = data;
}
}
}
@matt-newell
Copy link
Author

Dynamically render LWC components based on sObject type and load custom fonts using a static resource with assets.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment