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
ak1234ak1234 

Creating User From Apex Code

I am trying to create new user from my visualforce page and custom controller but i am getting error while dynamically passing parameter to User object, actuallly the constructor doesnt take the parameter dynamically, is there any other method to do same?? please replay me immediatly!! Thanks

Jia HuJia Hu
What is the error message?
ak1234ak1234

Error Message : System.NullPointerException: Attempt to de-reference a null object

 

actually it does'nt take dynamic parameter..

Jerun JoseJerun Jose
Please post the code you are using.
ak1234ak1234

Visualforce Page

 

<apex:page controller="CreateNewProfile">

 <apex:pageBlock title="Create User">

<apex:pageBlockButtons >

<apex:form >

 <apex:commandButton value="Create" action="{!Create}"/>

 </apex:form>

</apex:pageBlockButtons>

 <apex:pageBlockSection title="User Info">

 <apex:form >

<apex:OutputLabel value="Name : "></apex:OutputLabel>

 <apex:inputText value="{!name}"/> <br/><br/>

 <apex:OutputLabel value="User Name : "></apex:OutputLabel>

 <apex:inputText value="{!username}"/> <br/><br/>

 <apex:OutputLabel value="Email ID : "></apex:OutputLabel>

 <apex:inputText value="{!email}"/> <br/><br/>

<apex:OutputLabel value="Alias : "></apex:OutputLabel>

<apex:inputText value="{!alias}"/> <br/><br/>

 </apex:form>

</apex:pageBlockSection>

 </apex:pageBlock>

 </apex:page>

---------------------------------------------------------------------------------------

Apex custom controller

 

public class CreateNewProfile {

    public String alias { get; set; }

    public String email { get; set; }

    public String uname { get; set; }

    public String name { get; set; }

    

  public PageReference Create() {          

  

   Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];

   Id pid = p.Id;        

   callMe(alias,email,name,pid,uname);   

   return null;         

  } 

 

      Public void callMe(String a,String b,String c,Id i,String d)      {      

      User u = new User();  

      u.Alias=a;     

      u.Email=b;    

      u.EmailEncodingKey='UTF-8';   

      u.LastName=c;   

      u.LanguageLocaleKey='en_US';

      u.LocaleSidKey='en_US';    

      u.ProfileId = i;    

      u.TimeZoneSidKey='America/Los_Angeles';  

      u.UserName=d;   

      u.CommunityNickname='akv';

       insert u;         } 

 }

 

 

Thanks,

Akshay Varu

Jerun JoseJerun Jose
Also, could you post the stack trace of the exception. Or alteast the line number of the exception.
Jia HuJia Hu
Basically the code works for me.

public String uname { get; set; } -> public String username { get; set; }