0

I have a vf page with country and geography fields. Geography field is an autocomplete field.

Upon selection of country field, geography field list gets populated. Ex - 1.If users selects United Arab Emirates, Geography fields populates values

  1. A, B, C, D. ------User selected A, B.
  2. Then if the user selects country as United States, Geography field gets rerender and the value A,B is lost.

I need to retain the old values and also add the new values.

Below is my class and Vf Page -

Class -

public class RfpControllerExtension2 {

public string model{get;set;}
public List<String> SelectedDept{get;set;}
public List<Geography__c> results{get;set;}
public List<SelectOption> AllDept{get;set;}
public List<SelectOption> AllName{get;set;}
public List<String> SelectedName{get;set;}
public String discountScheduleID { get; set; }

public RfpControllerExtension2 (){

AllDept= new List<SelectOption>();
AllDept= getDept();
AllName= new List<SelectOption>();
AllName= getName();
}

public void FindAllName()
    {
        system.debug('@@@@@@ Entered');
        AllName= new List<SelectOption>();
        AllName= getName();
    }  

public List<SelectOption> getDept()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Select','--Select--'));
        Schema.DescribeFieldResult departfield =  Geography__c.Country__c.getDescribe();
        List<Schema.PicklistEntry> picklist = departfield.getPicklistValues();
        for( Schema.PicklistEntry f : picklist){      
        options.add(new SelectOption(f.getLabel(), f.getValue()));
        }    
        return options;

     }

   public List<SelectOption> getName()
    {
        List<SelectOption> options = new List<SelectOption>();
        Set<String> sProj = new Set<String>();

        if(discountScheduleID != null )
            {
               for(Geography__c Proj  :[select Id, Canonical_Name__c, Type__c from Geography__c where status__c = 'active' AND Country__c =: discountScheduleID order by Canonical_Name__c] )
               {
                    options.add(new SelectOption(Proj.ID,Proj.Canonical_Name__c));
               }
            }

        return options;

     }

     public Map<String, String> errorMap {
        get {
            if (errorMap == null || errorMap.size() == 0) {
                errorMap = new Map<String, String> {
                    'selectedGeographies' => '',
                    'flightDates' => '',
                    'agency' => '',
                    'agencyContact' => ''
                };
            }
            return errorMap;
        }
        private set;
    }

}

VF Page -

<apex:page controller="RfpControllerExtension2">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<c:FixSfdcFocusHack />
<c:JQuery />
<c:Chosen />
<style type="text/css">
    .fullWidth { width: 100%; }
    .flightDateRangeText { font-size: 90%;  }

    .bPageBlock .requiredForSubmit .requiredBlock {
        background-color: #FFD700;
        bottom: 1px;
        left: -4px;
        position: absolute;
        top: 1px;
        width: 3px;
    }

    .bPageBlock .requiredForSubmitInput {
        height: 100%;
        position: relative;
    }

</style>

