0

My visualforce page creates many contacts in one go. I want to pre-populate a certain account name when the page is loaded. Even though it always pre-populates a certain account, user should have the ability to use the lookup and change to another account if wanted. How can I do this?

My visualforce code used in the pageblocktable

<apex:column headerValue="Account Name">
    <apex:inputField value="{!eachRecord.record.AccountId}" required="true"/>
</apex:column>

Controller

public with sharing class AddMultipleContactsController {

    public List<WrapperContactList> contList {get; set;}
    public Integer rowToRemove {get; set;}

    public AddMultipleContactsController() {
        contList = new List<WrapperContactList>();
        addNewRowToContList();
    }

    public void addNewRowToContList(){
        contList = AddMultipleContactsHelper.addNewRowToContList(contList);
    }

    public void removeRowFromContList(){
        contList = AddMultipleContactsHelper.removeRowToContList(rowToRemove, contList);
    }

    public PageReference SaveMultipleContacts(){
        try{
            AddMultipleContactsHelper.save(contList);
        }catch(Exception ex){
            ApexPages.addMessage(new Apexpages.Message(Apexpages.Severity.ERROR,ex.getMessage()));
        }
        ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'All Contact Records have been successfully Saved'));
        return null;
    }

    public PageReference SaveMultipleContactsAndClose(){
        try{
        AddMultipleContactsHelper.save(contList);
        }catch(Exception ex){
            ApexPages.addMessage(new Apexpages.Message(Apexpages.Severity.ERROR,ex.getMessage()));
        }
        PageReference contactPage = new PageReference('/003/o');
        return contactPage;
    }

    public class WrapperContactList{
        public Integer index {get; set;}
        public Contact record {
        get{
            if(record.AccountId == null){
                Account_Setting__c m = new Account_Setting__c.getInstance();
                record.AccountId = m.Acccount_Id__c;
            }
            return record;
        } set {record = value;}
    }
    }
}
5
  • Is this possible using a custom setting ? Add the Id of the wanted Account to the Custom setting and retrieving from the controller ?
    – Tinkerbell
    Nov 3, 2015 at 17:18
  • 1
    yes you can do in that way and inside your controller you can fetch the value from custom setting and assign that to contact.Accountid field
    – Himanshu
    Nov 3, 2015 at 17:38
  • @Himanshu can you please provide me an example to follow ? I'm a bit confused to get this done
    – Tinkerbell
    Nov 3, 2015 at 18:33
  • 1
    do you already have controller of this vf page ?
    – Himanshu
    Nov 3, 2015 at 18:34
  • @Himanshu yes I updated my question with the controller.
    – Tinkerbell
    Nov 3, 2015 at 19:47

1 Answer 1

1

Create a hierarchy custom setting into your system by following these steps

Create Hierarachy custom setting

Define new custom field of type text.

enter image description here

Click on Manage to fill Account id value.

enter image description here

After creating custom settings you can access that value inside your controller in following way.

 public contact record {
  get {
     //fill only if accountid is null but you can put some other logic here. 
     if(record.Accountid==null)
     {
        //Call custom setting and fill the account id.
        Manage_System__c m = Manage_System__c.getinstance();

        record.Accountid = m.Account_ID__c;
     }
     return record; 
  }
  set { record = value; }


} 
7
  • This is very Helpful ! I'm currently trying this solution, this code logic should be added to my parameter in my Wrapper class right ?
    – Tinkerbell
    Nov 4, 2015 at 10:05
  • 1
    yes. that is correct. the basic idea is reading the value from custom setting and assign that to contact AccountId field
    – Himanshu
    Nov 4, 2015 at 10:12
  • I'm currently having problems accessing the getInstance() method. I'm getting the error "Invalid type: Account_Setting__c.getInstance". I have updated my question with what I tried. Help on this will be much appreciated.
    – Tinkerbell
    Nov 4, 2015 at 10:23
  • 1
    it is only Account_Setting__c m = Account_Setting__c.getinstance(); there is no new keyword between them.
    – Himanshu
    Nov 4, 2015 at 10:27
  • 1
    perfect! kudos to your effort :)
    – Himanshu
    Nov 4, 2015 at 10:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .