3

This question already has an answer here:

I have an object Application and Application Address.

Type field on Application address which has values Addr1,Addr2,Addr3

Trigger on application :- When new application is created, Create 3 application address of different type.

On my vf page. I want to show all 3 application address in different page block section.

I tried this and its working. But want to know how is this possible using map In constructor

this.bizapplicationaddress =[SELECT Type__c, Name, Id, Contact_Person__c, Contact_Person_Title__c, Application__c, Address__c, Additional_Instructions__c 
                                    From Application_Address__c
                                    WHERE Application__c = : this.application.id
                                    AND Type__c = 'Address1' LIMIT 1];                       
this.corapplicationaddress =[SELECT Type__c, Name, Id, Contact_Person__c, Contact_Person_Title__c, Application__c, Address__c, Additional_Instructions__c 
                                    From Application_Address__c
                                    WHERE Application__c = : this.application.id
                                    AND Type__c = 'Address2'LIMIT 1];                   
this.regapplicationaddress =[SELECT Type__c, Name, Id, Contact_Person__c, Contact_Person_Title__c, Application__c, Address__c, Additional_Instructions__c 
                                    From Application_Address__c
                                    WHERE Application__c = : this.application.id
                                    AND Type__c = 'Address3' LIMIT 1];        public void copyAddresDetailstoBiz()
{
    this.bizapplicationaddress.Contact_Person__c            =   this.regapplicationaddress.Contact_Person__c;
    this.bizapplicationaddress.Contact_Person_Title__c      =   this.regapplicationaddress.Contact_Person_Title__c;
    this.bizapplicationaddress.Address__c                   =   this.regapplicationaddress.Address__c; } public void copyAddresDetailstoCor()
{
    this.corapplicationaddress.Contact_Person__c            =   this.regapplicationaddress.Contact_Person__c;
    this.corapplicationaddress.Contact_Person_Title__c      =   this.regapplicationaddress.Contact_Person_Title__c;
    this.corapplicationaddress.Address__c                   =   this.regapplicationaddress.Address__c;
}

In page block

<apex:pageBlockSection columns="1" id="pbRegisteredAddr" title="Address1">
    <apex:inputField value="{!regapplicationaddress.Contact_Person__c}"/>
    <apex:inputField value="{!regapplicationaddress.Contact_Person_Title__c}"/>
    <apex:inputField value="{!regapplicationaddress.Address__c}"/>

</apex:pageBlockSection>
<apex:pageBlockSection columns="1" id="pbBusinessAddr" title="Address2"><apex:commandLink action="{!copyAddresDetailstoBiz}" value="Same As Above" reRender="pbBusinessAddr"/>
<apex:inputField value="{!bizapplicationaddress.Contact_Person__c}"/>
    <apex:inputField value="{!bizapplicationaddress.Contact_Person_Title__c}"/>
    <apex:inputField value="{!bizapplicationaddress.Address__c}"/>

</apex:pageBlockSection> 
<apex:pageBlockSection columns="1" id="pbCorrespondenceAddr" title="Address3">
<apex:commandLink action="{!copyAddresDetailstoCor}" value="Same As Above" reRender="pbCorrespondenceAddr"/>
    <apex:inputField value="{!corapplicationaddress.Contact_Person__c}"/>
    <apex:inputField value="{!corapplicationaddress.Contact_Person_Title__c}"/>
    <apex:inputField value="{!corapplicationaddress.Address__c}"/>
    <apex:inputField value="{!corapplicationaddress.Additional_Instructions__c}"/>
</apex:pageBlockSection>

marked as duplicate by Samuel De Rycke, Sergej Utko, Boris Bachovski, Benj, Saariko Oct 9 '13 at 15:17

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    I believe your question is: How to handle Maps on Visualforce page It would be good if you first try to implement this on your specific requirements yourself. – Samuel De Rycke Oct 9 '13 at 7:39
  • Nops that post doenst help me out in using map for my requirement. :( – TestNS Oct 9 '13 at 8:22
  • 1
    The point was to look at the answer and apply that to your own requirement such as the people posting answers are doing. Trying to abstract your specific requirements out of the true question results in a question which is a much greater resource to other developers. And it usually results in less copy pasting and more learning, which is what we try to facilitate here :-) – Samuel De Rycke Oct 9 '13 at 9:42
