ä»å¹´ã®æè¡ç³»ã¢ããã³ãã«ã¬ã³ãã¼ãããæçµæ¥ã§ããã
12æ1æ¥ããã¯ãªã¹ãã¹ã®25æ¥ã¾ã§ãæ¯æ¥æ稿ãããã¢ããã³ãã«ã¬ã³ãã¼ã®è¨äºãæ稿ããã¦ãã¾ããããé¢ç½ããåå¼·ã«ãªããè¨äºã¯è¦ã¤ãããã¾ããã§ããããï¼
ããããå¹´æ«å¹´å§ã®ãä¼ã¿ã«ã¢ããã³ãã«ã¬ã³ãã¼ã®è¨äºããã£ããèªã¿è½ã£ã¦ã¿ã¦ã¯ãããã§ããããï¼
ã¯ããã«
ãã¦ãä»åã¯Lightning Web Componentã¨Apexã¯ã©ã¹ã®ãã¼ã¿æ¸¡ãã«ã¤ãã¦ãWebAPIã£ã½ãå®è£ ãããç§ãæ±ãã¦ããé¡æã2ã¤ããã¾ãã
1ã¤ç®ã¯ã@wireããã¾ã仲è¯ããªãããã«ãªãç§ã¨ãã¦ã¯ãJSONå½¢å¼ã§ãã¼ã¿ãããã¨ããããã¨ããé¡æã§ãã
ããã¦ãããä¸ã¤@AuraEnabledã¯ããã±ã¼ã¸ã³ã°ããã¨ããã±ã¼ã¸ãã¤ã³ã¹ãã¼ã«ããçµç¹ã§ã¯APIãå ¬éããã¦ãã¾ã£ãããæ°è»½ã«è¿½å ã»åé¤ã§ããªãã£ããããã®ã§ãªãã¨ããããé¡æã§ãã
2ã¤ã®é¡æãå¶ããè¨è¨ææ³
ã¾ãã¯ãJSONå½¢å¼ã§ãã¼ã¿ãããã¨ããããããLightning Web Componentå´ã§ã¯JSON.stringify() / parse() ããApexã¯ã©ã¹å´ã§ã¯JSON.serialize() / deserialize()ãå©ç¨ãã¾ãã
次ã«ãï¼ AuraEnabledã®åé¡ã«ã¤ãã¦ã¯ãLightning Web Componentå´ããã³ã¼ã«ãããããã®ã¡ã½ãããä½æããããããåå¦çã«æ¯ãåãããã¨ã§å¦çãã¨ã«ã¡ã½ãããä½æããªãã¦ããããªãã¾ãã
Apexã¨LWCã®å®è£
ä»åã¯ããã¿ã³æ¼ä¸æã«å ¥åããã¯ã¹ã«å ¥åãããæååãéä¿¡ãã¦ãçµæãããã¹ã表示ããç°¡åãªãµã³ãã«ã«ãªãã¾ãã
Lightning Web Componentå´
Lightning Web Componentå´ã®ã½ã¼ã¹ã³ã¼ãã¯ä¸è¨ã®ããã«ãªãã¾ãã
ãã¼ã¿éä¿¡æã«ã¯ãJSON.stringify()ã使ã£ã¦éä¿¡ãããã¼ã¿ãJSONå½¢å¼ã«å¤æãã¦ãApexå´ã®ã¡ã½ãããã³ã¼ã«ãã¦ãã¾ãã
lwcCallApex.html
<template> <lightning-card title="Call Apex methods with JSON"> <div class="slds-var-p-around_medium "> <lightning-input type="text" label="Message" value={message} onchange={changeHandler}></lightning-input> <lightning-button variant="brand" label="Submit" onclick={clickSubmit}></lightning-button> <p class="slds-var-p-horizontal_small">Result: {result}</p> </div> </lightning-card> </template>
lwcCallApex.js
import { LightningElement, api } from 'lwc'; import callMethod from '@salesforce/apex/CallApex.callMethod'; export default class LwcCallApex extends LightningElement { message = "Lightning Web Component"; result = "(No result)"; clickSubmit(event) { const request = {}; request.path = "/submit"; request.param = this.message; callMethod({ request : JSON.stringify(request) }) .then(result => { if(res.status == 200) { this.result = JSON.parse(result).body; } else { this.result = "Error"; } }) .catch(error => { console.log(error); this.result = error; }) } changeHandler(event) { this.message = event.target.value; } }
Apexã¯ã©ã¹å´
Apexã¯ã©ã¹å´ã®ã½ã¼ã¹ã³ã¼ãã¯ä¸è¨ã®ããã«ãªãã¾ããLightning Web Componentå´ããéä¿¡ããã¦ããJSONå½¢å¼ã®ãã¼ã¿ãåãåããJSON.deserialize() ã§Apexã¯ã©ã¹ã«å¤æãã¦ãã¾ãã
å¦çã®æ¯ãåãã«ã¯ãWeb APIã£ã½ããã¹ãæå®ãã¦ãswitchæã§å¦çãæ¯ãåãã¦ãã¾ãã
çµæã«ã¤ãã¦ã¯çµæãæ ¼ç´ããApexã¯ã©ã¹(Response)ãçæãã¦ãJSON.serialize()ã§JSONå½¢å¼ã®ãã¼ã¿ã«å¤æãã¦ãã¾ãã
CallApex.cls
public with sharing class CallApex { @AuraEnabled public static String callMethod(String request) { String body = ''; try { Request req = (Request)JSON.deserialize(request, Request.class); switch on req.getPath() { when '/submit' { body = '[Submit] ' + req.getParam(); } } Response res = new Response(200, body); return JSON.serialize(res); } catch(Exception e) { throw new AuraHandledException(e.getMessage()); } } public class Request { String path; String param; public Request(String path, String param) { this.path = path; this.param = param; } public String getPath() { return path; } public String getParam() { return param; } } public class Response { Integer status; String body; public Response(Integer status, String body) { this.status = status; this.body = body; } public Integer getStatus() { return status; } public String getBody() { return body; } } }
ã¾ã¨ã
以ä¸ã§ãLightning Web Componentå´ã¨Apexã¯ã©ã¹å´ã®ããã¨ããJSONå½¢å¼ã®ãã¼ã¿ã«ãã¦åã渡ããã¨ãã§ããWeb APIã®ãããªãã¹ãå¼æ°ã¨ãã¦æ¸¡ããã¨ã§å¦çãã¨ã«ã¡ã½ãããå¢ãããã¨ãªãå¦çã追å ã§ããããã«ãªãã¾ããã
ä»å¾ã®æ¡å¼µã¨ãã¦ã¯ãã¼ã¿åã渡ãå¦çããã¬ã¼ã ã¯ã¼ã¯åãã¦ãããæ¨æºåã§ãããã®ã«ãããã¨èãã¦ãã¾ãã
ãããã«
ä»åã¯ãLightning Web Componentã¨Apexã¯ã©ã¹ã®ãã¼ã¿æ¸¡ããJSONå½¢å¼ã«ãã¤ã¤ãApexã¯ã©ã¹å´ã§å¦çãæ¯ãåããä»çµã¿ãå°å ¥ããå®è£ ãç´¹ä»ãã¾ããã
YouTubeãã£ã³ãã«ãmigration talksãã§ã¯Salesforceéçºé¢é£ã®åç»ãã¢ãããã¼ããã¦ãã¾ãã
æè¿ã§ã¯Lightning Web Componentãå¦ç¿ãã¦å 容ã解説ããåç»ãYouTubeã«æ稿ãã¦ãã£ã¦ãã¾ããåºç¤ã§ã¯éçºç°å¢ã®ã»ããã¢ããã¾ã§ã®åç»ãã¢ãããã¼ããã¦ãã¾ããããããã¯Lightning Web Componentãå®è£ ãã¦ããããã®æ¹æ³ã«ã¤ãã¦ç´¹ä»ãã¦ããããã¨æãã¾ãã
ãã²ãã£ã³ãã«ç»é²ãã¦ããã ããã¨å±ã¿ã«ãªãã¾ãã
æå¾ã«ãªãã¾ããããã®æ稿ã¯Salesforce Advent Calendar 2022 カレンダー2ãããã³チームスピリット Advent Calendar 2022 ã®ç¬¬25æ¥ç®ã®æ稿ã¨ãªãã¾ãã
ãã®è¨äºã¯ã note.com ã«æ稿ããè¨äºã®è»¢è¼ã«ãªãã¾ãã
note.com