You need to sign in to do that
Don't have an account?
ak1234
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
Error Message : System.NullPointerException: Attempt to de-reference a null object
actually it does'nt take dynamic parameter..
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
public String uname { get; set; } -> public String username { get; set; }