+ Start a Discussion
vamsi krishna 106vamsi krishna 106 

Vf page is not redirecting properly. Some times it redirecting but at some times it's not redirecting

Hi Guys,
I have parent and child and when i click on new(child related list button)it goes to the one vf page along with parent id. based on that parent id i am displaying last record from child(only one record) then if i click on save i need to create new record.This procces is going good but when i click on Save button i need to redirect to the parent record view page.(Some times it going very nice but in some times it's not redirecting to the parent record view page )but insert done..please help me ..Thanks in advance..
This is VF Page
<!--
Name:SBWEBVersionButton
Created By : VamsiKrishna
Description: Creating the TUR(Tissues Utilization Record) based on the Tissue.
Modifications:Mobility
-->
<apex:page standardController="Tissue__c" extensions="SBWEBVersioningCLS" sidebar="false" docType="html-5.0">
  <head>
    <script type='text/javascript' src='/canvas/sdk/js/publisher.js'/>
    <script src="https://mobile1.t.salesforce.com/soap/ajax/30.0/connection.js" type="text/javascript" />
  <apex:stylesheet value="{!URLFOR($Resource.SLDS0110, 'assets/styles/salesforce-lightning-design-system-vf.css')}" />
  <!--TURVersion CSS Resource--->
              <apex:stylesheet value="{!URLFOR($Resource.CBCSS, 'CBCSS/CBCSS/TURVersion.css')}" />
              <apex:stylesheet value="{!URLFOR($Resource.CBCSS,'CBCSS/calendar/calendar_blue.css')}" />
               <apex:stylesheet value="{!URLFOR($Resource.CBCSS,'CBCSS/CBCSS/jquery.mobile.datepicker.css')}" />
                <apex:stylesheet value="{!URLFOR($Resource.CBCSS,'CBCSS/CBCSS/jquery.mobile.datepicker.theme.css')}" />
              <apex:stylesheet value="http://code.jquery.com/mobile/git/jquery.mobile-git.css" /> 
            <!--Jquery Resource CDN-->
              <apex:includeScript value="//code.jquery.com/jquery-1.11.3.min.js"  />
                <apex:includeScript value="http://code.jquery.com/mobile/git/jquery.mobile-git.js"  />
                <apex:includeScript value="//code.jquery.com/jquery-migrate-1.2.1.min.js"  />
                <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
               <!--Jquery Resource CDN End-->
              <apex:includeScript value="{!URLFOR($Resource.CBCSS,'CBCSS/calendar/calendar.js')}"/>
              <apex:includeScript value="{!URLFOR($Resource.CBCSS, 'CBCSS/CBCSS/TURversionJs.js')}"/>
                  <apex:includeScript value="{!URLFOR($Resource.CBCSS, 'CBCSS/CBCSS/datepicker.js')}"/>
              <script src="//code.jquery.com/jquery-1.10.2.js"></script>
              <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
              <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" />
               <link rel="stylesheet" href="https://jqueryui.com/jquery-wp-content/themes/jqueryui.com/style.css"/>
               <link rel="stylesheet"  href="http://code.jquery.com/mobile/git/jquery.mobile-git.css" /> 
               <style>
               #ui-datepicker-div{
               width:300px;
              }
               .overlay {
                    background-color:#000;
                    position: absolute;
                    left: 0;
                    top: 0;
                    width: 100%;
                    height: 100%;
                    opacity:0.3;
            }
               </style>
              <script type="text/javascript">
        $(function(){
              $(function(){
                $("[id$='pdob']").datepicker({ maxDate: 0 , dateFormat: 'mm/dd/yy' }).on('changeDate', function(ev){
                    $('#sDate1').text($('#pdob').data('date'));
                    $('#pdob').datepicker('hide');
                });
               })
            })
             $(function(){
              $(function(){
                $("[id$='producerDate']").datepicker({ maxDate: 0 , dateFormat: 'mm/dd/yy' }).on('changeDate', function(ev){
                    $('#sDate1').text($('#pdob').data('date'));
                    $('#producerDate').datepicker('hide');
                });
               })
            })
            $(function () {
            $("[id$='pfirstname']").keydown(function (e) {
            if (e.shiftKey || e.ctrlKey || e.altKey) {
            e.preventDefault();
                    } else {
                    var key = e.keyCode;
                        if (!((key == 8) || (key == 32) || (key == 46) || (key >= 35 && key <= 40) || (key >= 65 && key <= 90))) {
                    e.preventDefault();
                    }
                }
                });
                });
                
                 $(function () {
            $("[id$='plastname']").keydown(function (e) {
            if (e.shiftKey || e.ctrlKey || e.altKey) {
            e.preventDefault();
                    } else {
                    var key = e.keyCode;
                        if (!((key == 8) || (key == 32) || (key == 46) || (key >= 35 && key <= 40) || (key >= 65 && key <= 90))) {
                    e.preventDefault();
                    }
                }
                });
                });
        function pdobvalidation(){
        var date=$("[id$='pdob']").val();
        var inputDate = new Date(date);
        var finalDate=inputDate.setHours(0,0,0,0);
        var today = new Date();
        var finaltoday=today.setHours(0,0,0,0);
        if(finalDate>finaltoday){
        $("[id$='IMDateErorr']").text("Patient Date of Birth must not be future Date");
                    
        return false;
        }else{
        $("[id$='IMDateErorr']").text("");  
        $("#finalerror").text("");  
        }
        }
         function producerDatevalidation(){
        var date=$("[id$='producerDate']").val();
        var inputDate = new Date(date);
        var finalDate=inputDate.setHours(0,0,0,0);
        var today = new Date();
        var finaltoday=today.setHours(0,0,0,0);
        if(finalDate>finaltoday){
        $("[id$='IMDateErorr2']").text("Procedure Date must not be future Date");            
        return false;
        }else{
        $("[id$='IMDateErorr2']").text("");
        $("#finalerror").text("");    
        }
        }
           
          function savingTUR(){
        
        var surgenname= $("[id$='contactnamenew']").val();
        var contactid=$("[id$='contactidnew']").val();
        if(surgenname==''){
         var surgenVal='';
        $("[id$='contactidnew']").val(surgenVal);
        }else if(surgenname && contactid==''){
        $("#contactiderorr").text("Please Select Proper Surgen Name");
        return false;
        }
        var error1=$("#IMDateErorr").text();
        var error2=$("#IMDateErorr2").text();
        var error3=$("#contactiderorr").text();
       if(error1.length==0 && error2.length==0 && error3.length==0){
         $("#finalerror").text("");
        $(".overlay").show();
        savingTURDetail();
        }else{
        $("#finalerror").text("Please check the Errors");
        return false;
         }
       }
           
        </script>
          <script> 
        function goBacktoParent() { 
            Sfdc.canvas.publisher.publish({name: "publisher.close", payload:{ refresh:"true"}});
        }
    </script>

  </head>
