0

I am wondering is it possible to rollback when external user creation fails after password do not meet org requirements?

A detailed explanation: I am currently using createExternalUser method in order to create a community user. I create an account from user inputs then a user instance. Finally I use following code to create my community user.

Site.createExternalUser(userInstance, accountId, password);

User creation returns null (it should return userId). I use standard Exception and Site.ExternalUserCreateException and both of then do not catch any exception and method returns null. When VF page refreshed I face with password error in apex:pagemessage tag. At this point I have an account created in Salesforce, which I want to rollback. My version is 40, document suggests that it is possible if I can catch the error:

Calls to this method in API version 30.0 and later won’t commit the transaction automatically. Calls to this method prior to API version 30.0 commit the transaction, making it impossible to roll back to a save point before the call.

Edit: As Tushar mentioned, I am currently triggering a database rollback if userId is null, but does anyone know why method doesn't throw any exception, or why I face with an error on my VF page if there is no exception thrown?

1

You can use Savepoint sp = Database.setSavepoint(); and Database.rollback(sp); to rollback manually.

Savepoint sp = Database.setSavepoint();
//other code
Id userId =  Site.createExternalUser(userInstance, accountId, password);

if(userId == null){
   Database.rollback(sp);
}
  • That's exactly what i am doing now. But do you know why method doesn't throw an exception if it can't create user, and why do I see error on my page message if there is no exception? – oozkan May 3 '18 at 9:04
  • 1
    @oozkan This method throws Site.ExternalUserCreateException when user creation fails. – Tushar Sharma May 3 '18 at 9:13
  • I already mentioned in the post that I am using both standard exception and Site.ExternalUserCreateException in my try catch block but method does not throw an exception. It just returns null. – oozkan May 3 '18 at 9:16
  • @oozkan I don't have any document link for this but Site method process the code and handle the error similar to Database class where it don't throw the exception. – Tushar Sharma May 3 '18 at 9:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.