Loading
Prepare for Email to Become the Default Login ExperienceRead More
Get Started with Communications, Media, and Energy & Utilities (CME)...
ExternalUserSelfRegistration

ExternalUserSelfRegistration

Facilitates creating a new external user using OmniScript. This implementation calls Site.CreateExternalUser with information passed in. It is then called within OmniScript through a remote action. The user ID, account ID, and contact ID are created and written to the output map. This information can be used in later steps. There are two optional remote options:

  • profileId creates a user of a different profile than the default set up in the community.

  • sendEmaildetermines if a confirmation email is sent to the new user.

Interface

VlocityOpenInterface

Sample Code

global with sharing class ExternalUserSelfRegistration implements VlocityOpenInterface {
   public Boolean invokeMethod(String methodName, Map<String,Object> inputMap, Map<String,Object> outMap, Map<String,Object> options) {
      Boolean success = true;
      try{
         if(methodName == 'selfReg') {
            registerUser(inputMap, outMap, options);
      }
      }catch(Exception e){
         logger.err(e);
         success=false;
      }
      return success;
      }
      public static void registerUser(Map<String,Object> inputMap, Map<String,Object> outMap, Map<String,Object> options)
      {
         Logger.dbg('user input '+inputMap);
         String email = (String)inputMap.get('email');
         String firstName = (String)inputMap.get('firstName');
         String lastName = (String)inputMap.get('lastName');
         String communityNickname = (String)inputMap.get('nickName');
         String password = (String)inputMap.get('password');
         String userName = email;
         //optional settings
         String profileId = String.isNotBlank((String)options.get('profileId'))?(String)options.get('profileId'):null;//this is optinal, when it is null, it will create user with default profile set in community
         String accountId = String.isNotBlank((String)options.get('accountId'))?(String)options.get('accountId'):null;//this is optional, when it is null, it will create person account
         Boolean sendEmail = false;
         if ((Boolean)options.get('sendEmail') == true)
         {
            sendEmail =true;
         }
         Logger.dbg('register user options: ' + options);
      User u = new User();
         u.Username = userName;
         u.Email = email;
         u.FirstName = firstName;
         u.LastName = lastName;
         u.CommunityNickname = communityNickname;
         u.ProfileId = profileId;
         String userId = null;
         try{
            userId = Site.createExternalUser(u, accountId, password, sendEmail);
            outMap.put('selfRegUserId', userId);
         }
         catch(Site.ExternalUserCreateException e){
            logger.err('Failed to create portal user with exception ' + e.getMessage());
            //outMap.put('externalUserCreateExceptionList', e.getDisplayMessages());
            if(e.getMessage().containsIgnoreCase('User already exists') )
            {
            Boolean isResetPwd = Site.forgotPassword(userName);
            if(isResetPwd)
               {
                  outMap.put('vlcExternalUserCreateException', e.getMessage()+ ' Please check your email to reset your password.');
               }
               else
               {
                  outMap.put('vlcExternalUserCreateException', e.getMessage());
               }
               }
               else
               {
                  outMap.put('vlcExternalUserCreateException', e.getMessage());
               }
            }
            if(userId != null)
            {
               User selfRegUser = [SELECT Id, AccountId, ContactId From User Where Id = :userId];
               outMap.put('selfRegPerAccntId', selfRegUser.AccountId);
               outMap.put('selfRegConId', selfRegUser.ContactId);
            }
         }
}

Related Implementations

None

 
Loading
Salesforce Help | Article