Created
April 4, 2019 14:15
-
-
Save matt-newell/efd663474be0a4fb4329a611d66410dc to your computer and use it in GitHub Desktop.
Dynamic sObject LWC
This file contains hidden or 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
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}; |
This file contains hidden or 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
<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> |
This file contains hidden or 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 } 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(); | |
} | |
} |
This file contains hidden or 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
<h1>Allo</> |
This file contains hidden or 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, 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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dynamically render LWC components based on sObject type and load custom fonts using a static resource with assets.