Use the register and deregister methods to for your users to register and deregister identity verification methods. Use the obfuscateUser method to scramble user object data on a user’s request. Use the formatPhoneNumber method to ensure the phone number properly formatted.
This class is introduced in API version 43.0. It isn't available in earlier versions.
The following are methods for UserManagement.
public static void deregisterVerificationMethod(Id userId, Auth.VerificationMethod method)
Type: void
Use this method to deregister an existing identity verification method. For example, your users can deregister a phone number when their phone number changes. While only end users can register an identity verification method, you and your users can deregister one. Keep this behavior in mind when you implement a custom registration page.
This method is introduced in API version 43.0. It isn't available in earlier versions.
global static String formatPhoneNumber(String countryCode, String phoneNumber)
Use this method to ensure a user’s mobile phone number is formatted as required by Salesforce. Then use the method’s return value to update the mobile field of the user’s record. This mobile number is used for SMS-based identity confirmation. For example, mobile phone numbers are stored along with other identity verification methods in Auth.VerificationMethod enum. This method is introduced in API version 43.0. It isn't available in earlier versions.
Here are some acceptable ways that users can enter their mobile number:
Now, consider the following examples.
Here's a code example that uses the formatPhoneNumber method. It gets the mobile number from the user and converts it to the format required by Salesforce. Then it updates the user’s record with the formatted mobile number.
global with sharing class PhoneRegistrationController { //Input variables global String countryCode {get; set;} global String phoneNumber {get; set;} global String addPhoneNumber() { if(countryCode == null) return 'Country code is required'; if(phoneNumber == null) return 'Phone number is required'; String userId = UserInfo.getUserId(); User u = [SELECT Id FROM User WHERE Id=:userId LIMIT 1]; String formatNum = System.UserManagement.formatPhoneNumber(countryCode, phoneNumber); u.MobilePhone = formatNum; update u; return null; } }
As long as the country code and phone number are separated, formatPhoneNumber returns a value in the proper format.
public static void obfuscateUser(Id userId, String username)
Type: void
This method is introduced in API version 43.0. It isn't available in earlier versions.
You can use the obfuscateUser method to protect the personal information of your org’s users. When invoked, Salesforce permanently scrambles the user’s object data and replaces it with random character strings. The user’s detail page exists, but the fields contain meaningless strings of characters. Salesforce merely obfuscates (scrambles) personal data because you can't delete a user in Salesforce; you can only disable or deactivate a user. In other words, the user record remains in the database and this method performs a soft delete.
Take care when using this method. The users’ data becomes anonymous and can never be recovered.
Considerations
Assure your admins that invoking this method doesn’t trigger an email change notification.
This method is part of our effort to protect users’ personal data and privacy. For more information on what you can do to actively protect user data, see Data Protection and Privacy in Salesforce Help.
public static void obfuscateUser(Id userId)
Type: void
This method is introduced in API version 43.0. It isn't available in earlier versions.
You can use the obfuscateUser method to protect the personal information of your org’s users. When invoked, Salesforce permanently scrambles the user’s object data and replaces it with random character strings. The user’s detail page exists, but the fields contain meaningless strings of characters. Salesforce merely obfuscates (scrambles) personal data because you can't delete a user in Salesforce; you can only disable or deactivate a user. In other words, the user record remains in the database and this method performs a soft delete.
Take care when using this method. The users’ data becomes anonymous and can never be recovered.
Considerations
Assure your admins that invoking this method doesn’t trigger an email change notification.
This method is part of our effort to protect users’ personal data and privacy. For more information on what you can do to actively protect user data, see Data Protection and Privacy in Salesforce Help.
public class UserManagementController{ public List <User> users {get; set;} public UserManagementController() { Profile p = [select id from profile where name = 'Customer Community User']; users = [select username, id from User where profileId=:p.id AND isactive=true]; } //Use method with extreme caution. Data can't be recovered. @InvocableMethod(label='User Management' description='Obfuscate User data and more') static public void obfuscate(List<User> users) { String uid = ApexPages.currentPage().getParameters().get('uid'); if(uid == null) return; User u = [select contactId from user where id=:uid]; System.UserManagement.obfuscateUser(uid); if(u.contactId != null) { List <Contact> contacts = [select id from Contact where id=:u.contactId LIMIT 1]; if (contacts == null || contacts.isEmpty() == true) return; delete contacts; } } }
public static System.PageReference registerVerificationMethod(Auth.VerificationMethod method, String startUrl)
Type:System.PageReference
Use this method to enable users to complete identity verification, such as 2FA, or to log in to their community without a password. Users register these methods to verify their identity when logging in. You create a custom registration page when implementing mobile-friendly passwordless logins. See passwordlessLogin.
The PageReference returned by registerVerificationMethod redirects the user to the Salesforce verification page. If the user enters the correct code, the user is redirected to the community page specified by the start URL. For example:
PageReference pr = System.UserManagement.registerVerificationMethod(Auth.VerificationMethod.TOTP,startUrl); PageReference p = System.UserManagement.deregisterVerificationMethod(userId,Auth.VerificationMethod.SALESFORCE_AUTHENTICATOR);
This method is introduced in API version 43.0. It isn’t available in earlier versions.