salesforcesalesforce-developer

Using the Salesforce REST API, is there a way to create a user and also send out the activation email?


In Salesforce, when I create a user via their REST API, no activation email is sent. There's also no ability to specify a password when the new user is created, which means the user cannot login. This also means the user can't verify their email at the same time as activating.

Is there a way to ensure an activation email gets sent to new SFDC users when they're created via API?

The Salesforce developer documentation is pretty lacking in this regard, so I'm not sure if I'm overlooking something.

The only two "solutions" I've come across are:

  1. A SFDC admin has to manually press the "reset password" button for the user.
  2. You need to make a second API call after the user is created, but this time to reset the password to whatever you want. Then I could set up a Flow to send an email to the user telling them what their initial password is. The problem with this approach is that the user's email is still unverified.

Tried adding a new user via API but no activation email was sent. Was expecting one to be sent.


Solution

  • Uh, good question!

    "Normal" Apex has DmlOptions (triggerUserEmail) for that or System.resetPassword('user id goes here') call.

    SOAP API has corresponding headers: https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_header_emailheader.htm

    But looks like REST API is lacking and has only Case/Lead Assignment Rule header.


    Would it be an option to try the SOAP route? Disappointing but you'd "just" have to craft the right XML, the session id (a.k.a. access_token, the "Authorization: Bearer ..." value) is reusable across APIs

    Would it be an option to combine the REST calls into 1 "all or nothing" operation. It's called composite requests. Insert, then use the referenceId to call https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_sobject_user_password_delete.htm ? I've used composites in the past, not for user stuff but they're pretty cool: https://salesforce.stackexchange.com/a/274696/799 (for more complex integrations I'd still prefer to expose an Apex webservice for all-or-nothing atomic operation rather than trusting the calling system to do it right... but hey, it's there, it's neat, it's "just" a little more involved JSON to write)

    Or you could write something in SF itself