この記事の目次
Salesforceのnot allowedとは?

Salesforceでnot allowedが出る条件

DMLとは
SalesforceにおいてのDMLとは、データベースに対する検索、更新、削除、登録をするときの指示のことです。Apexという言語でデータベースクラスメソッドやApex DMLステートメントを使用して実行ができます。 データ追加や更新、削除や復元などDMLステートメントで使うことが可能です。insert DMLでは、個別で取引先、取引の代表者など1つ、複数の組織データを追加できます。SQLで例えると、INSERT ステートメントに似ています。 以下サンプルコードです。Account newAcct = new Account(name = 'Acme');
try {
insert newAcct;
} catch (DmlException e) {
// Process exception here
}
update DMLは個別の取引先、取引責任者、請求書の明細など組織のデータを複数で更新が可能です。SQLで例えると、UPDATE ステートメントに似ています。
以下サンプルコードです。
Account a = new Account(Name='Acme2');
insert(a);
Account myAcct = [SELECT Id, Name, BillingCity FROM Account WHERE Id = :a.Id];
myAcct.BillingCity = 'San Francisco';
try {
update myAcct;
} catch (DmlException e) {
// Process exception here
}
DML currently not allowedのサンプルコード
「DML currently not allowed」というエラーが発生するサンプルコードを紹介します。最初に取引先の判定処理をして、結果を画面に出力するサンプルです。判定が正常に行われた場合は、「Checked_ok」、という項目をturuにしてupdateします。 下記、サンプルコードになります。
<!-- Visualforceページ -->
<apex:page controller=""AccountCheckController"">
< apex:outputText value=""{! msg }"" />
</apex:page>
/**
* コントローラクラス.
*/
public class AccountCheckController {
/** 取引先ID */
public String accountId;
/** メッセージ */
public String msg{get; set;}
/**
* コンストラクタ.
*/
public AccountCheckController() {
Map paramMap = ApexPages.currentPage().getParameters();
this.accountId = paramMap.get('id');
// 取引先取得
List accountList = [SELECT Id, Name, Checked_ok FROM Account WHERE Id =: this.accountId];
if (accountList.size() == 0) {
this.msg = '取引先が取得できませんでした。';
return;
}
Account acc = accountList.get(0);
// 判定処理
AccountCheck accountCheck = new AccountCheck();
boolean result = accountCheck.exec();
if (!result) {
this.msg = '判定処理でエラーが発生しました。';
return;
}
// 判定処理が正常であれば、チェック実施済フラグを立てる
acc.Checked_ok = true;
update acc;
this.msg = '判定処理が完了しました!';
}
}
Salesforceのnot allowedの解決方法

<!-- Visualforceページ -->
<!-- 追加部分 -->
<apex:page controller=""AccountCheckController"" action=""{! init }"">
<apex:outputText value=""{! msg }"" />
</apex:page>
/**
* コントローラ.
*/
public class AccountCheckController {
/** 取引先ID */
public String accountId;
/** メッセージ */
public String msg{get; set;}
/**
* 初期表示処理(追加部分).
*/
public void init() {
Map paramMap = ApexPages.currentPage().getParameters();
this.accountId = paramMap.get('id');
// 取引先取得
List accountList = [SELECT Id, Name, Checked_ok FROM Account WHERE Id =: this.accountId];
if (accountList.size() == 0) {
this.msg = '取引先が取得できませんでした。';
return;
}
Account acc = accountList.get(0);
// 判定処理
AccountCheck accountCheck = new AccountCheck();
boolean result = accountCheck.exec();
if (!result) {
this.msg = '判定処理でエラーが発生しました。';
return;
}
// 判定処理が正常であれば、チェック実施済フラグを立てる
acc.Checked_ok = true;
update acc;
this.msg = '判定処理が完了しました!';
}
}
Salesforceを便利に活用しよう
