Creating CRM user account via Code

Used following code to create a new user but it does not create a record.

$user = BeanFactory::newBean(‘Users’);
$user->user_name = “crmuser”;
$user->last_name = “Jhon”;
$user->status = ‘Active’;
$user->UserType = ‘RegularUser’;
$user->email1 = ‘jhon@test.com’;
$user->new_password = ‘mypass’;
$user->save();

Does user creation work via form only or can we also use Rest API to create users? Is this available via Bean too?

I never tried this myself, but a few things that occur to me as to why it might be failing

  • any missing required fields?
  • password complexity requirements are being met?
  • any clues in your logs?

Okay resolved this issue by doing the following changes.

First user_hash is missing in the save list. so adding that as

$user->user_hash = md5(‘mypass’);

Secondly adding the email address via SEA method after user is saved.

$sea = new SugarEmailAddress();
$sea->addAddress($_REQUEST[‘email’], true);
$sea->save($user->id, “Users”);

I hope this helps someone. If you also want to login the NEW user as soon as the form is submitted, you can add following code to do so .

  <form method="POST" action="index.php?module=Users&action=Login" id="user_login">
        <input type="hidden" name="user_name" value="<?php echo $_REQUEST['email'];?>">
        <input type="hidden" name="username_password" value="<?php echo $_REQUEST['username_password']; ?>">
        <input type="hidden" name="module" value="Users">
        <input type="hidden" name="action" value="Authenticate">
        <input type="hidden" name="return_module" value="Users">
        <input type="hidden" name="return_action" value="Login">
    </form>
    <script type="text/javascript">
    document.getElementById('user_login').submit();
    </script>
2 Likes

Nice work, I am sure this will be useful for others in the future!

Wouldn’t you like to turn this into a Technical Blog post?

To add here:
https://docs.suitecrm.com/blog/

i would love to contribute this and other snippets that i have. Please send me PM on how to do that.

1 Like

I guess it’s better if we keep talking about this publicly, that way others will be able to learn from it also :slight_smile:

We have some Documentation about how to edit the Documentation:

https://docs.suitecrm.com/community/contributing-to-docs/

Adding a Blog post is essentially adding a new file in the blog directory.

Do you prefer to use git with a local copy of the site (which you can deploy locally and see the results as you progress) or do you prefer to keep it simple and just add the file into GitHub web application?

I would prefer to just have a place to post small snippets and common solutions that people face often on suitecrm.
PS: is there a way we can mark a question as Resolved?

About the “mark as resolved” that exists as a plugin

There are many Discourse plugins, I want to test a few but for now I’m trying to be minimalistic, there is a lot to learn about Discourse itself, before we start extending it…

About the Blog, I would be quite pleased if it was updated often, and would simply be

a place to post small snippets and common solutions that people face often on suitecrm

We don’t need to blog to have only large posts, simple things will be just fine.

Have you seen my list of stuff that I would like someone to write up as blog posts?

1 Like