Lightning Web ã³ã³ããŒãã³ãã¢ã¯ã·ã§ã³ã«ã€ããŠç¥ã
åŠç¿ã®ç®ç
ãã®åå ãå®äºãããšã次ã®ããšãã§ããããã«ãªããŸãã
- ãŠãŒã¶ãŒå
¥åã«åºã¥ããŠé
ç®ã«å
¥åãããã£ãŒãããã¯ã¡ãã»ãŒãžããŠãŒã¶ãŒã«è¡šç€ºãã Lightning Web ã³ã³ããŒãã³ããã¢ã¯ã·ã§ã³ãäœæããã
- JavaScript ã«ã¹ã¿ã ãã¿ã³ã®æ©èœã® Lightning 察å¿ã®ä»£æ¿æ³ãžã®ç§»è¡ãéå§ããã
Lightning Web ã³ã³ããŒãã³ãã¢ã¯ã·ã§ã³: ã¹ããŒããã¹ããŒãã£ãæè»
ãããŸã§ãLightning Experience ãš Salesforce Classic ã®äž¡æ¹ã§æ©èœããJavaScript ãã¿ã³ã®åªããä»£æ¿æ³ãšãªãããã€ãã®ãœãªã¥ãŒã·ã§ã³ãèŠãŠããŸããããã ãããããã宣èšåãããã°ã©ã åã®ãœãªã¥ãŒã·ã§ã³ã§ã¯ãã¹ãŠã®äœ¿çšäºäŸã«å¯ŸåŠã§ããªãããšãåœç€Ÿã¯èªèããŠããŸãã幞ãããããã®ãœãªã¥ãŒã·ã§ã³ä»¥å€ã«ãææ®µããããŸããã§ã¯ããã§ãLightning Web ã³ã³ããŒãã³ããã³ãŒã«ããã¯ã€ãã¯ã¢ã¯ã·ã§ã³ãLightning Web ã³ã³ããŒãã³ãã¢ã¯ã·ã§ã³ãã玹ä»ããŸãã
Lightning Web ã³ã³ããŒãã³ãã¢ã¯ã·ã§ã³ã¯ãæ¢åã® Lightning Web ã³ã³ããŒãã³ããã¬ãŒã ã¯ãŒã¯äžã«æ§ç¯ãããŸããæ¢åã® Lightning Web ã³ã³ããŒãã³ããç°¡åã«ã¢ã¯ã·ã§ã³ã«å€æããŠãLightning Experience ã§äœ¿çšããããšãã§ããŸãã
Lightning Web ã³ã³ããŒãã³ããã¢ã¯ã·ã§ã³ãšããŠèµ·åå¯èœã«ããããã«ãã³ã³ããŒãã³ãã®ã¡ã¿ããŒã¿èšå® XML ãã¡ã€ã«ã«ã¿ãŒã²ãããšã㊠lightning__RecordAction ã远å ããtargetConfigs ã« actionType ãæå®ããŸããSalesforce ã§å®çŸ©ãããã¢ãŒãã«ãŠã£ã³ããŠã«ã¢ã¯ã·ã§ã³ã衚瀺ããã«ã¯ãactionType ã ScreenAction ã«èšå®ããŸããã¢ãŒãã«ãªãã§ã¢ã¯ã·ã§ã³ãå®è¡ããããŸãã¯ã«ã¹ã¿ã ã¢ãŒãã«ãŠã£ã³ããŠãå®çŸ©ããã«ã¯ãactionType ã Action ã«èšå®ããŸãã
Lightning Web ã³ã³ããŒãã³ãã¢ã¯ã·ã§ã³çšã®ãã 1 ã€ã®æçšãªããããã£ã¯ recordId ã§ãLightning ã¬ã³ãŒãããŒãžããã®èµ·åæã«ãã®ã¬ã³ãŒãã³ã³ããã¹ããã³ã³ããŒãã³ãã«æäŸããŸããã¬ã³ãŒãã®ããŒã¿ãŸãã¯ã¡ã¿ããŒã¿ã«ã¢ã¯ã»ã¹ããã«ã¯ãã³ã³ããŒãã³ãã® JavaScript ã¯ã©ã¹ã§ã@api ãã³ã¬ãŒã¿ãŒã䜿çšã㊠recordId å
¬éããããã£ãäœæããŸãã
ã§ã¯ãLightning Web ã³ã³ããŒãã³ãã¢ã¯ã·ã§ã³ã詳ããèŠãŠãããŸããããæ¬¡ã¯ãJavaScript ãã¿ã³ã®æ©èœã®ãã¡ãLightning Web ã³ã³ããŒãã³ãã¢ã¯ã·ã§ã³ã§ä»£çšã§ãããã®ã«ã€ããŠèª¬æããŸããããšãã°ããŠãŒã¶ãŒå ¥åã«åºã¥ããŠé ç®ã«å ¥åãããŠãŒã¶ãŒå ¥åã«åºã¥ããŠé ç®ã«å ¥åããããŒã¿å ¥åäžã«ãã£ãŒãããã¯ã¡ãã»ãŒãžã衚瀺ãããªã©ã§ãã
ãŠãŒã¶ãŒå ¥åã«åºã¥ãé ç®ãžã®å ¥åããã³ãã£ãŒãããã¯ã¡ãã»ãŒãžã®ãŠãŒã¶ãŒãžã®è¡šç€º
çŸåšãJavaScript ãã¿ã³ã䜿çšããŠãããŒã¿ãæ€èšŒãŸãã¯æäœãããŠãŒã¶ãŒãã¬ã³ãŒãã䜿ã£ãŠäœæ¥ãããšãã«ãã£ãŒãããã¯ãæç€ºã衚瀺ããŠãããšããŸãããã®äŸã§ã¯ããŠãŒã¶ãŒãã¬ã³ãŒããäœæãŸãã¯æŽæ°ããåã«ãLightning Web ã³ã³ããŒãã³ãã§ããŒã¿ãããã«ç°¡åã«æ€èšŒãŸãã¯æäœã§ãããã瀺ããŸãã
ãµã³ãã«çµç¹ã«ãããŸããŸãªç ç©¶ãããžã§ã¯ãã®è¿œè·¡ã«äœ¿çšãã Case Study (äºäŸ) ãšããã«ã¹ã¿ã ãªããžã§ã¯ããäœæããŸããããŸããæ°ãããã¹ããŠãŒã¶ãŒãäºäŸã«è¿œå ããååãšã¡ãŒã«ã¢ãã¬ã¹ãååŸã§ããããã«ããã¹ããŠãŒã¶ãŒã«ã¹ã¿ã ãªããžã§ã¯ããçšæãããŠããŸããã¡ãŒã«ã¢ãã¬ã¹ã¯åœç€Ÿã®äž»èŠãªé£çµ¡ææ®µã§ãããããæ£ããå ¥åãããããã«ããããšèããŠããŸãããŸãããã¹ããŠãŒã¶ãŒã®å¿åæ§ã確ä¿ããããã«ãäºäŸã®åãŠãŒã¶ãŒã«äžæã®ããã¯ããŒã ãäœæããããšã«ããŸãã
ååé ç®ãšã¡ãŒã«ã¢ãã¬ã¹é ç®ã衚瀺ããŠãã¡ãŒã«ã¢ãã¬ã¹ãæ€èšŒããåãšä¹±æ°ãçµã¿åãããŠããã¯ããŒã ãèªåçæãã Lightning Web ã³ã³ããŒãã³ãã¢ã¯ã·ã§ã³ãäœæããŠãããŸãã
ãã® Lightning Web ã³ã³ããŒãã³ãã¢ã¯ã·ã§ã³ãã³ãŒã«ããã³ã³ããŒãã³ãã®ã³ãŒãã¯ã次ã®ãšããã§ãã
-
createUser.html: ã¢ã¯ã·ã§ã³ãéãããšãã«è¡šç€ºããã Lightning Web ã³ã³ããŒãã³ãã®æ§é ãUI å®è£
(ããã¹ãé
ç®ããã¿ã³ãã¢ã¯ã·ã§ã³ã®ã¿ã€ãã«ãªã©) ãå«ãŸããŸãã
-
createUser.js: ã³ã³ããŒãã³ããšã衚瀺ã©ã€ããµã€ã¯ã«ãããã¹ãå
¥åããã¿ã³ã®ã¯ãªãã¯ãªã©çºçãããã¹ãŠã® UI ã€ãã³ãããªã¹ã³ããã³ã³ãããŒã©ãŒãJavaScript ã¡ãœããã¯ãå
¥åãããŒã¿ãã€ãã³ããç¶æ
ãžã®å€æŽãªã©ã®åŠçãå®çŸ©ããŠãã³ã³ããŒãã³ããåäœãããŸãã
-
createUser.js-meta.xml: Lightning Experience ã§ã®äœ¿çšãç®çãšããã³ã³ããŒãã³ãã®èšèšèšå®ãªã©ãã³ã³ããŒãã³ãã®ã¡ã¿ããŒã¿ãå®çŸ©ããã³ãŒãã
createUser.html
<template>
<lightning-quick-action-panel header="Add Test User to Case Study">
<lightning-input
type="text"
data-field="firstName"
label="User's First Name"
onblur={updateNickName}
required
></lightning-input>
<lightning-input
type="text"
data-field="lastName"
label="User's Last Name"
required
></lightning-input>
<lightning-input
type="text"
data-field="nickName"
label="Nickname"
required
></lightning-input>
<lightning-input
type="email"
data-field="email"
label="User's Email"
onblur={handleEmailBlur}
required
></lightning-input>
<lightning-input
type="password"
data-field="password"
label="User's Password"
onblur={handlePasswordBlur}
required
></lightning-input>
<div slot="footer">
<lightning-button
variant="neutral"
label="Cancel"
onclick={handleCancel}
></lightning-button>
<lightning-button
variant="brand"
label="Save User"
onclick={handleSave}
></lightning-button>
</div>
</lightning-quick-action-panel>
</template>createUser.js
import { LightningElement, api } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { CloseActionScreenEvent } from 'lightning/actions';
import { createRecord } from 'lightning/uiRecordApi';
import TEST_USER_OBJECT from '@salesforce/schema/Test_User__c';
import NAME_FIELD from '@salesforce/schema/Test_User__c.Name';
import PASSWORD_FIELD from '@salesforce/schema/Test_User__c.Password__c';
import EMAIL_FIELD from '@salesforce/schema/Test_User__c.Email__c';
import NICKNAME_FIELD from '@salesforce/schema/Test_User__c.Nickname__c';
import CASE_STUDY_FIELD from '@salesforce/schema/Test_User__c.Case_Study__c';
export default class CreateUser extends LightningElement {
@api recordId;
async handleSave() {
// query form elements
const firstNameEl = this.template.querySelector('[data-field="firstName"]');
const lastNameEl = this.template.querySelector('[data-field="lastName"]');
const nickNameEl = this.template.querySelector('[data-field="nickName"]');
const emailEl = this.template.querySelector('[data-field="email"]');
const passwordEl = this.template.querySelector('[data-field="password"]');
// check for errors on form elements (custom or otherwise)
const isFirstNameValid = firstNameEl.reportValidity();
const isLastNameValid = lastNameEl.reportValidity();
const isNickNameValid = nickNameEl.reportValidity();
const isPasswordValid = passwordEl.reportValidity();
const isEmailValid = emailEl.reportValidity();
if(!isFirstNameValid || !isLastNameValid || !isNickNameValid || !isPasswordValid || !isEmailValid) {
return;
}
const recordInput = {
apiName: TEST_USER_OBJECT.objectApiName,
fields: {
[NAME_FIELD.fieldApiName]: `${firstNameEl.value} ${lastNameEl.value}`,
[PASSWORD_FIELD.fieldApiName]: passwordEl.value,
[EMAIL_FIELD.fieldApiName]: emailEl.value,
[NICKNAME_FIELD.fieldApiName]: nickNameEl.value,
[CASE_STUDY_FIELD.fieldApiName]: this.recordId,
}
};
try {
await createRecord(recordInput);
this.dispatchEvent(
new ShowToastEvent({
title: 'Success!',
message: 'The test user has been created.',
variant: 'success'
})
);
this.dispatchEvent(new CloseActionScreenEvent());
} catch (error) {
new ShowToastEvent({
title: 'Error creating the test user, try again...',
message: error.body.message,
variant: 'error'
});
}
}
updateNickName() {
const firstName = this.template.querySelector('[data-field="firstName"]').value;
const nickNameEl = this.template.querySelector('[data-field="nickName"]');
if(firstName && !nickNameEl.value) {
const today = new Date();
nickNameEl.value = firstName + today.valueOf(today)
}
}
handleCancel() {
this.dispatchEvent(new CloseActionScreenEvent());
}
handlePasswordBlur() {
const passwordEl = this.template.querySelector('[data-field="password"]');
// clear custom errors
passwordEl.setCustomValidity('');
this.validatePassword(passwordEl);
}
validatePassword(input) {
// check lightning-input validity
if(input.reportValidity()) {
// perform custom validation
const value = input.value;
if (value === undefined) {
input.setCustomValidity('You must enter a password.');
} else if (value.length < 7) {
input.setCustomValidity('The password is the wrong length (must be >= 7): ' + value.length);
} else if (value.length > 15) {
input.setCustomValidity('The password is the wrong length (must be <= 15): ' + value.length);
} else if (value.search(/[0-9]+/) === -1) {
input.setCustomValidity('The password must contain at least one number.');
} else if (value.search(/[a-zA-Z]+/) === -1) {
input.setCustomValidity('The password must contain at least one letter.');
}
// display custom validation errors (if any)
input.reportValidity();
}
}
handleEmailBlur() {
const emailEl = this.template.querySelector('[data-field="email"]');
// clear Custom Errors
emailEl.setCustomValidity('');
this.validateEmail(emailEl);
}
validateEmail(input) {
// check lightning-input validity
if (input.reportValidity()) {
const value = input.value;
if (!/@gmail\.com$/.test(value)) {
input.setCustomValidity('Email must be a Gmail account.');
}
// display custom validation errors (if any)
input.reportValidity();
}
}
}createUser.js-meta.xml
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>57.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__RecordAction</target>
</targets>
<targetConfigs>
<targetConfig targets="lightning__RecordAction">
<actionType>ScreenAction</actionType>
</targetConfig>
</targetConfigs>
</LightningComponentBundle>Lightning Web ã³ã³ããŒãã³ãã¢ã¯ã·ã§ã³ãäœæãã
Lightning Web ã³ã³ããŒãã³ããäœæããããã¢ã¯ã·ã§ã³ã«å²ãåœãŠãŸããCase Study ã®ãªããžã§ã¯ã管çèšå®ã§ã[ãã¿ã³ããªã³ã¯ãããã³ã¢ã¯ã·ã§ã³] ã«ç§»åããŠã[æ°èŠã¢ã¯ã·ã§ã³] ãã¯ãªãã¯ãã以äžã®ãã©ã¡ãŒã¿ãŒã䜿çšããŠã¢ã¯ã·ã§ã³ãèšå®ããŸãã
é ç® |
å€ |
|---|---|
ã¢ã¯ã·ã§ã³çš®å¥ |
Lightning Web Component (Lightning Web ã³ã³ããŒãã³ã) |
Lightning Web ã³ã³ããŒãã³ã |
c:createUser |
衚瀺ã©ãã« |
Create Test User (ãã¹ããŠãŒã¶ãŒã®äœæ) |
åå |
Create_Test_User |
次ã«ãæ°ãã Lightning Web ã³ã³ããŒãã³ãã¢ã¯ã·ã§ã³ã Case Study ããŒãžã¬ã€ã¢ãŠãã«è¿œå ããŸãããŠãŒã¶ãŒã [Case Study (äºäŸ)] ã¬ã³ãŒã詳现ããŒãžãããã®ã¢ã¯ã·ã§ã³ãèµ·åãããšãäœæãã Lightning Web ã³ã³ããŒãã³ãã¢ã¯ã·ã§ã³ã衚瀺ãããŸãã