<script type="text/javascript">
    /* <![CDATA[ */

    /* 
     *  Render Chosen on select elements with the chzn-select class specified.
     */
    function renderChosen() {
        $j('.chzn-select').chosen();        
    }       

    /*
     *  Render required marker on elements where class == "required".
     */
    function renderRequired() {
        $j('.required').wrap('<div class="requiredInput" />').before('<div class="requiredBlock" />');
    }

    /*
     *  Render required marker on elements with the specified ID.
     */        
    function renderRequiredComponentId(componentId) {
        $j(jq(componentId) + ' .required').wrap('<div class="requiredInput" />').before('<div class="requiredBlock" />');
    }

    var rfpRequiredIds = ["dueDate", "clientsEstablishedPartners", "campaignMarketingObjectives", "campaignGoals",
        "scopeOfCampaign", "mediaRequested", "otherMediaRequested", "campaignTotalNet", 
        "primaryTargetAudience", "geographies", "servingPreference", "clientServedSpecifics", "adSizes",
        "creativeType", "richMediaType", "percentageFlash", "percentageRichMedia", "flightDateRanges"];

    var optRequiredFieldIds = ["planCompletionDate", "clientsEstablishedPartners",
        "campaignMarketingObjectives", "campaignGoals", "scopeOfCampaign", "mediaRequested", "campaignComments",
        "primaryTargetAudience", "geographies", "customGeoDetail", "servingPreference", "clientServedSpecifics",
        "adSizes", "creativeType", "richMediaType", "additionalCreativeNotes"];

    //SSirikonda - 07/25/2013 - Removing the field Plan_completion_Date__c as yellow marked field when
    //RFP type drop-down = SEM   
    /*var semRequiredFieldIds = ["planCompletionDate", "url", 
       "campaignMarketingObjectives", "campaignComments", "geographies", "customGeoDetail"];*/

    var semRequiredFieldIds = ["url", 
        "campaignMarketingObjectives", "campaignComments", "geographies", "customGeoDetail"];

    function modifyRequired() {
        $j('.requiredForSubmitInput').removeClass('requiredForSubmitInput');
        var type = $j('[id$="type"]').val();
        if (type == 'RFP') {
            $j.each(rfpRequiredIds, function(index, value) {
                var selector = '[id$="' + value + '"]';
                $j(selector).addClass('requiredForSubmitInput');
            });
        } else if (type == 'OPT') {
            $j.each(optRequiredFieldIds, function(index, value) {
                var selector = '[id$="' + value + '"]';
                $j(selector).addClass('requiredForSubmitInput');
            });
        } else if (type == 'SEM') {
            $j.each(semRequiredFieldIds, function(index, value) {
                var selector = '[id$="' + value + '"]';
                $j(selector).addClass('requiredForSubmitInput');
            });
        }
    }

    function changePrimaryContact() {
        var value = $j('[id$="primaryContact"]').val();
        var selector = '[id$="agencyContact"]';
        $j(selector).removeClass('requiredInput');
        var selector = '[id$="agency"]';
        $j(selector).removeClass('requiredInput');
        if (value == 'Agency') {
            var selector = '[id$="agency"]';
            $j(selector).addClass('requiredInput');
            var selector = '[id$="agencyContact"]';
            $j(selector).addClass('requiredInput');
        }
    }

    $j(document).ready(function() {
       renderChosen();
        renderRequired();
        changePrimaryContact();
        modifyRequired();
    });
   /* $(".chzn-select").chosen().change(function() {
          function(){
            renderChosen();
            alert("test");
            }
    });
    */

    /* ]]> */        
</script>

<apex:form id="form">
    <apex:pageMessages />
    <apex:selectList value="{!discountScheduleID}" size="1" styleClass="fullWidth chzn-select">
        <apex:actionSupport event="onchange" action="{!FindAllName}" rerender="geographies" oncomplete="renderChosen()"/>
        <apex:selectOptions value="{!dept}" />
    </apex:selectList>  

      <apex:outputLabel value="{!$ObjectType.RFP__c.Fields.Geography__c.Label}" />
          <apex:outputPanel id="geographies" layout="block"  styleClass="requiredInput">
       <apex:outputPanel id="geos" layout="block"  styleClass="requiredBlock"/>
     <apex:selectList value="{!SelectedName}" multiselect="true" id="selectedGeographies" styleClass="fullWidth chzn-select" >
        <apex:selectOptions value="{!AllName}" id="movieTextBox"></apex:selectOptions>
    </apex:selectList>
    <apex:outputPanel layout="block" styleClass="errorMsg" >
                        <apex:outputText value="{!errorMap['selectedGeographies']}" 
                            escape="false" rendered="{!errorMap['selectedGeographies'] != ''}" />
                    </apex:outputPanel>
                </apex:outputPanel>

</apex:form>

</apex:page>

enter image description here Regards

