You need to sign in to do that
Don't have an account?
Creating a user during a test?
Hi,
I have written a process that will automatically deactivate a user if they have not logged in since 90 days. When testing the code I need to test for a new user who has nevered logged in at all (log in date = null). I try to insert a user in my test script but it has an issue about reference for the profileId. I am using a valid 18 chararcter profileId but I keep getting the exception.
In addition, since you cannot delete a user once it is created will the user that I create in during the test script not get committed or is this the reason I am getting the error?
THE ERROR IS:
System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []
THE CODE IS:
@isTest private class DeactivateUsers_TEST { static testMethod void Test() { Test.startTest(); User u = new User(); u.FirstName = 'MyTest'; u.LastName = 'MyTest'; u.email = 'mytest@mytest.com'; u.alias = 'mytes'; u.communityNickname = 'mytest'; u.username = 'mytest@mytest.com'; u.EmailEncodingKey = 'ISO-8859-1'; u.LanguageLocaleKey = 'en_US'; u.LocaleSidKey = 'en_US'; u.TimeZoneSidKey = 'America/New_York'; u.ProfileId = '00e30000000c51xAAA'; insert u; DeactivateUsers d = new DeactivateUsers(); d.checkUsers(); Test.stopTest(); } }
Try out the sample code given below :
Account accInd = new account(name='individual');
insert accInd ;
Contact contact = new Contact(LastName = 'hello',FirstName = 'World',Email = 'a@dolby.com',AccountId = accInd.Id);
insert contact;
Profile p = [select id from profile where name = 'Volunteer'];
User u = new User(alias = 'standt',
email='a@dolby.com',
emailencodingkey='UTF-8',
FirstName = 'hello',
lastname='Testing',
languagelocalekey='en_US',
localesidkey='en_US',
IsPortalSelfRegistered = true,
profileid = p.Id,
ContactId = contact.id,
timezonesidkey='America/Los_Angeles',
CommunityNickname = 'Tom',
isActive = true,
username='a@dolby.com');
insert u;
Hope this helps.
All Answers
You can try the below code to insert an user
Thanks for the suggestion. I will try this but I think it will be an issue because the user I am testing for is a non admin user profile type that I need. My process never deactivates an admin; therefore, I don't think it will even partake in my code. In my query of my main FOR LOOP it has where profile.name <.> "System Administrator". Make sense?
I tried the code but I get the following error:
Invalid field UserName for SObject User at line 25 column 49
Make no sense because Username is a valid field name!!!???
Can you post your entire code, there might be a small mistake.
The code will not compile and the error is:Error: Compile Error: Invalid field username for SObject User at line 27 column 49
I even tried uncommenting my code and commenting your suggested code and it compiles but when it runs I get this error:
Class.DeactivateUsers_TEST.Test: line 7, column 18 External entry point
17:20:8.815|SOQL_EXECUTE_BEGIN|[7,18]|Aggregations:0|select id from profile where Name = 'System Administrator'
But after I manually typed 'System Administrator' in my code ran but I would be curious to know why your code is saying the username is not valid?
I appreciate your help.
Try out the sample code given below :
Account accInd = new account(name='individual');
insert accInd ;
Contact contact = new Contact(LastName = 'hello',FirstName = 'World',Email = 'a@dolby.com',AccountId = accInd.Id);
insert contact;
Profile p = [select id from profile where name = 'Volunteer'];
User u = new User(alias = 'standt',
email='a@dolby.com',
emailencodingkey='UTF-8',
FirstName = 'hello',
lastname='Testing',
languagelocalekey='en_US',
localesidkey='en_US',
IsPortalSelfRegistered = true,
profileid = p.Id,
ContactId = contact.id,
timezonesidkey='America/Los_Angeles',
CommunityNickname = 'Tom',
isActive = true,
username='a@dolby.com');
insert u;
Hope this helps.
Hi Tom,
I am working on creating a similar process, in which a user would be de-activated after 60 days without any logins.
I would really appreciate if you could share your code so that it would help me in getting started.
Thanks!
It isn't the cleanest of code but it works. I never completely tested the feature that emails the users that they are close to being deactivated because it was determined that we did not want to give the users any notice because it would defeat the purpose because they would simply log in once. You need to create an email template or remove the reference to the template in the code. It is a static email template that simply informs the user they are about to be deactivated if they do not log into SF.
Basically if you schedule the code as shown below the users do not get a notice. All admins are excluded from being deactivated and a custom boolean field name No_Auto_Deactivate__c was added to the User object so you can exclude specific users from ever being deactivated.
In addition, you need to add a boolean field named Deactivate_Recipient__c to the User object. This field controls who receives the email summary that shows who was deactivated and is about to be activate in 30 days. Typically admins would have this field checked.
I didn't write the best code but it works. Good Luck
Thanks Tom!
I'll give this a try..Appreciate your help.:smileyhappy: