+ Start a Discussion
harsha vardhan vasa 9harsha vardhan vasa 9 

junction object fields not displaying on vf page

hi guys,
i have implemented vf to get the userterritory detatils for the entered username in search box.i am able to get the results from the soql query but not able to display on vf screen,
PFB code and let know where i went wrong.
VF Page:
<apex:page controller="Apexcontroller" >
    <apex:form >
        <apex:pageBlock >
            <apex:pageMessages id="showmsg"></apex:pageMessages>
            <apex:pageBlockSection >
                <apex:inputText value="{!searchstring}" label=" enter username here" />
                <apex:commandButton value="Search" action="{!search}" reRender="accpg,showmsg" />
            </apex:pageBlockSection>
            <apex:pageBlockTable value="{!usrter}" var="a" id="accpg" rendered="{!usrter.size!= 0}">
            <!--    <apex:column value="{!a.id}" /> -->
                <apex:column headerValue="user">
                   <apex:inputfield required="{false}" value="{!a.userid}" />
                </apex:column>
                <apex:column headerValue="Territory">
                   <apex:inputfield required="{false}" value="{!a.territoryid}" />
                </apex:column>
          <!--      <apex:column value="{!a.territoryid}" /> -->
            </apex:pageBlockTable>
            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="Save"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>  
</apex:page>

Controller:
public class Apexcontroller {
    public string searchstring{get;set;}
    public list<userterritory> usrter{get;set;} 
    public list<user> usrlist = new list<user>();
    public Apexcontroller(){
        usrter =new list<userterritory>();
    }
    public void search(){
        system.debug('value of searchstring entered is'+searchstring); 
        user[] usr =[select id from user where username =:searchstring];
        for(user u:usr){
            usrter=[select id,userid,territoryid,isactive from userterritory where userid =:u.id];
            system.debug('userterritory list is'+usrter);
            if(usrter.size() ==0){
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'No matching territories found for the entered username'));
            }
        }
    }
    public void save(){
    }
    
}
Deepali KulshresthaDeepali Kulshrestha
Hi harsha,
Greetings to you!

- I can help you but I need your object name and API name and their respective fields name and API names because 'userterritory' is a custom object and custom object appends '__c' so it should be 'userterritory__c' and its fields also appends __c.

- You are doing SOQL query within the for loop which can hit the governor limit and throw an error. Change your controller class to the below class :  -
public class Apexcontroller  {
        public string searchstring{get;set;}
        public list<userterritory> usrter{get;set;}
        public list<user> usrlist = new list<user>();
        public Apexcontroller (){
            usrter =new list<userterritory>();
        }
        public void search(){
            system.debug('value of searchstring entered is'+searchstring);
            List<User> usr =[select id from user where username =:searchstring Limit 10000];
            Set<Id> userIdSet = new Set<Id>();
            if(!usr.isEmpty()){
                for(user u:usr){
                    userIdSet.add(u.Id);
                }
            }
            usrter=[select id,userid,territoryid,isactive from userterritory where userid IN: userIdSet];
            system.debug('userterritory list is'+usrter);
            if(usrter.size() ==0){
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'No matching territories found for the entered username'));
            }

        }
        public void save(){
        }
    }

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.
harsha vardhan vasa 9harsha vardhan vasa 9
Hi Deepali,
thanks for ur time and effort.
userterritory is standard junction object  between user and territory objects in territory management.

Regards,
Harsha
8446546754.