5
  • There are many way, If you are calling, action method then store in class side variable . if you can calling js method then store the value in js variables. and retrieve when required.
    – Ankuli
    Commented Sep 30, 2015 at 12:36
  • Hi Ankuli.. Can u let me know imy above code what change need to be done.. Commented Sep 30, 2015 at 12:40
  • Sure. let me have the eyes on code. On selection on country, you are calling action method or javascript method ? which method
    – Ankuli
    Commented Sep 30, 2015 at 12:46
  • 1
    FindAllName method is being called.. This is present in the class Commented Sep 30, 2015 at 12:58
  • Please check the answer, does it ans your question.
    – Ankuli
    Commented Sep 30, 2015 at 13:04

1 Answer 1

0

Please refer this code for your class. I have created a new String variable strGeographix which will store geograpic values and put in your errorMap on every call.

    public class RfpControllerExtension2 {

public string model{get;set;}
public List<String> SelectedDept{get;set;}
public List<Geography__c> results{get;set;}
public List<SelectOption> AllDept{get;set;}
public List<SelectOption> AllName{get;set;}
public List<String> SelectedName{get;set;}
public String discountScheduleID { get; set; }

private String strGeographix = '';

public RfpControllerExtension2 (){
    AllDept= new List<SelectOption>();
    AllDept= getDept();
    AllName= new List<SelectOption>();
    AllName= getName();

}

public void FindAllName()
    {
        system.debug('@@@@@@ Entered');
        AllName= new List<SelectOption>();
        AllName= getName();
    }  

public List<SelectOption> getDept()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Select','--Select--'));
        Schema.DescribeFieldResult departfield =  Geography__c.Country__c.getDescribe();
        List<Schema.PicklistEntry> picklist = departfield.getPicklistValues();
        for( Schema.PicklistEntry f : picklist){      
        options.add(new SelectOption(f.getLabel(), f.getValue()));
        }    
        return options;

     }

   public List<SelectOption> getName()
    {
        List<SelectOption> options = new List<SelectOption>();
        Set<String> sProj = new Set<String>();

        if(discountScheduleID != null )
            {
               for(Geography__c Proj  :[select Id, Canonical_Name__c, Type__c from Geography__c where status__c = 'active' AND Country__c =: discountScheduleID order by Canonical_Name__c] )
               {
                    options.add(new SelectOption(Proj.ID,Proj.Canonical_Name__c));
                    strGeographix += Proj.Canonical_Name__c + ', ';
               }
            }
            errorMap.put('selectedGeographies',Proj.Canonical_Name__c);

        return options;

     }

     public Map<String, String> errorMap {
        get {
            if (errorMap == null || errorMap.size() == 0) {
                errorMap = new Map<String, String> {
                    'selectedGeographies' => '',
                    'flightDates' => '',
                    'agency' => '',
                    'agencyContact' => ''
                };
            }
            return errorMap;
        }
        private set;
    }

}
9
  • Ankuli.. Now in the second selection , options list is having both values old and new..Can we have a solution where upon second selection options list should have values which are related to the newly selected country and my textbox should hold the old value..It sounds wierd.. But is there any way. Commented Sep 30, 2015 at 13:11
  • Please use above code. and understand my changes.
    – Ankuli
    Commented Sep 30, 2015 at 13:30
  • There will be an error in errorMap.put('selectedGeographies',Proj.Canonical_Name__c); line as Proj variable will be out of scope of loop. And secondly i am getting the same old and new values. I uploaded an pic.. Commented Sep 30, 2015 at 13:38
  • Ankuli.. You can see . After United Arab Emirates selected when i am selecting Switzerland, in the list i am getting the values for both United Arab Emirates & Switzerland.. Commented Sep 30, 2015 at 13:40
  • Ankuli.. The list is not clearing off.. I believe that is the issue... Commented Sep 30, 2015 at 13:46

You must log in to answer this question.

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