Lightning Web ã³ã³ããŒãã³ãã¢ã¯ã·ã§ã³ã¯ãLightning Experience ã®æªæ¥åœ¢ã®ããã°ã©ã åã¢ã¯ã·ã§ã³ã§ããåœç€Ÿã§ã¯ããã®äžé£ã®ãœãªã¥ãŒã·ã§ã³ããJavaScript ãã¿ã³ã®åªããä»£æ¿æ³ãšããŠèããŠããã ãããšãæåŸ ããŠããŸãã
JavaScript ãã¿ã³ã䜿çšããããŒãããŒã¢ããªã±ãŒã·ã§ã³ãå€çšããŠãããšããæ¹ã«ã¯ãåœç€Ÿã®å€æ°ã®ããŒãããŒããã§ã«ã¢ããªã±ãŒã·ã§ã³ã Lightning ã«ç§»è¡ããã³ã¢ããã°ã¬ãŒããå§ããŠããããšã¯æå ±ã§ããããAppExchange ã§ã¯ãLightning Experience çšã«æŽæ°ãããã¢ããªã±ãŒã·ã§ã³ãå¢ããŠããŸãã
ãªãœãŒã¹
- Lightning Web Components Developer Guide (Lightning Web ã³ã³ããŒãã³ãéçºè ã¬ã€ã): Lightning Web ã³ã³ããŒãã³ãã®æŠèŠ
- Trailhead: ã¯ã€ãã¯ã¹ã¿ãŒã: Lightning Web ã³ã³ããŒãã³ã
- Trailhead: Lightning Web ã³ã³ããŒãã³ãã®åºæ¬
