+ Start a Discussion
aKallNVaKallNV 

using variables in Dynamic SOQL part II

Yesterday, I posted the following code because I couldn't get  the unitFilter variable to return a value inside of the getStrategyQuery() method. I managed to get the unitFilter variable working. Now I am stuck on the on selectedUnit variable. It's giving me the same problem...for some reason it's returning null inside of the method. I don't understand why and at a loss at how to get it. 

 

On vf page there is a picklist of selectOptions which are the units. When a user selects a particular unit the page should re-render. I can see in the viewState log that the selectedUnit variable is being populated with the selection made...so again, not sure why it's not being found in the method that uses it to create the query string getStrategyQuery()

 

thanks for any help.

 

 

 

 

public with sharing class narrativeWizard_Controller {
        
        public String accountID;
        public String swpnAccount { get; set; }
        public SchoolWorkPlanLog__c getLog { get; set; }
        public List<UniversalJoin__c> juncObs { get; set; }
        public List<cAIS> theWrappers { get; set; } 
        public Map<ID,List<cAIS>> theWrapperMap { get; set; }
        public List<DeployedStrategy__c> theDSs { get; set; }
        public List<cGS> theGoalWrappers { get; set; }
        public Set<String> unitOptions = new Set<String>();
        public String selectedUnit { get; set; }
        public Set<String> princyOptions = new Set<String>();
        public String selectedPrincy { get; set; }
        public Set<String> catyOptions = new Set<String>();
        public String selectedCaty { get; set; }
        public List<cCONS> theConWrappers { get; set; }
        public List<cCASES> theCaseWrappers { get; set; }
        public String unitFilter { get; set; }
        
        //sets up class extension of SchoolWorkPlanLog__c
        private final SchoolWorkPlanLog__c swpn;
                
        //CONSTRUCTOR
        public narrativeWizard_Controller(ApexPages.StandardController swpnController) {
                this.accountID = ApexPages.currentPage().getParameters().get('aID');
                this.swpn = (SchoolWorkPlanLog__c)swpnController.getRecord();
                this.setUpAccount();  
                this.makesStrategyWrappers();
                this.makesGoalWrappers();
                this.makesContactWrappers();
                this.makesCaseWrappers(); 
                this.getLog= new SchoolWorkPlanLog__c();
                this.getUnits(); 
                this.setUpUnitFilter();
                this.getAllStrategies();
        }
        
        public void setUpAccount() {
                Account acct = [select ID, Name from Account where ID = :accountID Limit 1];
                this.swpnAccount = acct.Name;
        }
        
        public String setUpUnitFilter() {
                ID UP = Userinfo.getUserId();
                User userX = [select ID, Name, Department__c, Unit__c from User where ID =:UP];         
                
                return userX.Unit__c;
        }       
        
        //sets up the query string for the makesStrategyWrappers method below
        private String getStrategyQuery() {
                unitFilter = setUpUnitFilter();
                String losFilters = '';
                if(selectedUnit == 'All') {
                	losFilters += '';
                }else if((null != selectedUnit) && (selectedUnit != 'All')) {
                	losFilters += ' AND Unit__c =:selectedUnit';
                }else if (null != unitFilter) {
                    losFilters += ' AND Unit__c =:unitFilter';
                }else {
                    losFilters += '';
                }
                
                return 'select ID, Name, Status__c, Strategy__r.Name,Strategy__r.Category__r.Name, Strategy__r.Category__r.Principle__r.Name, EndDate__c, Unit__c, StartDate__c, (select ID, Name, Name__c, Status__c, DeployedStrategy__r.Strategy__r.Name, EndDate__c, LatestRating__c from DeployedActionItem__r Order By ItemNumber__c ASC) from DeployedStrategy__c where Account__c = :accountID And Status__c = \'Active\''+losFilters;
        }
        
        //attempt at Dynamic Query
        public void makesStrategyWrappers() {
                //variables    
            theDSs = new List<DeployedStrategy__c>();
            List<cAIS> cAIS1 = new List<cAIS>();   
            theWrappers = new List<cAIS>();
            theWrapperMap = new Map<Id, List<cAIS>>();
            
            String queryString = this.getStrategyQuery();
            theDSs = Database.query(queryString);
            
            for(DeployedStrategy__c ds : theDSs) {
                
                for(DeployedActionItems__c actI : ds.DeployedActionItem__r) {
                        cAIS1 = theWrapperMap.get(ds.Id);
                    
                    if(null == cAIS1) {       
                            cAIS1 = new List<cAIS>();
                            theWrappers.add(new cAIS(actI)); 
                            theWrapperMap.put(ds.Id, cAIS1);                              
                    }
                    cAIS1.add(new cAIS(actI));
                }
            }
            
        } 
        
        
        //pulls all Strategies to populate filter options
        public void getAllStrategies() {
                
                         
            for(DeployedStrategy__c ds : [select ID, Name, Status__c, Strategy__r.Name,Strategy__r.Category__r.Name, Strategy__r.Category__r.Principle__r.Name, EndDate__c, Unit__c, StartDate__c, (select ID, Name, Name__c, Status__c, DeployedStrategy__r.Strategy__r.Name, EndDate__c, LatestRating__c from DeployedActionItem__r Order By ItemNumber__c ASC) from DeployedStrategy__c where Account__c = :accountID And Status__c = 'Active']) {
                
                unitOptions.add(ds.Unit__c);
                princyOptions.add(ds.Strategy__r.Category__r.Principle__r.Name);
                catyOptions.add(ds.Strategy__r.Category__r.Name);
                }
        }
         
         //makes Goal Wrappers
         public void makesGoalWrappers() {
                thegoalWrappers = new List<cGS>();        
                if(theGoalWrappers.size() <1) {
                        for(Goal__c gs1 : [select ID, Unit__c, Account__c, Name, Status__c, StartDate__c, EndDate__c, Pillar__c, DemographicType__c from Goal__c where Account__c = :accountID and Status__c = 'Active']) {
                                theGoalWrappers.add(new cGS(gs1));
                        }
                }
         }....more code but not relevant.

 

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

First of all I don't understand why are you not using any action in actionSupport but I assume your req do not need any action. I would suggest you to add a attribute immediate="true" in your action supoort ,

 

Just give it a try and let me know.

All Answers

Shashikant SharmaShashikant Sharma

Are you calling an AJAX function ( are you using actionFunction ) on change of picklist,  please share that part of you VFP that you use to make call on change of picklist? I think your actionFunction is immediate true and you are not passing parameter also, but would be better if I see your VFP code once.

aKallNVaKallNV

yes. I am using actionSupport on the picklist to reRender the table. I have added some stuff to try to debug, which are in bold below. I'm using the {Now()} statement to confirm that the section is actually rerendering...by seeing that the time stamp changes. The {!selectedUnit} statement shows me that the property is actually being populated by the selection being made. dt and dt2 are two more timestamps that declared inside of the getStrategyQuery() and makeStrategyWrappers() methods. These two show a time stamp when the page is opened...they have the same time as the first time stamp. However, when select a unit...only the first time stamp changes...dt and dt2 don't change, which tells me that the methods are not being called...but still not sure about what to do.

 

 

 

 

<apex:page sidebar="false" showHeader="true" standardController="SchoolWorkPlanLog__c" extensions="narrativeWizard_Controller" id="tt1">
    <apex:form >
      <apex:pageBlock mode="edit" id="pb1">  
      <apex:pageBlockButtons >
         <apex:commandButton action="{!processSelected}" value="Save"/>
      </apex:pageBlockButtons>  
      <apex:pageBlockSection title="{!swpnAccount}" columns="1">
              <!--<apex:inputField value="{!getLog.Subject__c}"/> Decided we didn't need a Subject but leaving in place in case decision is overruled.-->
              <apex:inputField value="{!getLog.InteractionDate__c}"/>
              <apex:inputField value="{!getLog.PreEngagementPlan__c}"/>
              <apex:pageBlockSection collapsible="true" columns="1" title="Narrative">
              <apex:inputField value="{!getLog.HoursSpent__c}"/>
              <apex:inputField value="{!getLog.Narrative__c}"/>
              </apex:pageBlockSection>
      </apex:pageBlockSection>
      <apex:pageBlockSection columns="1" collapsible="true" title="Next Steps">
          <b><apex:outputLabel value="Use the following fields to define your next step. Upon saving this record a new Log will be created with the Date and Plan that you specify here."/></b>
          <apex:inputField value="{!getLog.NextStepDate__c}"/>
          <apex:inputField value="{!getLog.NextStepPlan__c}"/>
      </apex:pageBlockSection>
      <apex:pageBlockSection columns="1" id="pbs1" title="Off-Plan?">
          <apex:outputPanel >
              <b><apex:outputLabel value="Check the box to indicate that none of the options below are appropriate:"/></b>
              <apex:actionRegion >
                  <apex:inputField value="{!getLog.OffPlan__c}">
                      <apex:actionSupport event="onclick" reRender="catField, pb2"/>
                  </apex:inputField>
              </apex:actionRegion>
          </apex:outputPanel>
      </apex:pageBlockSection>
      <apex:outputPanel id="catField">
      <apex:pageBlockSection rendered="{!showAIandGoal = false}" columns="1">
          <b><apex:outputText value="Find a category that best classifies this narrative:"/></b>
          <apex:inputField value="{!getLog.Category__c}"/>
      </apex:pageBlockSection>    
      </apex:outputPanel>      
      </apex:pageBlock>      
      <apex:pageBlock id="pb2">
      <apex:pageBlockSection title="Select the Work Plans, Goals, Cases and Contacts that this Entry pertains to." collapsible="false" rendered="{!showAIandGoal}" columns="1">
          <apex:tabPanel switchType="client" selectedTab="tab1" >
              <apex:tab label="Work Plans" id="tab1">
              <apex:pageBlockSection collapsible="false" columns="3">
              <apex:pageBlockSectionItem > <apex:outputLabel value="Filter by Unit"/> <apex:selectList value="{!selectedUnit}" size="1"> <apex:selectOptions value="{!Units}"/> <apex:actionSupport event="onchange" reRender="table1"/> </apex:selectList> </apex:pageBlockSectionItem>      
              <apex:pageBlockSectionItem >
              <apex:outputLabel value="Filter by Principle"/>
               <apex:selectList value="{!selectedPrincy}" size="1">
                  <apex:selectOptions value="{!Princys}"/>
                  <!--<apex:actionSupport event="onchange" reRender="pbt1"/>-->
               </apex:selectList>
              </apex:pageBlockSectionItem>
              <apex:pageBlockSectionItem >
              <apex:outputLabel value="Filter by Category"/>
               <apex:selectList value="{!selectedCaty}" size="1">
                  <apex:selectOptions value="{!Catys}"/>
                  <!--<apex:actionSupport event="onchange" reRender="pbt1"/>-->
               </apex:selectList>
              </apex:pageBlockSectionItem>
              </apex:pageBlockSection>
                  <apex:pageBlockSection collapsible="false" columns="1"  id="table1">
                     <br> {!Now()}</br> <br>{!selectedUnit}</br> <br> {!dt}</br> <br> {!dt2}</br>
                      <apex:pageBlockSectionItem >
                          <apex:pageBlockTable value="{!theDSs}" var="d"  >
                              <apex:column headerValue="Strategies">
                                  <b>{!d.Strategy__r.Name }</b>
                              </apex:column>
                              <apex:column headerValue="Unit">
                                  <b>{!d.Unit__c}</b>
                              </apex:column>
                              <apex:column headerValue="Start Date" width="125">
                                  <b><apex:outputText value="{0,date,MM-dd-yyyy}">
                                      <apex:param value="{!d.StartDate__c}"/>
                                  </apex:outputText></b>                  
                              </apex:column>
                              <apex:column breakBefore="true" colspan="3" >
                                  <apex:dataTable value="{!theWrapperMap[d.id]}" var="a" width="100%" border="5px">
                                      <apex:column >
                                          <apex:inputCheckbox value="{!a.selected}"/>
                                      </apex:column>
                                      <apex:column headerValue="Action Items">
                                          {!a.wAI.Name__c}
                                      </apex:column>
                                      <apex:column headerValue="Status">
                                          {!a.wAI.Status__c}
                                      </apex:column>
                                      <apex:column headerValue="Current Rating" width="110">
                                          {!a.wAI.LatestRating__c}
                                      </apex:column>
                                      <apex:column headerValue="New Rating">
                                          <apex:selectList size="1" value="{!a.selectedRating}">
                                              <apex:selectOptions value="{!a.ratings}"/>
                                          </apex:selectList>
                                      </apex:column>
                                  </apex:dataTable>
                              </apex:column> 
                          </apex:pageBlockTable>
                      </apex:pageBlockSectionItem>
                  </apex:pageBlockSection>
                  </apex:tab>
                  <apex:tab label="Goals" id="tab2">
                      <apex:pageBlockSection columns="1">
                      <apex:pageBlockSectionItem >
                      <apex:pageBlockTable value="{!theGoalWrappers}" var="d">
                          <apex:column >
                              <apex:inputCheckbox value="{!d.selected}"/>
                          </apex:column>              
                          <apex:column headerValue="Goals">
                             {!d.wG.Name }
                          </apex:column>
                          <apex:column headerValue="Pillars">
                              {!d.wG.Pillar__c}
                          </apex:column>
                          <apex:column headerValue="Unit">
                              {!d.wG.Unit__c}
                          </apex:column>
                          <apex:column headerValue="Start Date" width="125">
                              <apex:outputText value="{0,date,MM-dd-yyyy}">
                                  <apex:param value="{!d.wG.StartDate__c}"/>
                              </apex:outputText>                  
                          </apex:column>
                      </apex:pageBlockTable>
                      </apex:pageBlockSectionItem>
                   </apex:pageBlockSection>
               </apex:tab>
              <apex:tab label="Cases" id="tab3">
                  <apex:pageBlockSection columns="1">
                      <apex:pageBlockSectionItem >
                      <apex:pageBlockTable value="{!theCaseWrappers}" var="case">
                          <apex:column >
                              <apex:inputCheckbox value="{!case.selected}"/>
                          </apex:column>              
                          <apex:column headerValue="Cases">
                             {!case.wCase.CaseNumber }
                          </apex:column>
                          <apex:column headerValue="Title">
                              {!case.wCase.Subject}
                          </apex:column>
                      </apex:pageBlockTable>
                      </apex:pageBlockSectionItem>
                   </apex:pageBlockSection>
              </apex:tab>
              <apex:tab label="Contacts" id="tab4">
                  <apex:pageBlockSection columns="1">
                      <apex:pageBlockSectionItem >
                      <apex:pageBlockTable value="{!theConWrappers}" var="con">
                          <apex:column >
                              <apex:inputCheckbox value="{!con.selected}"/>
                          </apex:column>              
                          <apex:column headerValue="Contacts">
                             {!con.wCon.Name }
                          </apex:column>
                          <apex:column headerValue="Title">
                              {!con.wCon.Title}
                          </apex:column>
                      </apex:pageBlockTable>
                      </apex:pageBlockSectionItem>
                   </apex:pageBlockSection>
              </apex:tab>
          </apex:tabPanel>
          </apex:pageBlockSection> 
      </apex:pageBlock>
    </apex:form>   
</apex:page>

 

Shashikant SharmaShashikant Sharma

First of all I don't understand why are you not using any action in actionSupport but I assume your req do not need any action. I would suggest you to add a attribute immediate="true" in your action supoort ,

 

Just give it a try and let me know.

This was selected as the best answer
aKallNVaKallNV

The page behaved slightly differently by additing immediate = true attribute. 

 

The ultimate goal of getting the table to reRender did not happen. However, the debugging tags that I added behaved differently. All 3 timestamps worked the same as described above...which is that the first one always changes but the dt and dt2 do not change when the page is reRendered. The difference is that tag that is pulling the selectedUnit property into the page was not returning a value. Without the immeidate attribute it would show the value that was selected...with immediate attribute nothing is returned. 

aKallNVaKallNV

I got it. You were right. I needed to use the Action attribute to call the method. 

 

Thanks