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
SFDEV ThorSFDEV Thor 

Site.createPortalUser() takes approx 20-25 seconds for first user registration of the day? Why?

We have a Lightning Community and enabled Self Registration.
The issue is weird. It takes approx 20-25 seconds for the very first User registration of the day and all user registrations onwards take only 2.5-3 seconds. We are facing this problem on normal sandbox as well as on partial sandbox also.
Code snippet is as below:
 
public void registerUser(String newUserJson) {
	// This is the input, received from an Lightning Component
	Contact userContact = (Contact)JSON.deserialize(newUserJson, Contact.class);
	
	// This Account has Owner which has role - No issue here
	Account userAccount = [SELECT Id FROM Account WHERE Name = 'Individual' LIMIT 1];	
	
	Profile userProfile = [SELECT Id FROM Profile WHERE Name = 'ABC Community User' LIMIT 1];

	// Setup User data
	User newUser = new User();
	newUser.UserName = generateUniqeUserName(userContact.Email);
	newUser.Email = userContact.Email;
	newUser.FirstName = userContact.FirstName;
	newUser.LastName = userContact.LastName;
	newUser.ProfileId = userProfile.Id;

	// If using site to host the community the user should not hit s1 after logging in from mobile.
	String networkId = Network.getNetworkId();
	if (networkId != null && siteAsContainerEnabled(Network.getLoginUrl(networkId))) {
		newUser.UserPreferencesHideS1BrowserUI = true;
	}

	// Compute dynamic nickname for user
	String nickname = ((userContact.FirstName != null && userContact.FirstName.length() > 0) ? userContact.FirstName.substring(0, 1) : '' ) + userContact.LastName.substring(0, 1);
	nickname += String.valueOf(Crypto.getRandomInteger()).substring(1, 7);
	newUser.CommunityNickname = nickname;

	// First Create Contact	
	userContact.AccountId = userAccount.Id;
	INSERT userContact;
		
	// Create User
	newUser.ContactId = userContact.Id;
	String userId = Site.createPortalUser(newUser, userAccount.Id, password, true);
}

public static boolean siteAsContainerEnabled(String communityUrl) {
	Auth.AuthConfiguration authConfig = new Auth.AuthConfiguration(communityUrl, '');
	return authConfig.isCommunityUsingSiteAsContainer();
}
	
public static String generateUniqeUserName(String email) {        
	return email + ((isSandbox() == true) ? '.abc.sandbox' : '.abc');
}

public static Boolean isSandbox() {
	return [SELECT Id, Name, OrganizationType, NamespacePrefix, InstanceName, IsSandbox FROM Organization].IsSandbox;
}

Please advise what might be taking so long time for simple User registration.

Note: We don't have any other failure in code or complete process, its all about long execution time for only first user registration of the day.
Thanks.
Raj VakatiRaj Vakati
I dnt see any issue in the code .. 
Can you check with your network connection 
Ramesh DRamesh D
We have similar issue with one of our apps which is connected to salesforce, while registering for the first time in the day it takes little longer not sure why but after first registration it's quick.