6

I'm trying to write an Apex web service that can create a customer portal user. After creating the user, there is a separate step which does a password reset for the user and sends them an email.

The problem I'm running into is that Salesforce is automatically sending the user an email when the Contact and User are created. I want to prevent this so I can send the email later after setting their password.

I've tried the code below, which sounds like it should work according to the docs.

Database.DMLOptions dlo = new Database.DMLOptions();
dlo.EmailHeader.triggerUserEmail = false;
dlo.EmailHeader.triggerAutoResponseEmail = false;
dlo.EmailHeader.triggerOtherEmail = false;

...
contact.setOptions(dlo);
Database.insert(contact, dlo);

...
user.setOptions(dlo);
Database.insert(user, dlo);

Unfortunately, each time I create a user with the webservice I'm still receiving an email from Salesforce, despite setting these flags on DMLOptions...

Does anybody know of a solution which will prevent this from happening?

Edit: I also tried with Site.createPortalUser(user, acctId, null, false). This didn't work for the following reason:

System.TypeException: That operation is only allowed from within an active site.
2
  • Can you confirm that you are using customer portals and not Communities?
    – greenstork
    Commented May 2, 2014 at 16:16
  • Yes, I'm using a customer portal. It seems that I can't use the Site class to create the user since the Apex webservice is running on my org, and not on a Force.com Site. Commented May 2, 2014 at 17:29

1 Answer 1

1

After further investigation and running some debug logs, I found that our org had a managed trigger that was firing when the User record was being inserted.

We were not able to determine exactly what the trigger was doing (as it was managed code), but after disabling it the email is no longer being sent from Salesforce, and DMLOptions now appears to be working as expected.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .