function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Eager-2-LearnEager-2-Learn 

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();
    }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Pradeep_NavatarPradeep_Navatar

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

hisrinuhisrinu

You can try the below code to insert an user

 

Id PID = [select id from profile where Name = 'System Administrator'].Id;
User u1 = new User(LastName='testuser10', UserName='test123121@test.com', email='test@test.com', Alias='test', TimeZoneSidKey='America/Chicago', LocaleSidKey='en_US', EmailEncodingKey='ISO-8859-1', ProfileId = PID, LanguageLocaleKey='en_US');
insert u1;
Eager-2-LearnEager-2-Learn

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?

Eager-2-LearnEager-2-Learn

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!!!???

hisrinuhisrinu

Can you post your entire code, there might be a small mistake.

Eager-2-LearnEager-2-Learn

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 = 'Sys​tem 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.

 

 

@isTest
private class DeactivateUsers_TEST {
    static testMethod void Test() {
        Test.startTest();
        

/*
        Id PID = [select id from profile where Name = 'System Administrator'].Id;
        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 = PID;
        insert u;
*/



Id PID = [select id from profile where Name = 'Sys​tem Administrator'].Id;
User u1 = new User(LastName='mytest', username​='mytest@mytest.com', email='mytest@mytest.com', 
        Ali​as='mytes', TimeZoneSidKey='America/Chicago', Local​eSidKey='en_US', EmailEncodingKey='ISO-8859-1', 
        Pr​ofileId = PID, LanguageLocaleKey='en_US');
insert u1; 
              

        DeactivateUsers d = new DeactivateUsers();
        d.checkUsers();
        Test.stopTest();
    }
}

 

Pradeep_NavatarPradeep_Navatar

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.

This was selected as the best answer
SalesforceDF2011SalesforceDF2011

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!

Eager-2-LearnEager-2-Learn

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

 

 

global class DeactivateUsers_schedule implements Schedulable{
   global void execute(SchedulableContext SC){
       DeactivateUsers d = new DeactivateUsers(); 
       d.checkUsers(false);
   }
}

 

 

 

