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
Amit Singh.ax1042Amit Singh.ax1042 

How to retrieve account Name from visualforce page??

i have created a visualforce page (for creating user)... i have created a custom lookup on this vf page (lookup for accounts)...

and one more thing i have added .. i have taken a custom field on User (standard) object named as Account__c...

i want that whenever we will create new user by using this vf page... once account is selected this account name should be stored in Accouunt__c..

how to do??

 

here is my Code,

 

vf Page-: (CreateNewUser)

 

 

 

 

 

<apex:page standardController="User" extensions="CreateNewUserExtension">
 
<script language="javascript">
    var newWin=null;
    function openLookupPopup(name, id)
    {
        var url="/apex/AccountLookupPopup?namefield=" + name + "&idfield=" + id;
        newWin=window.open(url, 'Popup','height=500,width=600,left=100,top=100,resizable=no,scrollbars=yes,toolbar=no,status=no');
        if (window.focus)
        {
            newWin.focus();
        }
           
        return false;
    }
                 
    function closeLookupPopup()
    {
       if (null!=newWin)
       {
          newWin.close();
       } 
    }
</script> 

<apex:sectionHeader title="User Creation" subtitle="{!User.Name}" help="/help/doc/user_ed.jsp?loc=help"></apex:sectionHeader>
    <apex:form >
        <apex:pageBlock mode="edit">
        <apex:pageMessages />
            <apex:pageBlockButtons >
                    <apex:commandButton action="{!saveme}" value=" Save "></apex:commandButton>
                    <apex:commandButton action="{!cancel}" value="Cancel"></apex:commandButton>
                 </apex:pageBlockButtons>
             
              <apex:pageBlockSection title="User Information" columns="2" collapsible="false"> 
                <apex:pageBlockSectionitem >
                 <apex:outputLabel value="Account"/>
                 <apex:outputPanel >
                 <apex:inputHidden value="{!accountId}" id="targetId" />
                 <apex:inputText size="20" value="{!accountName}" id="targetName" onFocus="this.blur()" disabled="false"/>
                 <a href="#" onclick="openLookupPopup('{!$Component.targetName}', '{!$Component.targetId}'); return false"><apex:image value="{!$Resource.lookup_logo}" title="LookupLogo"/></a>
                 </apex:outputPanel>
                 </apex:pageBlockSectionitem>
                  <apex:inputField value="{!User.User_Type__c}" />
                  <apex:inputField value="{!User.FirstName}"/>
                  <apex:inputField value="{!User.LastName}"/>
                  <apex:inputField value="{!User.Username}"/>
                  <apex:inputField value="{!User.Email}"/>
                  <apex:inputField value="{!User.alias}"/>
                  <apex:inputField value="{!User.UserRoleId}"/> 
                  <apex:inputField value="{!User.IsActive}"/>
                  <apex:inputField value="{!User.UserType}"/>
                  <apex:inputField value="{!User.ProfileId}"/>
                   <apex:inputField value="{!User.Phone}"/>
                   <apex:inputField value="{!User.Extension}"/>
                   <apex:inputField value="{!User.Fax}"/>
                   <apex:inputField value="{!User.EmployeeNumber}"/>
                   <apex:inputField value="{!User.Title}"/>
                   <apex:inputField value="{!User.CompanyName}"/>
                   <apex:inputField value="{!User.Division}"/>
                   <apex:inputField value="{!User.Department}"/>
                   <apex:inputField value="{!User.CommunityNickName}"/>

                   <apex:inputField value="{!User.UserType}"/>
                  
                   <apex:inputField value="{!User.ReceivesInfoEmails}" />
              </apex:pageBlockSection>
             
               <apex:pageBlockSection title="Other Information" columns="1" collapsible="false">
                   <apex:inputField value="{!User.EmailEncodingKey}"/>
                   <apex:inputField value="{!User.TimeZoneSidKey}"/>
                   <apex:inputField value="{!User.LocaleSidKey}"/>
                   <apex:inputField value="{!User.LanguageLocaleKey}"/>
               </apex:pageBlockSection>
              </apex:pageBlock>
              </apex:form>
  
</apex:page>

 

 

Apex Class-:CreateNewUserExtension

 

public class CreateNewUserExtension {
  
    public Id accountId{get;set;}
    public String accountName{get;set;}
      public List<Account> accs{get;set;}
    private final User user;
    public CreateNewUserExtension(ApexPages.StandardController controller)
    {
   
      this.user= (User)controller.getrecord();

    }
   
   
     public PageReference saveme()
    {
 
    user.Username = user.Username;
    user.UserRoleId = user.UserRoleId;
    user.FirstName = user.FirstName;
    user.LastName = user.LastName;
    user.Email = user.Email;
    user.ProfileId = user.ProfileId;
    user.CompanyName = user.CompanyName;
    user.CommunityNickName = user.CommunityNickName;
    user.EmailEncodingKey = user.EmailEncodingKey;
    user.TimeZoneSidKey = user.TimeZoneSidKey;
    user.LocaleSidKey = user.LocaleSidKey;
    user.LanguageLocaleKey = user.LanguageLocaleKey;
    insert user;
    return null; 
  }

}

 

 

Cheers,

Amit Singh

CodeFinderCodeFinder

Hi,

 

I am not sure where are you trying to create an account. I want to know how the account is created. do you want to create a new account when the user is created?

 

As per my understanding I am giving this code. check this and let me know. I am not totally sure. but this might give you a small lead towards what I am indicating. I am really not sure if my code is totally correct as I wrote it in notepad.

 

public class CreateNewUserExtension {

	
	private User user;
	
	public CreateNewUserExtension(ApexPages.StandardController controller)
    	{
   
     	 this.user= (User)controller.getrecord();

    	}

	public PageReference saveme(){

	Pagereference save = new Pagereference('/apex/xxxxxxxx');
	save.setRedirect(true);
	try{
      		if(user != null && user.Id == null)
      		{
        	insert user;
      		}      
      		else if(user.Id != null){
        
        	update user;
      		}
    	}
    	catch(Exception e)
    	{
      		Apexpages.Message msg = new Apexpages.Message(Apexpages.Severity.ERROR, '**bleep** Error');
      		ApexPages.addMessage(msg);
      		save = new Pagereference('/apex/CreateNewUser');
    	}

	Account acc = new Account(Name = user.Username+'Account');
	insert acc;
	
	Account account = [Select id, Name from Account where Name= :user.Username+'Account'];

	User u = [Select id, Account__c, Name from User where Name= :user.Name];
	u.Account__c = account.name;
	update u;

}

 

 

 

 

Shashikant SharmaShashikant Sharma

Your save method should only be this

public PageReference saveme()
    {
       //to fetch account you should
       List<Account> accL = [Select Name from Account where Name =: AccountName Limit 1];
       Account a =  accL.size() > 0 ? accL.get(0) : new Account();
       //this will update or inser bothas per case                                                                 upsert user;
    
     }