function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vivek KidambiVivek Kidambi 

Portal user creation using apex

I am working on a requirement to automate portal user creation based on user requests and sync it to downstream system real time. I have a problem creating contact and user in Same transaction due to setup non setup object. I cannot create user in a future method since I need to display the status of user creation on vf page for user. Appreciate if anyone has a workaround for this issue
Best Answer chosen by Vivek Kidambi
Varun PareekVarun Pareek
Yes, there is a workaround for this. Since you do not want to use @future, you would need to identify a way to segregrate contact and user creation in two different transactions. Here is how you do it:
<!---VF code snippet-->
<apex:commandButton value="Save" action="{!createContact}" onComplete="createUser();"/>
<apex:actionFunction name="createUser" action="{!createUserController}"/>

In the above snippet, createContact is your controller method that creates a Contact when "Save" button is clicked. Once the transaction is completed, the createUser(); JS function is called which subsequently calls the action method in your controller createUserController.

This is how you can bifurcate the process in 2 transactions. Once you have this ready, you may want to add some validations like caling the createUserController method only when the contact was created successfully.
 

All Answers

ManojjenaManojjena
Hey

You can use future annotaion to solve this issue .
ManojjenaManojjena
Varun PareekVarun Pareek
Yes, there is a workaround for this. Since you do not want to use @future, you would need to identify a way to segregrate contact and user creation in two different transactions. Here is how you do it:
<!---VF code snippet-->
<apex:commandButton value="Save" action="{!createContact}" onComplete="createUser();"/>
<apex:actionFunction name="createUser" action="{!createUserController}"/>

In the above snippet, createContact is your controller method that creates a Contact when "Save" button is clicked. Once the transaction is completed, the createUser(); JS function is called which subsequently calls the action method in your controller createUserController.

This is how you can bifurcate the process in 2 transactions. Once you have this ready, you may want to add some validations like caling the createUserController method only when the contact was created successfully.
 
This was selected as the best answer