Loading
Salesforce から送信されるメールは、承認済ドメインからのみとなります続きを読む

ProfileId が Apex 経由で更新されたときのエラー「FIELD_INTEGRITY_EXCEPTION」

公開日: Oct 13, 2022
説明
古いプロファイルのライセンスと新しいプロファイルのライセンスが同じであれば、更新は正常に行われます。

問題を再現するためのサンプルコード
 
list<user> users = [SELECT Id, firstname, lastname, IsActive, ProfileId, UserType FROM User where profileId in :[select id from Profile where name = '<chatter user profile>']]; // これらのユーザーは異なるライセンス (「XYZ ライセンス」) を持っています。 
Profile p = [select id from profile where name='Standard Platform User']; // これらのユーザーは異なるライセンス (「ABC ライセンス」) を持っています。 
for(User u:users){ 
 u.profileId= p.id; 
} 
update users;

結果:FIELD_INTEGRITY_EXCEPTION, You can only reassign this user to a profile of the current license.: [ProfileId]. (FIELD_INTEGRITY_EXCEPTION、このユーザーは現在のライセンスのプロファイル [ProfileId] にのみ再割り当てできます。)

同じライセンスで更新が正常に行われるサンプルコード
 
list<user> users = [SELECT Id,firstname, lastname, IsActive,ProfileId,UserType FROM User where profileId in :[select id from Profile where name = 'Standard User']]; // これらのユーザーは「salesforce ライセンス」を持っています。 
Profile p = [select id from profile where name='Standard Platform User'];// これらのユーザーは「salesforce ライセンス」を持っています。 
for(User u:users){ 
u.profileId= p.id; 
} 
update users;


結果: 正常に更新されました
解決策
これは設計通りの動作です。ユーザープロファイルはユーザーライセンスに依存するため (プロファイル項目はユーザーライセンス項目に依存します)、利用可能なプロファイルは該当するユーザーに選択されたユーザーライセンスに依存します。 実際にコードを通じてそれを変更できるようになれば、まったく新しいセキュリティリスクが発生することになります。 

この問題を回避するには:
  • Apex 経由でユーザーをコピーします。 
  • コピーされた古いユーザーを無効になります。 
  • リストにある新規ユーザーを挿入します。 
 
問題を回避するためのサンプルコード
 
list<user> users = [SELECT Id,firstname, lastname, IsActive,ProfileId,UserType FROM User where profileId in :[select id from Profile where name = 'portal']]; // これらのユーザーは異なるライセンス (「XYZ ライセンス」) を持っています。 
Profile p = [select id from profile where name='community']; // これらのユーザーは異なるライセンス (「ABC ライセンス」) を持っています。 
for(User u:users){ 
u.isactive = false; 
} 
update users; 
list<user> newUserList = new list<user>(); 
for(User u1:users){ 
user NewUser = new user(); 
NewUser.allrequiredFields = u1.allrequiredFields; 
. 
. 
// すべての必須項目を配置します。 
newUserList.add(NewUser); 
} 
insert newUserList;
ナレッジ記事番号

000386999

 
読み込み中
Salesforce Help | Article