You need to sign in to do that
Don't have an account?
EagerToLearn
User trigger won't fire because I can't do an update on the user record in a test
I have an AfterUpdate user trigger. The test class is below but I keep getting the following error at the UPDATE line...
System.DmlException: Update failed. First exception on row 0 with id 00560000001A1zeAAC; first error: INSUFFICIENT_ACCESS_OR_READONLY, Cannot insert a user with this profile: []
I have tried without runAS and with runAs and both give the same error! I need to create an active user then I need to deactive that user which would fire my trigger that is the business requirement.
Any ideas? I thought I found and Idea that asks to allow updating user records in a test (ones that are created by the test itself). Is there a way to resolve this issue? Thanks in advance.
@isTest
public class SM_User_Trigger_Test {
@testSetup static void createUsers() {
Profile pf= [Select Id from profile where Name='Standard User'];
List<User> UserList = new List<User>();
User user = new User();
String orgId=UserInfo.getOrganizationId();
String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000));
String uniqueName=orgId+dateString+RandomId;
User u = new User(firstname = 'Test1',
lastName = 'Tester',
email = 'test1.tester' + '@somecompany' + orgId + '.org',
Username = 'test1.tester' + '@somecompany' + orgId + '.org',
isActive = true,
EmailEncodingKey = 'ISO-8859-1',
Alias = uniqueName.substring(18, 23),
TimeZoneSidKey = 'America/Los_Angeles',
LocaleSidKey = 'en_US',
LanguageLocaleKey = 'en_US',
ProfileId = pf.Id
);
UserList.add(u);
insert UserList;
}
static testMethod void testCase1() {
Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
User u1 = new User(Alias = 'newUser', Email='newuser@testorg.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='newuser@testorg.com');
System.runAs(u1) {
User User = [SELECT id, isActive FROM User LIMIT 1];
System.debug('User: ' + user);
User.isActive = false;
test.startTest();
Update User;
test.stopTest();
}
}
}
System.DmlException: Update failed. First exception on row 0 with id 00560000001A1zeAAC; first error: INSUFFICIENT_ACCESS_OR_READONLY, Cannot insert a user with this profile: []
I have tried without runAS and with runAs and both give the same error! I need to create an active user then I need to deactive that user which would fire my trigger that is the business requirement.
Any ideas? I thought I found and Idea that asks to allow updating user records in a test (ones that are created by the test itself). Is there a way to resolve this issue? Thanks in advance.
@isTest
public class SM_User_Trigger_Test {
@testSetup static void createUsers() {
Profile pf= [Select Id from profile where Name='Standard User'];
List<User> UserList = new List<User>();
User user = new User();
String orgId=UserInfo.getOrganizationId();
String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000));
String uniqueName=orgId+dateString+RandomId;
User u = new User(firstname = 'Test1',
lastName = 'Tester',
email = 'test1.tester' + '@somecompany' + orgId + '.org',
Username = 'test1.tester' + '@somecompany' + orgId + '.org',
isActive = true,
EmailEncodingKey = 'ISO-8859-1',
Alias = uniqueName.substring(18, 23),
TimeZoneSidKey = 'America/Los_Angeles',
LocaleSidKey = 'en_US',
LanguageLocaleKey = 'en_US',
ProfileId = pf.Id
);
UserList.add(u);
insert UserList;
}
static testMethod void testCase1() {
Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
User u1 = new User(Alias = 'newUser', Email='newuser@testorg.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='newuser@testorg.com');
System.runAs(u1) {
User User = [SELECT id, isActive FROM User LIMIT 1];
System.debug('User: ' + user);
User.isActive = false;
test.startTest();
Update User;
test.stopTest();
}
}
}
I run your test code, and i try to debug to find out the resean of the error【INSUFFICIENT_ACCESS_OR_READONLY】.
【Step 1】⇒modifiy code
i modified your select SQOL , code as below.
------------------------------debug code start----------
User User = [SELECT id, isActive,ProfileId FROM User LIMIT 1];
System.debug('User⇒⇒⇒'+User);
------------------------------debug code end----------
【Step 2】⇒output debug log
and i got user below. because the error message is 【first error: INSUFFICIENT_ACCESS_OR_READONLY, Cannot insert a user with this profile: []】
the biggest resean i think it shoub be profile. so let me try to see what this ProfileId is.
User⇒⇒⇒User:{Id=0057F000001bNN8QAM, IsActive=false, ProfileId=00e7F000001fYo0QAE}
【Step 3】⇒ copy ProfileId to URL, and open it with your browser.
【URL】https://ap5.salesforce.com/00e7F000001fYo0QAE
and the page show message below.That is the real resean why error happens
Insufficient Privileges
You do not have the level of access necessary to perform the operation you requested. Please contact the owner of the record or your administrator if access is necessary. For more information, see Insufficient Privileges Errors.
【Step 4】⇒the last step is to modified your soql to search your test data.
use the code below , and rus tests, no error . great.
User User = [SELECT id, isActive,ProfileId FROM User where firstname = 'Test1' LIMIT 1];// this is the test data your created.
i hope it will help you to solve it.
All Answers
I run your test code, and i try to debug to find out the resean of the error【INSUFFICIENT_ACCESS_OR_READONLY】.
【Step 1】⇒modifiy code
i modified your select SQOL , code as below.
------------------------------debug code start----------
User User = [SELECT id, isActive,ProfileId FROM User LIMIT 1];
System.debug('User⇒⇒⇒'+User);
------------------------------debug code end----------
【Step 2】⇒output debug log
and i got user below. because the error message is 【first error: INSUFFICIENT_ACCESS_OR_READONLY, Cannot insert a user with this profile: []】
the biggest resean i think it shoub be profile. so let me try to see what this ProfileId is.
User⇒⇒⇒User:{Id=0057F000001bNN8QAM, IsActive=false, ProfileId=00e7F000001fYo0QAE}
【Step 3】⇒ copy ProfileId to URL, and open it with your browser.
【URL】https://ap5.salesforce.com/00e7F000001fYo0QAE
and the page show message below.That is the real resean why error happens
Insufficient Privileges
You do not have the level of access necessary to perform the operation you requested. Please contact the owner of the record or your administrator if access is necessary. For more information, see Insufficient Privileges Errors.
【Step 4】⇒the last step is to modified your soql to search your test data.
use the code below , and rus tests, no error . great.
User User = [SELECT id, isActive,ProfileId FROM User where firstname = 'Test1' LIMIT 1];// this is the test data your created.
i hope it will help you to solve it.
Thanks for the help -- I will try this out and mark solved when confirmed.
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
Starting with V24, test classes would by default not see any existing non-setup org data. (User is a setup object ^o^)
↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ This is the real resean why error happens
If you modify your code once again. code as below .
------------------------------------------------------------------------
List<User> UserList = [SELECT id, isActive,firstname FROM User];
System.debug('UserList----> ' + UserList);
------------------------------------------------------------------------
And we run The TestClass , let us see what is the UserList is.
System log just output the log below. you can find out , we can read everything of user Object, not only your testdata we created in your setup method.
UserList----> (User:{Id=0057F000001bNN8QAM, IsActive=true, FirstName=Automated}, User:{Id=0057F000000zQUDQA2, IsActive=true, FirstName=yu}, User:{Id=0057F000001bNN7QAM, IsActive=true, FirstName=Integration}, User:{Id=0057F000001bNNBQA2, IsActive=true, FirstName=Security}, User:{Id=0057F000002JIVCQA4, IsActive=true, FirstName=Test1}, User:{Id=0057F000001bNNAQA2, IsActive=true})
Here is all the data we can see . The first one user is system user, we can not see it at all .
User:{Id=0057F000001bNN8QAM, IsActive=true, FirstName=Automated}, ⇒⇒Insufficient Privileges
User:{Id=0057F000000zQUDQA2, IsActive=true, FirstName=yu}, ⇒⇒administrator
User:{Id=0057F000001bNN7QAM, IsActive=true, FirstName=Integration}, ⇒⇒User Integration
User:{Id=0057F000001bNNBQA2, IsActive=true, FirstName=Security}, ⇒⇒User Security
User:{Id=0057F000002JIVCQA4, IsActive=true, FirstName=Test1}, ⇒⇒This is TestData that we created at Setup method.
User:{Id=0057F000001bNNAQA2, IsActive=true}) ⇒⇒Chatter Expert
I hope it will help you to understand. ^o^