Accountの削除機能を実現したいと思います。
このレコードIDを取得し、削除できますね。
最終的な画面イメージ
レコード前の「Del」クリックすると、「削除してよろしいでしょうか。」みたいなメッセージを表示されること、
確定の場合は本当の削除いたします、キャンセル(取消)の場合は何もしない。
じゃあ、昨日作ったTest2020の取引先は削除しましょうか。
Visualforce1.vfp
<apex:page controller="Apex1" showHeader="false" sidebar="false">
<script>
function doDeleteJs(accid) {
if (window.confirm("削除してよろしいでしょうか。")) {
doDeleteFn(accid);
}
}
</script>
<style type="text/css">
/* footer右へ表示*/
.footer{
text-align: right;
}
</style>
<apex:sectionHeader subtitle="Account表示一覧" title="Account"/>
<apex:form id="formId2">
<apex:pageMessages id="message2"/>
<apex:pageBlock title="Add Account">
<apex:pageBlockButtons location="both">
<apex:commandButton value="Save" action="{!save}" reRender="pageBlockTable,formId2" />
<apex:commandButton value="Cancel" action="{!cancel}" reRender="pageBlockTable,formId2,pb2" />
</apex:pageBlockButtons>
<apex:pageBlockSection title="Account Details" columns="1" id="pb2">
<apex:inputField value="{!act.name}" />
<apex:inputField value="{!act.Type}"/>
<apex:inputField value="{!act.Industry}"/>
<apex:inputField value="{!act.Phone}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
<apex:form id="formId">
<apex:pageMessages id="message"/>
<apex:actionFunction name="doDeleteFn" action="{!doDelete}" reRender="formId" >
<apex:param name="id" value="" />
</apex:actionFunction>
<apex:pageBlock >
<apex:outputPanel id="showpanel">
<apex:pageBlockTable value="{!accounts}" var="acc" footerClass="footer" id="pageBlockTable">
<apex:column headerValue="Action" width="70px">
<apex:commandLink value="" onclick="" style="color:#015ba7;" id="edit" reRender="formId,message">
Edit
</apex:commandLink>
|
<apex:commandLink value="" onclick="doDeleteJs('{!acc.Id}');" style="color:#015ba7;" id="del" reRender="formId,message,showpanel,buttons,pageBlockTable,foo">
Del
</apex:commandLink>
</apex:column>
<apex:column headerValue="取引先名" >
<apex:outputlink value="https://ap2.salesforce.com/{!acc.id}" target="_blank" >{!acc.Name}</apex:outputlink>
</apex:column>
<apex:column value="{!acc.Type}" />
<apex:column value="{!acc.Industry}" />
<apex:column value="{!acc.Phone}" />
</apex:pageBlockTable>
</apex:outputPanel>
</apex:pageBlock>
</apex:form>
</apex:page>
説明:
①onclick=”doDeleteJs(‘{!acc.Id}’);”
!acc.Id:レコードIDを取得できる、doDeleteJs:JSのメソッド名を呼び出し。
②function doDeleteJs(accid)
「Del」をクリックする時、doDeleteJs(accid)を呼び出し、レコードIDを渡して。
③window.confirm:
https://www.w3schools.com/jsref/met_win_confirm.asp
確定とキャンセルボタンがあります。
④doDeleteFn(accid);
actionFunctionの name=”doDeleteFn” と合わせて、IDも渡す。
⑤<apex:actionFunction name=”doDeleteFn” action=”{!doDelete}” reRender=”formId” >
https://developer.salesforce.com/docs/atlas.ja-jp.pages.meta/pages/pages_compref_actionFunction.htm
AJAX 要求を使用したコントローラの action メソッドを JavaScript コードから直接呼び出すことをサポートするコンポーネントです。
action=”{!doDelete}”:
Apex1の中に「doDelete」メソッドを呼び出し、削除処理すること。
削除した後、reRender=”formId”を通して、Accoun一覧画面をリフレッシュします。
⑥<apex:param name=”id” value=”” />
<apex:actionFunction> の中にある、一緒にname=”id”のパラメータを持っている。
<apex:actionFunction> の間のバインドはパラメータの順序に基づいて行われるため、<apex:param> の順序がコール元の引数リストと一致していることを確認してください。
Apex1.apxc
public with sharing class Apex1 {
public List<Account> accounts; //Account情報
public Account act{get;set;} //新規Account情報用
//コンストラクター
public Apex1() {
act = new Account(); //デフォルト New Accountオブジェクト
}
//変数 accounts のgetメソッド
public List<Account> getAccounts() {
try {
accounts= [select Id,Name,Type,Industry,phone from Account order by createddate desc limit 50 ];
return accounts;
} catch (Exception e) {
ApexPages.addMessages(e);
return null;
}
}
//キャンセル操作、何もない、画面をリフレッシュ
public PageReference cancel() {
return null;
}
//保存ボタン押下する、呼び出しメソッド
public PageReference save() {
ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.INFO, '保存しました。'));
//system.debug('act name:' + act.name + ' = ' + act.accountNumber);
upsert act;
return null;
}
//Account削除メソッド
public void doDelete() {
SavePoint sp = Database.setSavepoint(); //トランザクションの制御
try {
String sfid = ApexPages.currentPage().getParameters().get('id'); //Visualforce1.vfpからのidを取得
List<Account> deleteList= [select Id from Account where Id=:sfid]; //データベースと合わせて
if (deleteList.size() == 0) {
ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR, 'すでに削除されました。'));
} else {
delete deleteList;
ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.INFO, '削除しました。'));
accounts= [select Id,Name,Type,Industry,phone from Account order by createddate desc limit 50 ]; //レコード更新
}
} catch(exception ex) {
system.debug('Error:'+ex.getMessage());
ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR, ex.getMessage()));
Database.rollback(sp); //savepoint の生成時点と同じ状況にデータベースを復元
}
}
}
説明:
①public void doDelete()
Accountの削除処理メソッド、<apex:actionFunction name=”doDeleteFn” action=”{!doDelete}” reRender=”formId” >、 action=”{!doDelete}” と同じです。
②SavePoint sp = Database.setSavepoint();
データベースの保存のポイントを設定し、何かエラーが発生した場合、この時点に戻ることできます。
③String sfid = ApexPages.currentPage().getParameters().get(‘id’);
<apex:param name=”id” value=”” />を渡すidを取得し、クリックしたレコードIDですね。
④List<Account> deleteList= [select Id from Account where Id=:sfid];
データベースの中にSELECTしますので、あるかどうかの確認できます。
⑤deleteList.size() == 0
レコードは存在しない。
⑥delete deleteList; //DML削除操作
ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.INFO, ‘削除しました。’)); //削除した後、画面へのメッセージ提示内容
accounts= [select Id,Name,Type,Industry,phone from Account order by createddate desc limit 50 ]; //削除した後、レコード更新
⑦Database.rollback(sp);
savepoint の生成時点と同じ状況にデータベースを復元、元々設定した②SavePoint sp = Database.setSavepoint();の状態を戻ります。
普通の場合はactionFunctionの実行プロセス:
①apex:commandButtonとかの中にonclickからJSメソッドを呼び出す、パラメータも渡す。
<apex:commandButton id="cmdId" onclick="xxxJs('{!acc.Id}', '{!acc.Name}');" reRender="formId" />
②Jsから、メソッド名同じ、actionFunctionを呼び出す、パラメータも渡す。
function xxxJs(accid, accname) {
xxxFn(accid, accname); //actionFunction のnameと同じ
}
③ActionFunctionのactionはAPEXとのメソッド名と同じ、actionFunctionからクラスメソッドを呼び出す、パラメータも渡す。
<apex:actionFunction name="xxxFn" action="{!doxxx}" reRender="formId">
<apex:param name="aid" value="" />
<apex:param name="aname" value="" />
</apex:actionFunction>
④クラスメソッドの定義、クライアントからのパラメータを取得
public xxxController {
public xxxController() {
}
public void doxxx() {
String sfid = ApexPages.currentPage().getParameters().get('aid');
String sfid = ApexPages.currentPage().getParameters().get('aname');
}
}
以上となります。
何か問題がございましたら、コメントをいただければ幸いです。
Latest posts by zchao (see all)
- Auraでアクションボタン作成して画面のチェックボックス項目一括処理 - 2021年4月12日
- デフォルト項目値を含むレコード作成実例説明(defaultFieldValues) - 2021年1月9日
- Salesforce のノーコード・ローコード開発 - 2020年12月31日
转载请注明:zchao博客之家 » Apex+VisualforceでAccountオブジェクトのCRUDとページングクエリーDーAccountを削除(004)