public class DeactivateUsers {
    EmailTemplate template;
    //==================================================================
    // Parameter Definition
    // emailUsers: If set to true individual users are sent an alert
    //             email when they are close to being deactivated   
    //==================================================================
    public void checkUsers(boolean emailUsers ) {     
        Messaging.SingleEmailMessage[] lstEmail = new Messaging.SingleEmailMessage[]{}; 
        // Threshold constants  
        integer deactivateThreshold = 90;
        integer alertThreshold = 60;
     
        datetime createdDate;
        datetime lastLogInDate;
        datetime now = datetime.now();        
        string deactivatedUsers = '';
        string alertUsers = '';
        string subject;
        string body = '';
        string userEmail = '';
        id userId;

        integer deactivateCount = 0 ;
        integer alertCount = 0;
        integer activeUsersBefore = 0;
        
        integer x = 0; 
        integer errCount = 0;
        //boolean errsOccur = false;
        string errMsg;        
        
        // Used to build message to be emailed
        Map<id, string> deactivatedUsersMap = new Map<id, string>();
        List<id> errNumList = new List<id>();
        List<string> errMsgList = new List<string>();
        string lineMsg;
                 
        //integer queryRowLimit = Limits.getLimitQueryRows();
        integer queryRowLimit = Limits.getLimitDMLRows();
                
        // get email template
        template = [ Select Id From EmailTemplate Where Name = 'DeactivateUsers' Limit 1 ];

        activeUsersBefore = [ Select Count() FROM User u 
                              Where u.IsActive = true and u.Profile.UserLicense.Name = 'Salesforce' ];
        
        List<User> userList;
        userList = [ Select u.Id, u.Email, u.CreatedDate, u.LastLogInDate, u.IsActive, u.Name, u.Profile.Name
                     From User u 
                     Where u.IsActive = true and u.Profile.Name <> 'System Administrator'
                       and u.No_Auto_Deactivate__c = false
                       and u.Profile.UserLicense.Name = 'Salesforce'
                     Order by u.Name
                     LIMIT :queryRowLimit ];   
                        
        if ( userList.size() == 0 ) { return; }
        // Loop through the userList
        for ( User ul : userList ) {
            createdDate = ul.CreatedDate;
            lastLogInDate = ul.LastLogInDate;
            userEmail = ul.Email;
            userID = ul.Id;
            
            // User never logged in since they were created
            // we must use the created date to compare too
            if ( lastLogInDate == Null ) {    
                // Deactivate user and build user message
                if ( createdDate <= (now - deactivateThreshold) || Test.isRunningTest() ) {
                    ul.IsActive = false;                    
                    lineMsg = ul.Name + ' - never logged in and user was created on ' + createdDate.format('MM/dd/yyyy') + '<br>';
                    deactivatedUsersMap.put(userID, lineMsg);
                    deactivateCount +=  1;
                // Build alert message and user email
                } else if ( createdDate <= (now - alertThreshold) || Test.isRunningTest() ) {
                    alertUsers += ul.Name + ' - never logged in' + '<br>';
                    //Store email in array list that will be sent to the user
                    if ( emailUsers ) {
                        lstEmail.add(sendEmail(ul.Id));   
                    }
                    alertCount += 1;              
                }                
            } else { // User logged in at least once
                // Deactivate user and build user message              
                if ( lastLogInDate <= (now - deactivateThreshold) ) {  
                    ul.IsActive = false;                    
                    lineMsg = ul.Name + ' - last logged in on ' + lastLogInDate.format('MM/dd/yyyy') + '<br>';
                    deactivatedUsersMap.put(userID, lineMsg);
                    deactivateCount += 1;
                                                           
                // Build alert message and user email                   
                } else if ( lastLogInDate <= (now - alertThreshold) ) {
                    alertUsers += ul.Name + ' - last logged in on ' + lastLoginDate.format('MM/dd/yyyy') + '<br>';  
                    //Store email in array list that will be sent to the user
                    if ( emailUsers ) {
                        lstEmail.add(sendEmail(ul.Id));
                    } 
                    alertCount += 1; 
                }    
            }
        } 
        
        if ( deactivateCount > 0 || Test.isRunningTest() ) {    // At least one user can be deactivated at this point
            try {                
                // Lets save to the database and catch any any users that cannot be deactivated for whatever reason
                Database.SaveResult[] results = Database.Update(userList, false);
                 
                if (results != null){                
                    errMsg = 'Apex DeactivateUsers.CheckUsers method threw the following exception(s): ' + '<br><br>';
                    // Loop through the results object array
                    for ( Database.SaveResult result : results ) {                    
                        if ( !result.isSuccess() ) {
                            //errsOccur = true;                        
                            Database.Error[] errs = result.getErrors();
                            // Loop through the errors contained in the result errs array
                            // and build the message list for the email and build a list containing 
                            // the users id of records that cannot be deactivated
                            for( Database.Error err : errs ) {                                
                                lineMsg = '<b>' + userList[x].Name + '</b> ' + err.getStatusCode() + ' - ' + err.getMessage() + '<br><br>';
                                errMsgList.add(lineMsg);
                                errNumList.add(userList[x].ID);
                                errCount += 1;
                                break;
                            }                                         
                        } 
                        x += 1;                 
                    }
                    // adjust the deactivateCount to reflect the true number of deactivated users
                    deactivateCount = deactivateCount - errCount;
                    if ( errCount > 0 ) {                       
                        // Remove the users from deactivatedUserMap who could not be deactivated
                        for ( Id errID : errNumList ) {
                            deactivatedUsersMap.remove(errID); 
                        }                        
                    }
                }             
            } catch (Exception e) {
                subject = 'Apex DeactivateUsers.checkUsers threw the follwing exception exception';
                body = e.getMessage();
                EmailAlerts.emailAdmins(subject, body);            
            }
            
            // Build message containing a list of users who were deactivated
            // First we need to move the map content to a list so we can loop and build the message
            List<string> deactivatedUserList = new List<string>();
            deactivatedUserList = deactivatedUsersMap.values();
            deactivatedUserList.sort();
            
            for ( integer i = 0; i <= deactivatedUserList.size() - 1; i++ ) {
                deactivatedUsers = deactivatedUsers + deactivatedUserList.get(i);
            }                                     
        }    
        body = '<p><b>As of ' + Now.format('MM/dd/yyyy hh:mm:ss a') + ':</b></p>';
        body += '<p>The deactivation threshold is ' + deactivateThreshold + ' days.<br>';
        body += 'The alert threshold is ' + alertThreshold + ' days.<br>';
        body += 'Total Active Users before: ' + activeUsersBefore + '<br>';
        body += 'Total Active Users now   : ' + (activeUsersBefore - deactivateCount) + '</p><br>';
        if ( deactivateCOunt > 0 ) {            
            body += '<p><b>Licenses were deactivated for the following ' + deactivateCount + ' users:</b><br><br>' + deactivatedUsers + '</p><br>';
        }
        if ( alertCount > 0 || Test.isRunningTest() ) {
                body += '<p><b>The following ' + alertCount + ' users are close to being deactivated:</b><br><br>' + alertUsers + '</p>';
        }
        if ( deactivateCount > 0 || alertUsers.length() > 0 || Test.isRunningTest() ) {
        //BEGIN CHANGE 04/17/11 TPB
            //if ( deactivateCount > 0 || Test.isRunningTest() ) {
            if ( errCount > 0 || Test.isRunningTest() ) {
        // BEGIN CHANGE 04/17/11
            body += '<br><br><b>Some users could not be deactivated for one reason or another.  Please review the errors below.</b><br><br>';            
                for ( integer i = 0; i <= errMsgList.size() - 1; i++ ) {
                    body += errMsgList.get(i);
                }
            }
            subject = 'Apex DeactivateUsers.checkUsers: User\'s Licenses Automatically Deactivated or Close to Being Deactivated in SFDC';           
// Begin Change TPB 01/21/12
            /* EmailAlerts.EmailAdmins(subject, body); */        
            EmailAlerts.emailDeactivateRecipients(subject, body); 
// End Change TPB 01/21/12
        }                
        
        if ( emailUsers && alertCount > 0 ) {
            //Send Email
            Messaging.SendEmailResult[] sendResult;
            sendResult = Messaging.sendEmail(lstEmail);
        }
    }
    