<body>

  <div class="slds">
  <apex:form >
  <apex:actionFunction action="{!CreateTUR}" name="savingTURDetail"  oncomplete="goBacktoParent();" status="sts" immediate="true"/>
    <div class="maindiv">
         <div class="divclass">
             <span class="spanone"><label><b>Tissue Name</b></label></span>
              <span class="spantwo"><apex:outputField value="{!tis.name}"/></span>
         </div>
         <br/>
         <div class="divclass">
              <span class="spanone"><label><b>Surgeon Name</b></label></span>
              <span class="spantwo">
              <apex:inputText html-autocomplete="off"  id="contactnamenew" value="{!contactname}" onkeyup="gensurgenname.call($(this));" styleClass="slds-input" style="max-width:300px" />
               <div id="contactiderorr" style="color:red"></div>
               <div class="drpdwnlist"></div>
          </span>
         </div>
       <div class="divclass">
              <span class="spanone"><label><b>Patient First Name</b></label></span>
              <span class="spantwo"><apex:inputtext value="{!Patient_First_Name}" id="pfirstname" styleClass="slds-input" style="max-width:300px" />
             </span>
         </div>
         <br/>
         <div class="divclass">
              <span class="spanone"><label><b>Patient Last Name</b></label></span>
              <span class="spantwo"><apex:inputtext value="{!Patient_Last_Name}" id="plastname" styleClass="slds-input" style="max-width:300px"/>
              </span>
         </div>
         <br/>
         <div class="divclass">
              <span class="spanone"><label><b>Patient DOB</b></label></span>
              <span class="spantwo">
            <apex:inputText id="pdob" size="10" styleClass="slds-input"  value="{!PatientDOB}" style="max-width:300px" onkeyup="return pdobvalidation();"/>
            
            <div id="IMDateErorr" style="color:red"></div>
            </span>
         </div>
         <br/>
         <div class="divclass">
              <span class="spanone"><label><b>Gender</b></label></span>
              <span class="spantwo">
              <apex:selectList id="gender" value="{!Genders}"  size="1" styleClass="slds-input" style="max-width:300px">
                <apex:selectOptions value="{!gender}" />
               </apex:selectList>
              </span>
         </div>
         <br/>
         <div class="divclass">
             <span class="spanone"> <label><b>Procedure Type</b></label></span>
              <span class="spantwo">
              <apex:selectList id="procedureType" value="{!Procedure_Type}"  size="1" styleClass="slds-input" style="max-width:300px">
               <apex:selectOptions value="{!ProducerType}"/>
               </apex:selectList></span>
         </div>
         <div class="divclass">
              <span class="spanone"><label><b>Procedure Type Comments</b></label></span>
              <span class="spantwo"><apex:inputTextarea value="{!Procedure_Type_Comments}" styleClass="slds-input" style="max-width:300px"/></span>
         </div>
         <br/>
         <div class="divclass">
              <span class="spanone"><label><b>Procedure Date</b></label></span> 
              <span class="spantwo"><apex:inputtext style="max-width:300px" styleClass="slds-input" size="10" id="producerDate"  value="{!Procedure_Date}" onkeyup="return producerDatevalidation();" />
                  <div id="IMDateErorr2" style="color:red"></div>
              </span>
         </div>
         <br/>
         <div class="divclass">
              <span class="spanone"><label><b>Procedure Code(1)</b></label></span>
              <span class="spantwo"><apex:inputtext value="{!Procedure_Code_1}" styleClass="slds-input" style="max-width:300px"/></span>
         </div>
         <br/>
         <div class="divclass">
              <span class="spanone"><label><b>Procedure Code(2)</b></label></span>
              <span class="spantwo"><apex:inputtext value="{!Procedure_Code_2}" styleClass="slds-input" style="max-width:300px"/></span>
         </div> 
         <br/>
         <div class="divclass">
              <span class="spanone"><label><b>Comments</b></label></span>
              <span class="spantwo"><apex:inputTextarea value="{!Comments}" styleClass="slds-input" style="max-width:300px"/></span>
         </div>
         <div class="divclass">
               <span class="spanone"><label><b>Pending</b></label></span>
              <span class="spantwo">
              <apex:selectList id="pending" size="1" value="{!Pendings}" styleClass="slds-input" style="max-width:300px">
                  <apex:selectOptions value="{!pendingNEw}"/>
              </apex:selectList></span>
              
         </div>
         <div class="divclass">
               <span class="spanone"><label><b>Source</b></label></span>
              <span class="spantwo">
              <apex:selectList value="{!Sources}" size="1" styleClass="slds-input" style="max-width:300px">
                  <apex:selectOptions value="{!Source}"/>
              </apex:selectList>
              </span>
         </div>    
         <apex:inputText value="{!contactid}"  styleClass="dummy" style="display:none" id="contactidnew" />
         <div id="finalerror" style="color:red"></div>
         <div class="divclass">
              <apex:commandButton value="Save"  onclick="return  savingTUR();"  styleClass="btnclck">
              </apex:commandbutton>
             <div> <apex:pagemessages /></div>
         </div>
        </div>
  </apex:form>
  </div>
  <div class="overlay" style="display:none">
     <center><apex:image url="{!$Resource.Saving_Icon}" height="50" width="50" style="top:45%; left:45%;position: absolute;"  /></center>
    </div>
  </body>
   
