0

I have this below method from which i am doing a rest api call and populating dropdown on vf page on button click.

I am also getting city values in Json response. After getting the dropdown I want when i will select any values in dropdown it should auto populate the city values in the inputtext field.

Apex -

public void sendNotification(){
        String requestEndpoint = integSettings.get('AgileEndpoint').Endpoint__c;
        jsonWrapper wrap = new jsonWrapper();
        wrap.zip= zip;
        wrap.type= 'K12';
        String jsonBody = json.serialize(wrap);
        result = new List<JSON2Apex>();
        HTTP auth = new HTTP();
        HTTPRequest r = new HTTPRequest();
        r.setHeader('Content-Type','application/json');
        r.setEndpoint(requestEndpoint );
        r.setMethod('POST');
        r.setBody(jsonBody);
        HTTPResponse authresp=new HttpResponse();
        authresp = auth.send(r);
        if(authresp.getStatusCode()==200){
        response = authresp.getBody();
        result = (List<JSON2Apex>)JSON.deserialize(response , LIST<JSON2Apex>.class);
        }else{
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Entered Zipcode doesnot exsits'));
        }

        options = new List<SelectOption>();

        results = new List<JSON2Apex>();
        for(JSON2Apex t : result){

            results.add(t);
            options.add(new SelectOption(t.institution_Long_Name, t.institution_Long_Name ));

        }
    }

    // Inner class for accepting zip parameter from visualforce page 
    public class jsonWrapper {
        public String zip{get; set;}
        public String type{get; set;}
    }

    public class JSON2Apex { 
        public String fault {get;set;}
        public String inst_UID {get;set;}
        public String institution_Long_Name {get;set;}
        public String mailing_City{get;set;}
        public String mailing_St{get;set;}

    }

Json response -

[{"fault":"","inst_UID":"648353","institution_Long_Name":"ALBERT LEA AREA LEARNING CNTR","mailing_City":"ALBERT LEA","mailing_St":"MN"},{"fault":"","inst_UID":"648347","institution_Long_Name":"ALBERT LEA SENIOR HIGH SCHOOL","mailing_City":"ALBERT LEA","mailing_St":"MN"},{"fault":"","inst_UID":"648348","institution_Long_Name":"HALVERSON ELEMENTARY","mailing_City":"ALBERT LEA","mailing_St":"MN"},{"fault":"","inst_UID":"648349","institution_Long_Name":"HAWTHORNE ELEMENTARY","mailing_City":"ALBERT LEA","mailing_St":"MN"},{"fault":"","inst_UID":"648350","institution_Long_Name":"LAKEVIEW ELEMENTARY","mailing_City":"ALBERT LEA","mailing_St":"MN"},{"fault":"","inst_UID":"648351","institution_Long_Name":"SIBLEY ELEMENTARY","mailing_City":"ALBERT LEA","mailing_St":"MN"},{"fault":"","inst_UID":"648352","institution_Long_Name":"SOUTHWEST MIDDLE SCHOOL","mailing_City":"ALBERT LEA","mailing_St":"MN"},{"fault":"","inst_UID":"779477","institution_Long_Name":"ST THEODORE ELEMENTARY SCHOOL","mailing_City":"ALBERT LEA","mailing_St":"MN"},{"fault":"","inst_UID":"766032","institution_Long_Name":"ST. THEODORE","mailing_City":"ALBERT LEA","mailing_St":"MN"}]

vf page -

                <div class="col-md-3">
                    <apex:outputLabel style="margin-bottom: 0px;font-weight: inherit;" >PickList from CallOut :</apex:outputLabel>
                    <div class="form-group">
                        <apex:selectList size="1" value="{!schoolName}">
                             <apex:selectOptions value="{!options}"/>
                        </apex:selectList>   
                    </div>
                </div> 
            </div> 

             <div class="col-md-3">
                    <apex:outputLabel style="margin-bottom: 0px;font-weight: inherit;" >Mailing City</apex:outputLabel>
                    <div class="form-group">
                        <apex:inputText value="{!Lead.AgileCity__c}" styleClass="form-control"/>
                    </div>
                </div>
  • What's wrong with it? – EricSSH Jul 26 '17 at 17:22
  • Upon selecting dropdown values how to populate the values . On button click i made the call and got the response. Now how to use that response to populate city on dropdown selection. – hellraizer Jul 26 '17 at 17:25
  • Where you loop through your result looks correct, is that object getting deserialized correctly? Why are you doing this results.add(t);? – EricSSH Jul 26 '17 at 17:34
  • Still you can filter JSON2Apex wrapper object. Take it into list iterate and get the value into selectOption – bharath Jul 26 '17 at 18:34
  • Bharath.. i am getting the values in selectoption. i need when i select institution_Long_Name from dropdown, its corresponding city which is there in json should get poplated in input text.. – hellraizer Jul 26 '17 at 18:39