    //-----------------------------------------------
    // Build the introduction text and email object
    //-----------------------------------------------
    Messaging.SingleEmailMessage sendEmail( id UserId ) {
/*
        string sIntro;
    
        sIntro = '<p>Dear User,</p>';
        sIntro = sIntro + '<p>The YourCompanyName Salesforce.com system is sending this automated email ' +
                           'because you have not logged into Salesforce.com in over ' + alertThreshold + ' days.</p>';
        sIntro = sIntro + '<p>If you do not log into Salesforce.com soon you will be deactivated.  ' +
                          'Should you become deactivated and need to gain access to the system, please contact ' +
                          'a YourCompanyName Salesforce.com system administrator.</p>';                          
        sIntro = sIntro + '<p>Thank you,<br> YourCompanyName Salesforce.com Support.</p>';  
*/      
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();        
        email.setReplyTo('noreply@salesforce.com');
        email.setSaveAsActivity(false); 
        email.setTargetObjectId(UserId); 
        email.templateId = template.Id;   
        email.setUseSignature(false);
        // use the below methods if you do not use a templateid above
        //email.setSubject('YourCompanyName Saleforce.com User Deactivation Alert!');        
        //String[] toAddresses = new String[] {sUserEmail, 'YourEmail@YourCompanyName.com'};            
        //email.setToAddresses(toAddresses);           
        //email.setHtmlBody(sIntro);
        
        // for testing
        //email.setCcAddresses(new string[] {'YourEmail@YourCompanyName.com'});

// Begin Change TPB 01/21/12        
        /* email.setCcAddresses(EmailAlerts.getSystemAdminList()); */
        email.setCcAddresses(EmailAlerts.getDeactivateRecipientList());
// End Change TPB 01/21/12
                
        return email; 
    }  
}

 

 

 

global class EmailAlerts { 
    //=======================================
    // emails the active admins in the system
    //=======================================   
    public static void emailAdmins( string subject, string body ) {
        // Send an email to the Admin on failure.
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        
        //*** BEGIN TEST USE ONLY ***      
        //String[] toAddresses = new String[] {'my.email.com'};
        //mail.setToAddresses(toAddresses);
        //*** END TESTING USE ONLY ***
        
        //>>>>>>> UNCOMMENT FOR PRODUCTION <<<<<<<
        mail.setToAddresses( getSystemAdminList() );
        
        mail.setSubject(subject);
        mail.setHTMLbody(body);
        Messaging.sendEmail( new Messaging.SingleEmailMessage[] { mail });
    }

    //=============================================
    // gets the list of active admins in the system
    //=============================================  
    public static string[] getSystemAdminList() {
        List<string> AdminList = new List<string>();        
        User[] SysAdmins = [ Select u.email
                             From User u
                             Where u.Profile.Name = 'System Administrator'
                             And IsActive = true ];
        for ( User u : SysAdmins ) {         
            AdminList.add(u.email);
        }     
        return AdminList;
    }

    //====================================================
    // emails the user who have are selected as Deactivate 
    // Recipients in the system and who are active
    //====================================================
    public static void emailDeactivateRecipients( string subject, string body ) {
        // Send an email to the recipients that are checked on the User record.
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        
        //*** BEGIN TEST USE ONLY ***      
        //String[] toAddresses = new String[] {'my.email.com'};
        //mail.setToAddresses(toAddresses);
        //*** END TESTING USE ONLY ***
        
        //>>>>>>> UNCOMMENT FOR PRODUCTION <<<<<<<
        mail.setToAddresses( getDeactivateRecipientList() );
        
        mail.setSubject(subject);
        mail.setHTMLbody(body);
        Messaging.sendEmail( new Messaging.SingleEmailMessage[] { mail } );
    }
    
    //=============================================
    // gets the list of active user who are selected
    // as Deactivate Recipients
    //=============================================      
    public static string[] getDeactivateRecipientList() {
        List<string> DeactivateRecipientList = new List<string>();
        User[] DeactivateRecipients = [ Select u.email
                                        From User u
                                        Where u.Deactivate_Recipient__c = true
                                        And IsActive = true ];
        for ( User u : DeactivateRecipients ) {
            DeactivateRecipientList.add(u.email);   
        }                         
        return DeactivateRecipientList;
    }
}

 

SalesforceDF2011SalesforceDF2011

Thanks Tom!

 

I'll give this a try..Appreciate your help.:smileyhappy: