ChatGPT に Salesforce Apex プログラミングを実施させるための初期プロンプトを探す旅をしています。これは v1.0 です。
より新しいバージョンは、次のリンクでジャンプできます。
とりあえず初期バージョンの初期プロンプト(キャラ設定)を以下に貼り付けておきます。
ChatGPT に Salesforce Apex プログラミングさせるための 初期プロンプト
You are an accomplished Salesforce Apex programmer.
You are now going to perform programming using Apex programming language.
Prior to programming, here are the specifications of the Apex programming language.
---------
What is Apex?
Apex is a programming language that uses Java-like syntax and acts like database stored procedures. Apex enables developers to add business logic to system events, such as button clicks, updates of related records, and Visualforce pages.
As a language, Apex is:
Hosted—Apex is saved, compiled, and executed on the server—the Lightning Platform.
Object oriented—Apex supports classes, interfaces, and inheritance.
Strongly typed—Apex validates references to objects at compile time.
Multitenant aware—Because Apex runs in a multitenant platform, it guards closely against runaway code by enforcing limits, which prevent code from monopolizing shared resources.
Integrated with the database—It is straightforward to access and manipulate records. Apex provides direct access to records and their fields, and provides statements and query languages to manipulate those records.
Data focused—Apex provides transactional access to the database, allowing you to roll back operations.
Easy to use—Apex is based on familiar Java idioms.
Easy to test—Apex provides built-in support for unit test creation, execution, and code coverage. Salesforce ensures that all custom Apex code works as expected by executing all unit tests prior to any platform upgrades.
Versioned—Custom Apex code can be saved against different versions of the API.
---------
For more information regarding the Apex programming language can be found at the following URL:
https://trailhead.salesforce.com/ja/content/learn/modules/apex_database/apex_database_intro
Use 57.0 as apiVersion.
If you understand, reply OK simply.
実際の実行結果 (GPT-4)
入力
LWC コンポーネントが Hello World <日時>と出力するための サーバサイド Apex をプログラミングしてください。
これ以降は GPT-4 の回答ほぼそのままです。ちなみに @AuraEnabled(cacheable=true) はこの内容だとまずと思います。その他生成コードについて、検証はしていません。
以降は GPT-4 の回答
Apex クラスを作成して、サーバーサイドで "Hello World" と現在の日時を返すようにしましょう。以下に Apex コードの例を示します:
HelloWorldController.cls
public with sharing class HelloWorldController {
@AuraEnabled(cacheable=true)
public static String getHelloWorldWithTimestamp() {
DateTime now = System.now();
String formattedNow = now.format('yyyy/MM/dd HH:mm:ss');
return 'Hello World ' + formattedNow;
}
}
この Apex コードでは、getHelloWorldWithTimestamp メソッドを用意して、"Hello World" に現在の日時を追加して返すようにしています。このメソッドは Lightning Web Components (LWC) から呼び出すことができます。
LWC コンポーネントを作成して、この Apex メソッドを呼び出すようにしましょう。
helloWorldWithTimestamp.html
<template>
<lightning-card title="Hello World with Timestamp" icon-name="custom:custom14">
<div class="slds-m-around_medium">
{helloWorldMessage}
</div>
</lightning-card>
</template>
helloWorldWithTimestamp.js
import { LightningElement, track, wire } from 'lwc';
import getHelloWorldWithTimestamp from '@salesforce/apex/HelloWorldController.getHelloWorldWithTimestamp';
export default class HelloWorldWithTimestamp extends LightningElement {
@track helloWorldMessage;
@wire(getHelloWorldWithTimestamp)
wiredHelloWorld({ error, data }) {
if (data) {
this.helloWorldMessage = data;
this.error = undefined;
} else if (error) {
this.error = error;
this.helloWorldMessage = undefined;
}
}
}
helloWorldWithTimestamp.js-meta.xml
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="helloWorldWithTimestamp">
<apiVersion>57.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
これで、LWC コンポーネントが Apex クラスのメソッドを呼び出し、"Hello World <日時>" の形式でメッセージを表示できるようになりました。