0

I have a visualforce page which calls a controller method via actionFunction. When I reRender the pageBlockSection, the variable values get reset.

There is no method that resets the variable values. I changed the reRender from the pageBlockSection to the pageBlockTable under it. The values are retained.

Edit:-

           public with sharing class WorkflowRouteEditController {
              Workflow_Route__c route;
              public Boolean displayPopup {get;set;}
              public String SelectedAnswer{get;set;}
              public List<String> dummy1{get;set;}
              public List<String> dummy2{get;set;}
              public String SelectedValue{get;set;}
              public String PossibleValue{get;set;}
              public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
              public Map<ID,List<SelectOption>> MapOfCriteriaToAnswerOptions{get;set;}
              public Map<ID,Workflow_Criteria__c> MapOfIDToCriteria{get;set;}
              public Map<ID,AnswerWrapper> MapOfCriteriaToWrapper{get;set;}
              public Map<ID,List<String>> MapOfCriteriaToSelectedAnswerOptions{get;set;}
              public Map<ID,List<SelectOption>> MapOfCriteriaToSelectedOptions{get;set;}
              public Boolean toDelete=false;
              public Boolean isRecordNew=false;

                /*answer wrapper which associates the questions for which the answers are to be selected*/
              public class AnswerWrapper {
                public Workflow_Criteria__c crit {get; private set;}
                public Workflow_Answer__c answer {get; private set;}

                private AnswerWrapper (Workflow_Criteria__c crit) {
                    this.crit = crit;
                }
                public String getDisplayAnswer() {
                    if (answer != null && String.isNotBlank(answer.values__c )) 
                    {          
                       return answer.values__c.replace('\n','| '); 
                    }

                    return null;
                }

              }

              public List<AnswerWrapper>wrapperList {get; private set;}


              /*method that saves the route and the answers when the save button on the route page is clicked*/
              public PageReference SaveValues() {
                List<Workflow_Answer__c> AnswersToDelete=new List<Workflow_Answer__c>();
                List<Workflow_Answer__c> AnswersToUpsert=new List<Workflow_Answer__c>();
                saveRoute();
                PageReference WorkflowRoutes = new PageReference('/'+route.Schedule_Workflow__c);
                WorkflowRoutes.setRedirect(true);
                return WorkflowRoutes;
              }  

                //constructor
                public WorkflowRouteEditController (ApexPages.StandardController std) {
                  List<String> FieldList=new List<String>{'Schedule_Workflow__r.Name'};
                  MapOfCriteriaToAnswerOptions=new Map<ID,List<SelectOption>>();
                  MapOfCriteriaToSelectedOptions=new Map<ID,List<SelectOption>>();
                    dummy1=new List<String>();
                    dummy2=new List<String>();
                    displayPopUp=false;
                    if(!Test.isRunningTest())std.addFields(FieldList);
                    route = (Workflow_Route__c) std.getRecord();  
                    if(String.isBlank(route.id)) 
                    {
                      isRecordNew=true;
                    }
                    prepareWrapperList();
                    PopulatePickListValues();

                }

                /*method call happens when the cancel button on the route page is clicked*/
                public PageReference CancelMethod() {
                /*If the page is edited, then cancel will redirect to the record page*/
                PageReference WorkflowRoute = new PageReference('/'+route.Schedule_Workflow__c);

                if(toDelete&&isRecordNew) {      
                  Delete route;      
                }
                if(route.Schedule_Workflow__c!=null) {
                  WorkflowRoute.setRedirect(true);
                  return WorkflowRoute;
                }
                //If the page is a new record, then it will redirect to the workflow view page
                String RetURL=ApexPages.currentPage().getParameters().get('retURL');
                PageReference CancelPage=new PageReference(RetURL);
                CancelPage.setRedirect(true);
                return CancelPage;  

              }

                /*method is called on page load as well as whenever there is a saving of answer and route records to populate the page with the saved answer values*/
                private void prepareWrapperList() {
                  MapOfCriteriaToWrapper=new Map<ID,AnswerWrapper>();
                  MapOfCriteriaToSelectedAnswerOptions=new Map<ID,List<String>>();
                  MapOfCriteriaToSelectedOptions=new Map<ID,List<SelectOption>>();
                  // Get Answers for this route
                map<id,Workflow_Answer__c>answerMap = new map<id,Workflow_Answer__c>();  // keyed by criteria id

                system.debug('the route id is'+route.id);
                for (Workflow_Answer__c answer : [ select id, name, values__c, Workflow_Criteria__c
                                      from Workflow_Answer__c
                                         where Workflow_Route__c = :route.id]) {


                  answerMap.put(answer.Workflow_Criteria__c, answer);                      
                }                        

                /*Get Criteria (questions) for this workflow and prepare wrappers with answers using new answers where needed.*/
                  wrapperList = new List<AnswerWrapper>();

                  for (Workflow_Criteria__c crit : [ select id, name, Picklist_Field_Name__c, Search_Object_Name__c
                                        from Workflow_Criteria__c
                                        where Schedule_Workflow__c = :route.Schedule_Workflow__c
                                        order by Criteria_Order__c asc]) {
                    AnswerWrapper wrap = new AnswerWrapper(crit);

                    wrapperList.add(wrap);
                    Workflow_Answer__c answer = answerMap.get(crit.id);
                    if (answer == null) {
                      answer = new Workflow_Answer__c(Name = crit.name);
                    }  
                    wrap.answer = answer;
                    List<String> AnswerList=new List<String>();
                    if(wrap.answer.values__c!=null)  
                    AnswerList=wrap.answer.values__c.split('\\|');

                    MapOfCriteriaToSelectedAnswerOptions.put(crit.ID,AnswerList);
                    for(String AnswerRec:AnswerList) {
                      if(MapOfCriteriaToSelectedOptions.containsKey(crit.ID)) {
                        MapOfCriteriaToSelectedOptions.get(crit.ID).add(new SelectOption(AnswerRec.trim(),AnswerRec.trim()));
                      }
                      else {
                        MapOfCriteriaToSelectedOptions.put(crit.ID,new List<SelectOption>{new SelectOption(AnswerRec.trim(),AnswerRec.trim())});
                      }
                    }
                    if(String.isBlank(wrap.answer.values__c)) {
                      MapOfCriteriaToSelectedOptions.put(crit.ID,new List<SelectOption>{new SelectOption('','')});
                    }
                    //MapOfIDToCriteria.put(crit.ID,crit);  
                    MapOfCriteriaToWrapper.put(crit.ID,wrap);                  
                  }


                }  // end of prepareWrapperList()

                 /*method call happens when clicked on 'click to specify answer'link to show popup which displays the list of answer values that can be selected*/   
                 public void showPopup()
                {

                displayPopup = true;    
                system.debug('SELECTED ANSWER IS--'+SelectedAnswer);
                prepareWrapperList();
                PopulatePickListValues();
                }

                //closes popup on canceling the selection of answer values from the answer select popup
                public void closePopup() {
                    displayPopup = false;  


                }

                /*method is called when answer values are selected for a certain criteria and the 'save' button on the answer select popup is clicked 
                this method saves (inserts/updates) the answer values along with insertion of a route if one doesnt already exist*/
                public PageReference redirectPopup()
                {
                   displayPopup = false;

                   if(MapOfCriteriaToWrapper.containsKey(SelectedAnswer)) {
                     List<String> IndividualAnswers=new List<String>();
                      List<SelectOption> AvailableOptions=new List<SelectOption>();
                     if(String.isNotBlank(SelectedValue)) {
                         IndividualAnswers=SelectedValue.split(',');
                       for(Integer i=0;i<IndividualAnswers.size();i++) {

                         SelectOption CurrentSelect=new SelectOption(IndividualAnswers[i],IndividualAnswers[i]);
                         if(i==0) {

                           if(String.isNotBlank(IndividualAnswers[i])&&IndividualAnswers[i]!=null) {  

                             MapOfCriteriaToWrapper.get(SelectedAnswer).answer.values__c=IndividualAnswers[i];
                           }
                           else {
                             MapOfCriteriaToWrapper.get(SelectedAnswer).answer.values__c='';
                             continue;
                           }
                         }
                         else {
                           if(String.IsBlank(MapOfCriteriaToWrapper.get(SelectedAnswer).answer.values__c)) {
                             MapOfCriteriaToWrapper.get(SelectedAnswer).answer.values__c+=IndividualAnswers[i];
                           }
                           else {
                             MapOfCriteriaToWrapper.get(SelectedAnswer).answer.values__c+='|'+IndividualAnswers[i];
                           }
                         }

                         AvailableOptions.add(CurrentSelect);
                       }

                       MapOfCriteriaToSelectedOptions.put((ID)SelectedAnswer,AvailableOptions);
                       MapOfCriteriaToSelectedAnswerOptions.put((ID)SelectedAnswer,IndividualAnswers);
                     }
                     else {
                       MapOfCriteriaToWrapper.get(SelectedAnswer).answer.values__c='';
                       MapOfCriteriaToSelectedOptions.put((ID)SelectedAnswer,new List<SelectOption>{new SelectOption('','')});
                       MapOfCriteriaToSelectedAnswerOptions.put((ID)SelectedAnswer,new List<String>{''});
                     }

                     if(MapOfCriteriaToWrapper.get(SelectedAnswer).answer.ID!=null) {
                       update MapOfCriteriaToWrapper.get(SelectedAnswer).answer;
                     }
                     else {
                        system.debug('route id is'+route.ID);
                         if(route.ID==null) {
                            saveRoute();
                         }
                       MapOfCriteriaToWrapper.get(SelectedAnswer).answer.Workflow_Criteria__c=SelectedAnswer;
                       MapOfCriteriaToWrapper.get(SelectedAnswer).answer.Workflow_Route__c=route.ID;
                       insert MapOfCriteriaToWrapper.get(SelectedAnswer).answer;
                       toDelete=true;
                    }

                    MapOfCriteriaToAnswerOptions=new Map<ID,List<SelectOption>>();
                    prepareWrapperList();
                    PopulatePickListValues();

                   }

                   return null;   
                }  

                /*method call happens when eithe the 'Save' button on the Route page is clicked 
                or when 'Save' button on the answer select popup is clicked but a route is not already present*/
                public void saveRoute() { 
                  if(route.Id==null) {
                    Decimal MaxRouteOrderAlreadyExisting=0;
                    MaxRouteOrderAlreadyExisting=maxOrderOfRouteOnWorkflow();
                    route.Priority__c=(MaxRouteOrderAlreadyExisting+1);
                    }
                    upsert route;

                }

                //updates the order field on route 
                public Decimal maxOrderOfRouteOnWorkflow(){
                Decimal maxRouteOrder=0;
                List<Workflow_Route__c> lstAlreadyExistingRoutes=new List<Workflow_Route__c>();
                lstAlreadyExistingRoutes=[Select id,Priority__c from Workflow_Route__c where Schedule_Workflow__c=:route.Schedule_Workflow__c ORDER BY Priority__c DESC];
                if(lstAlreadyExistingRoutes!=null && lstAlreadyExistingRoutes.size()>0){
                  maxRouteOrder=lstAlreadyExistingRoutes[0].Priority__c;
                  return maxRouteOrder;
                }
                else{
                  return maxRouteOrder;
                }
              }

                // populates the possible answer values which can be selected on the answer value select popup based on the picklist field the question is based on
               public void PopulatePickListValues() {
                 MapOfCriteriaToAnswerOptions=new Map<ID,List<SelectOption>>();
                  for(AnswerWrapper Wrap:wrapperList) {
                    Set<String> ExistingValues=new Set<String>();

                    ExistingValues.addAll(MapOfCriteriaToSelectedAnswerOptions.get(Wrap.crit.ID));
                    List<Schema.PicklistEntry> PickListEntries=new List<Schema.PicklistEntry>();
                      if(schemaMap.containsKey(wrap.crit.Search_Object_Name__c)) {
                  Map<String,Schema.SObjectField> MapOfFieldToObjectField=schemaMap.get(wrap.crit.Search_Object_Name__c).getDescribe().fields.getMap();
                  //Iterate through all the fields of the Sobject
                  for(String sobjField : MapOfFieldToObjectField.keyset()) {        
                                if(sobjField.equalsIgnoreCase(Wrap.crit.Picklist_Field_Name__c)) {
                                  //Find the controlling field of the selected picklist
                                  PickListEntries = MapOfFieldToObjectField.get(sobjField).getdescribe().getPicklistValues();

                                }
                            }
                  }

                    for( Schema.PicklistEntry CurrentValue : PickListEntries)
                   {

                       if(MapOfCriteriaToAnswerOptions.containsKey(Wrap.crit.ID)) {
                         if(!ExistingValues.contains(CurrentValue.getLabel()))
                         MapOfCriteriaToAnswerOptions.get(Wrap.crit.ID).add(new SelectOption(CurrentValue.getLabel(), CurrentValue.getValue()));
                       }
                       else {
                         if(!ExistingValues.contains(CurrentValue.getLabel()))
                         MapOfCriteriaToAnswerOptions.put(Wrap.crit.ID,new List<SelectOption>{new SelectOption(CurrentValue.getLabel(), CurrentValue.getValue())});
                       }

                   } 

                  }


                }

            } 

The route.ID value always seems to be null. This causes multiple Route records to get inserted on save. There is no reRender of the entire form. It is confusing as to why the route object instance is getting created again.

  • post full code of controller/extension – Oleksandr Berehovskyi Nov 24 '17 at 12:28
  • Please look into the updated description. – Sai Krishna Nov 27 '17 at 7:42
2

Attribute immediate="true" present in commandButton. This attribute does not fire getters or setters, defines that validation rules should be skipped and field values would set to blank values.

refer blog for more info

| improve this answer | |
  • The field values are not an issue. I understand the purpose of immediate="true". Will it reset the controller variable values? – Sai Krishna Nov 23 '17 at 10:07
  • @SaiKrishna are field values populated during constructing controller our user enters values? – Oleksandr Berehovskyi Nov 24 '17 at 7:31
  • field values are entered by the user. Back end variables which query the data and store it in a List get reset. – Sai Krishna Nov 24 '17 at 12:19

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.