课题:Apex TestClass内为临时用户赋予权限
在Profile能够给Object设定权限的时候,只需要为临时用户指定Profile就可以了。
现在权限都在PermissionSet内指定,那么我们就需要在测试类的时候,给临时用户赋予PermissionSet
再然后使用System.Run(赋予PermissionSet的临时用户),才能够读取Object的数据。
代码
PermissionSet ps = [SELECT Id FROM PermissionSet WHERE Name = 'PermissionSet名称'];
insert new PermissionSetAssignment(AssigneeId =ユーザID, PermissionSetId = ps.Id);
测试类的代码
String uniqueUserName = 'standarduser' + DateTime.now().getTime() + '@salesforcegogogo.com';
Profile p = [SELECT Id FROM Profile WHERE Name='Profile名称'];
User u = new User(
Alias = 'standt',
Email='standarduser@testorg.com',
EmailEncodingKey='UTF-8',
LastName='Testing',
LanguageLocaleKey='ja_JP',
LocaleSidKey='ja_JP',
ProfileId = p.Id,
TimeZoneSidKey='Asia/Tokyo',
UserName=uniqueUserName);
insert u;
PermissionSet ps = [SELECT Id FROM PermissionSet WHERE Name = 'PermissionSet名称'];
insert new PermissionSetAssignment(AssigneeId = u.Id, PermissionSetId = ps.Id);
System.runAs(u) {
//具体的な処理
}