Announcement

Collapse
No announcement yet.

How to Create a User for every Contact Created?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to Create a User for every Contact Created?

    I need to allow every Contact created as a record in the Contact Entity to access the system.

    I am using a workflow to create a User for every contact created (trigger).

    I also saw the documentation: https://docs.espocrm.com/administrat...ating-new-user

    How can I generate a username ($userName in docs' formula) for every Contact from the workflow/formula?

  • #2
    maybe email address do the trick easilly

    Comment


    • #3
      item I am trying to insert the email as the username but I am getting username cannot be empty error:

      Code:
      $emailAddress = string\substring(entity\attribute(emailAddress), 0);
      $userName = string\match($emailAddress, '/([^@]+)/');
      
      $password = password\generate();
      $hash = password\hash($password);
      
      $userId = record\create(
         'User',
         'userName', $userName,
         'password', $hash
         'emailAddress', $emailAddress
      );
      
      $emailBody = string\concatenate(
         'Username: $userName',
         'Password: '
      );
      
      $emailId = record\create(
         'Email',
         'to', $emailAddress,
         'status', 'Sending',
         'subject', 'Access info',
         'body', $emailBody,
         'isHtml', false
      );
      
      ext\email\send($emailId);
      The error is from $emailAddress, when I replace it with a hardcoded string (eg: 'myemail@gmail.com') it works fine. But I'm trying to extract it from the entered emailAddress field then use a regex to create a username out of everything before the '@'. It is not taking the emailAddress entered
      Last edited by JosNas; 11-29-2021, 09:23 AM.

      Comment


      • item
        item commented
        Editing a comment
        entity\attribute('emailAddress')

      • JosNas
        JosNas commented
        Editing a comment
        item much appreciated the User was successfully created, but is there a way to relate that user record to the contact record? Like when the user logs in he can see other contacts but can edit only his contact record

    • #4
      create portal role and relate portal user to portal role but how ?


      $userId = record\create( 'User',
      'userName', $userName,
      'password', $hash
      'emailAddress', $emailAddress,
      'type', 'portal',
      'contactId', entity\attribute('id'),
      //'teamsIds', list('idOfPortalRole') // BAD
      );

      there a tables :
      portalRoleUser => relate with these table
      portalRole
      portalUser. => relate with these table


      if you find, you can post here the working formula who can help another
      Last edited by item; 11-29-2021, 05:40 PM.

      Comment


      • #5
        I think I found an alternative solution. I assign each created User to the Contact that triggered the creation.

        Code:
        $userID = record\findOne('User', 'createdAt', 'desc', 'firstName=', firstName, 'middleName=', middleName, 'lastName=', lastName);
        
        assignedUserId = $userID;
        assignedUserName = record\attribute('User', $userID, 'name');
        The formula above is not assigning the user though for some reason

        Comment

        Working...
        X