2
List <String> addressTypes = new List <String>();
for(Schema.PicklistEntry pEntry : Application_Address__c.Type__c.getDescribe().getPicklistValues())
{
    addressTypes.add(pEntry.getValue());
}
Map<String,Application_Address__c>  applicationAddresses = new Map<String,Application_Address__c>();
for(Application_Address__c item : [SELECT   Type__c, Name, Id, Contact_Person__c, Contact_Person_Title__c, 
                                            Application__c, Address__c, Additional_Instructions__c 
                                    From    Application_Address__c
                                    WHERE   Application__c = : this.application.id])
{
    applicationAddresses.put(item.Type__c,item);    
}

<apex:repeat value="{!addressTypes}" var="type">
    <apex:pageBlockSection columns="1" id="applicationAddress" title="{!type}">
        <apex:inputField value="{!applicationAddresses[type].Contact_Person__c}"/>
        <apex:inputField value="{!applicationAddresses[type].Contact_Person_Title__c}"/>
        <apex:inputField value="{!applicationAddresses[type].Address__c}"/>
         <apex:inputField value="{!corapplicationaddress.Additional_Instructions__c}" rendered="{!type == 'Address3'}"/>
    </apex:pageBlockSection>
</apex:repeat>
  • Thanks it worked... can i have the section to be rendered as Address1 , Address 2 and then Address3.. – TestNS Oct 9 '13 at 9:53
  • Now its rendering as Address3 as first, Address 1 and Address 2 section...I want it to be as Address1 then Address 2 and then Address 3 section – TestNS Oct 9 '13 at 10:30
  • I updated my answer ;-) – Cloud Ninja Oct 9 '13 at 10:35
  • Thanks... but sorry as i had to be more specific for this... If i have the address type as 'Business Address', 'Registered Addr' and correpodence.. and want to show the sections from top to bottom as 'Registered', Business and then corresponce.. then the sort doesnt serve my purpose – TestNS Oct 9 '13 at 11:00
  • Your Type__c field is a picklist ? – Cloud Ninja Oct 9 '13 at 11:02
2

Great question and consideration, Maps in Apex can be used to great effect to optimise your code and the use of platform resources such as SOQL queries.

public with sharing class AddressesController {

    public Map<String, Address__c> AddressMap {get;set;}

    public AddressesController()
    {
        // Load addresses for Type Address1, Address2 and Address3 for the given Application
        List<Address__c> addresses = 
            [select Id, Name, Type__c, Contact_Person__c, Address__c from Address__c
                where Type__c in ('Address1', 'Address2', 'Address3')
                      /* Application__c = this.application.Id */];
        // Map the addresses by type
        AddressMap = new Map<String, Address__c>();
        for(Address__c address : addresses)
            AddressMap.put(address.Type__c, address);
    }
}

You can also leverage Maps in Visualforce using the following syntax.

        <apex:pageBlockSection columns="1" id="pbRegisteredAddr1" title="Address1">
            <apex:inputField value="{!AddressMap['Address1'].Contact_Person__c}"/>
            <apex:inputField value="{!AddressMap['Address1'].Address__c}"/>
        </apex:pageBlockSection>
        <apex:pageBlockSection columns="1" id="pbRegisteredAddr2" title="Address2">
            <apex:inputField value="{!AddressMap['Address2'].Contact_Person__c}"/>
            <apex:inputField value="{!AddressMap['Address2'].Address__c}"/>
        </apex:pageBlockSection>
        <apex:pageBlockSection columns="1" id="pbRegisteredAddr3" title="Address3">
            <apex:inputField value="{!AddressMap['Address3'].Contact_Person__c}"/>
            <apex:inputField value="{!AddressMap['Address3'].Address__c}"/>
        </apex:pageBlockSection>

Not the answer you're looking for? Browse other questions tagged or ask your own question.