</apex:page>
 
/*
Created By : VamsiKrishna
Description: This is related to the TUR(Tissue Utilization Record) Versioning.This will Run when u click on Version  
Custom Buttion and It show the editable VF page if that tissue have the active TUR and now if u edit that TUR it create the new TUR and existing one is in in-active.
otherwise it show one new form to Create the new tur Related to that tissue.
*/
public without sharing class SBWEBVersioningCLS
{
   public string contactname{get;set;}
    public id contactid{get;set;}
    public string Patient_Last_Name{get;set;}
    public string Patient_First_Name{get;set;}
    public string PatientDOB{get;set;}
    public string Pending{get;set;}
    public string Pendings{get;set;}
    public string Procedure_Code_1{get;set;}
    public string Procedure_Code_2{get;set;}
    public string Procedure_Date{get;set;}
    public string Procedure_Type{get;set;}
    public string Genders{get;set;}
    public string Procedure_Type_Comments{get;set;}
    public string Comments{get;set;}
    public string Sources{get;set;}
    public tissue__c tis{get;set;}
    public tissue__c oldtisdetails{get;set;}
    public list<TUR__c> tur{get;set;}
    public Boolean isMobile{get;set;}
    public id turid;
                public SBWEBVersioningCLS(ApexPages.StandardController controller) {
                    this.oldtisdetails=(tissue__c )controller.getRecord();
                    //Here we are fetching the Active TUR detials
                       tur=new list<TUR__c>();
                       tis=new tissue__c();
                        try{
                        tis=[SELECT id,name from Tissue__c WHERE id=:oldtisdetails.id];
                            tur=[SELECT id,Tissue__c,Surgeon_Name__r.name,Surgeon_Name__r.id,Source__c,Gender__c,Active__c,Surgeon_Name__c,Patient_First_Name__c,Patient_Last_Name__c,Procedure_Code_1__c,
                                 Procedure_Code_2__c,Procedure_Date__c,Procedure_Type__c,Procedure_Type_Comments__c,PatientDOB__c,Pending__c,
                                 Comments__c FROM TUR__c WHERE Active__c=true AND tissue__c=:oldtisdetails.id];
                           }catch(exception e){}
                           if(!tur.isEmpty()){
                               for(TUR__c tr:tur){
                              Sources=tr.Source__c;
                              Comments=tr.Comments__c ;
                              Procedure_Type_Comments=tr.Procedure_Type_Comments__c;
                              Genders=tr.Gender__c;
                              Procedure_Type=tr.Procedure_Type__c;
                              //Procedure_Date=string.valueof(tr.Procedure_Date__c);
                              if(tr.Procedure_Date__c!=NULL){
                              String strProDate=string.valueof(tr.Procedure_Date__c);
                              String[] arrProDate = strProDate.split('-');
                             Procedure_Date=arrProDate[1]+'/'+arrProDate[2]+'/'+arrProDate[0];
                              }
                              Procedure_Code_2=tr.Procedure_Code_2__c;
                              Procedure_Code_1=tr.Procedure_Code_1__c;
                              Pendings=tr.Pending__c;
                              if(tr.PatientDOB__c!=NULL){
                               String strPDOB=string.valueof(tr.PatientDOB__c);
                              String[] arrpDOB = strPDOB.split('-');
                             PatientDOB=arrpDOB[1]+'/'+arrpDOB[2]+'/'+arrpDOB[0]; 
                            } 
                             Patient_First_Name=tr.Patient_First_Name__c;
                             Patient_Last_Name=tr.Patient_Last_Name__c;
                             //PatientDOB=tr.PatientDOB__c;
                             contactname=tr.Surgeon_Name__r.name;
                             contactid=tr.Surgeon_Name__c;
                          }
                      }
                      if(String.isNotBlank(ApexPages.currentPage().getParameters().get('sfdcIFrameHost')) || String.isNotBlank(ApexPages.currentPage().getParameters().get('sfdcIFrameOrigin'))){
                           isMobile = true;
                      }else{isMobile = false;}
            }
            public List<SelectOption> getProducerType(){
                   List<SelectOption> pTypes= new List<SelectOption>();
                   Schema.DescribeFieldResult ptypeResult =TUR__c.Procedure_Type__c.getDescribe();
                   List<Schema.PicklistEntry> ptypeList = ptypeResult.getPicklistValues();
                   for( Schema.PicklistEntry Tpt : ptypeList)
                   {
                      pTypes.add(new SelectOption(Tpt.getLabel(), Tpt.getValue()));
                   }       
                   return pTypes;
               }
             public list<SelectOption> getgender(){
             List<SelectOption> gender= new List<SelectOption>();
             Schema.DescribeFieldResult genderResult =TUR__c.Gender__c.getDescribe();
                   List<Schema.PicklistEntry> genderList = genderResult .getPicklistValues();
                   for( Schema.PicklistEntry gen: genderList )
                   {
                      gender.add(new SelectOption(gen.getLabel(), gen.getValue()));
                   }       
                   return gender;
             }
             public list<SelectOption> getpendingNEw(){
             List<SelectOption> pendingss= new List<SelectOption>();
             Schema.DescribeFieldResult PendResult =TUR__c.Pending__c.getDescribe();
                   List<Schema.PicklistEntry> pendList = PendResult.getPicklistValues();
                   for( Schema.PicklistEntry pen:pendList )
                   {
                      pendingss.add(new SelectOption(pen.getLabel(),pen.getValue()));
                   }       
                   return pendingss;
             }
             public list<SelectOption> getSource(){
             List<SelectOption> Source= new List<SelectOption>();
             Schema.DescribeFieldResult SourceResult =TUR__c.Source__c.getDescribe();
                   List<Schema.PicklistEntry> SourceList = SourceResult.getPicklistValues();
                   for( Schema.PicklistEntry sou: SourceList )
                   {
                      Source.add(new SelectOption(sou.getLabel(), sou.getValue()));
                   }       
                   return Source;
             }
            @RemoteAction 
      Public static string surgenName(string con){
      
               string qstring = con+'%';
               string resultstring='';
               list<contact> srchconlist =new list<contact>();
                srchconlist = [select id,name from contact where name like:qstring];
                    if(!srchconlist.isEmpty()){
                       for(contact co : srchconlist){
                              resultstring+=co.name+'%'+co.id+'#';
                               }
                           }else{
                           resultstring='No Records';
                           }
                       return resultstring;
                     }
//This is used to save the records related to the TUR..
        public pagereference CreateTUR(){
        pagereference parentpage=new pagereference('/'+oldtisdetails.id);//It redirect the page to the Parent Record Details Page based on the Tissue id
          
           system.debug('oldtisdetails@@@'+oldtisdetails);
           if(String.isNotBlank(ApexPages.currentPage().getParameters().get('sfdcIFrameHost')) || String.isNotBlank(ApexPages.currentPage().getParameters().get('sfdcIFrameOrigin'))){
             isMobile = true;
            }else{isMobile = false;}
           TUR__c newtur=new TUR__c();
           newtur.Tissue__c=oldtisdetails.id;
           newtur.Patient_Last_Name__c=Patient_Last_Name;
           system.debug('contactid@@'+contactid);
           newtur.Surgeon_Name__c=contactid;
           newtur.Patient_First_Name__c=Patient_First_Name;
           if(PatientDOB!=''){
           String  dbdt=PatientDOB;
           newtur.PatientDOB__c=date.parse(PatientDOB);
           }
           newtur.Pending__c=Pendings;
           newtur.Procedure_Code_1__c=Procedure_Code_1;
           newtur.Procedure_Code_2__c=Procedure_Code_2;
           if(Procedure_Date!=''){
           newtur.Procedure_Date__c=date.parse(Procedure_Date);
           }
           newtur.Procedure_Type__c=Procedure_Type;
           newtur.Gender__c=Genders;
           newtur.Procedure_Type_Comments__c=Procedure_Type_Comments;
           newtur.Comments__c =Comments ;
           newtur.Source__c=Sources;
           try{
           insert newtur;//Inserting the TUR
            if(!tur.isEmpty()){
               //tur.id=turid;
                
               tur[0].Active__c=false;//In-active the Old TUR
               update tur;//Updating the Old TUR.
                }
                 if(isMobile==false){
                 parentpage.setRedirect(true);
                    return parentpage; //It redirect the page to the Parent Record Details Page
                }else{
                    return null;// It is for Mobile redirection
                 }  
                }catch(DMLException de){
                     ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Error'+de));
                     return null;
                }
                  
  }
}
Please check and give me some suggestions..i do nto know why it is redirecting some times and why it's not redirecting at some times..please..
James LoghryJames Loghry
There are serveral logical branches in your createTUR method, which could explain why your page sometimes redirects, and sometimes does not.  For instance, if "tur" is empty, or isMobile == true, then no redirect happens.  Also, if an exception is encountered, then no redirect happens.

You should take a closer look at your if statements and exceptions, and you'll find why your page is not redirecting.