0

You have to first create a MAP in Apex and populate it with different values of Schools and Citys as Key value pair. Access this map as JSON string in the VF page. Convert this JSON string into a javascript object. Now populate the input text field based on the School Name inside the onchange event of the <apex:selectList. I hope this will help.

Controller:

public class MyController {
    public List<SelectOption> options {get;set;}
    public String schoolName {get;set;}
    public Map<String, String> mapSchoolCity {get;set;}
    public String mapSchoolCityJSON {get{return JSON.serialize(mapSchoolCity);}}
    public MyController(){
        //Get the response from rest api call. I have hardcoded it for the demo.
        String jsonResp = '[{"fault":"","inst_UID":"648353","institution_Long_Name":"ALBERT LEA AREA LEARNING CNTR","mailing_City":"ALBERT LEA 1","mailing_St":"MN"},{"fault":"","inst_UID":"648347","institution_Long_Name":"ALBERT LEA SENIOR HIGH SCHOOL","mailing_City":"ALBERT LEA 2","mailing_St":"MN"},{"fault":"","inst_UID":"648348","institution_Long_Name":"HALVERSON ELEMENTARY","mailing_City":"ALBERT LEA 3","mailing_St":"MN"},{"fault":"","inst_UID":"648349","institution_Long_Name":"HAWTHORNE ELEMENTARY","mailing_City":"ALBERT LEA 4","mailing_St":"MN"},{"fault":"","inst_UID":"648350","institution_Long_Name":"LAKEVIEW ELEMENTARY","mailing_City":"ALBERT LEA 5","mailing_St":"MN"},{"fault":"","inst_UID":"648351","institution_Long_Name":"SIBLEY ELEMENTARY","mailing_City":"ALBERT LEA 6","mailing_St":"MN"},{"fault":"","inst_UID":"648352","institution_Long_Name":"SOUTHWEST MIDDLE SCHOOL","mailing_City":"ALBERT LEA 7","mailing_St":"MN"},{"fault":"","inst_UID":"779477","institution_Long_Name":"ST THEODORE ELEMENTARY SCHOOL","mailing_City":"ALBERT LEA 8","mailing_St":"MN"},{"fault":"","inst_UID":"766032","institution_Long_Name":"ST. THEODORE","mailing_City":"ALBERT LEA 9","mailing_St":"MN"}]';
        List<JSON2Apex> result = (List<JSON2Apex>)JSON.deserialize(jsonResp , LIST<JSON2Apex>.class);
        options = new List<SelectOption>();
        options.add(new SelectOption('','Select School' ));
        mapSchoolCity = new Map<String, String>();
        for(JSON2Apex t : result){
            options.add(new SelectOption(t.institution_Long_Name, t.institution_Long_Name ));
            mapSchoolCity.put(t.institution_Long_Name, t.mailing_City);
        }       
    }
    public class JSON2Apex { 
        public String fault {get;set;}
        public String inst_UID {get;set;}
        public String institution_Long_Name {get;set;}
        public String mailing_City{get;set;}
        public String mailing_St{get;set;}
    }   
}

VF Page:

<apex:page controller="MyController" >
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"/>    
    <apex:form>
        <apex:selectList size="1" value="{!schoolName}" onchange="populateCity(this.options[this.selectedIndex].value);">
            <apex:selectOptions value="{!options}"/>
        </apex:selectList>
        <apex:inputText id="cityId"/>
    </apex:form>
    <script type="text/javascript">
        var j$ = jQuery.noConflict();
        var obj = j$.parseJSON('{!JSENCODE(mapSchoolCityJSON)}');
        console.log(obj);
        function populateCity(cityName){
            j$('[id*="cityId"]').val(obj[cityName]);
        }
    </script>        
</apex:page>

Screenshot:

In your JSON String, every Scholl has the same mailing_City as "ALBERT LEA". I have modified it and added 1,2,3.. to differentiate between different selections.

enter image description here

  • I hope my answer is helpful to you. If this answer has solved your question please mark it as accepted answer. That will help others who are looking at it to know the accepted solution. – Saroj Bera Aug 1 '17 at 6:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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