function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Michael Hedrick 2Michael Hedrick 2 

Visual Force Save Button Inactive After Error

Hello All,
The 'Save' button on my VF page is inactive if a user does not complete all of the required field.  
Also, can I chnage the location of the error message?  The Save button is at the bottom the screen(mobile) and the user will not see the error at the top of the page.
Thanks,
M

VF Page
<apex:page docType="html-5.0" standardController="Account" extensions="SourcingAuditAccountController"  standardStylesheets="true" lightningStylesheets="true" showheader="false">
  
<div id="startLat">
</div>

<div id="startLon">
</div>

 <script>
    window.onload = function() {
        var startPos;
        var geoSuccess = function(position) {
            startPos = position;
            document.getElementById('{!$Component.inpform.pb.pbs.startLat}').value=startPos.coords.latitude;
            document.getElementById('{!$Component.inpform.pb.pbs.startLon}').value=startPos.coords.longitude;
            //alert('@@@ ' + document.getElementsByClassName('findclass')[0].value);
           document.getElementsByClassName('Latclass')[0].value =startPos.coords.latitude;
           document.getElementsByClassName('Lonclass')[0].value =startPos.coords.longitude;

        };
        navigator.geolocation.getCurrentPosition(geoSuccess);
    };
  </script>
  
    <script type='text/javascript' src='/canvas/sdk/js/publisher.js'/>  
    <script>
        Sfdc.canvas.publisher.subscribe({name: "publisher.showPanel", onData:function(e) { 
            Sfdc.canvas.publisher.publish({name:"publisher.setValidForSubmit", payload:"true"}); 
        }});

        Sfdc.canvas.publisher.subscribe({ name: "publisher.post", onData: function(e) { 
            Sfdc.canvas.publisher.publish({name:"publisher.setValidForSubmit", payload:"false"}); 
            saveActivity();
        }});
    </script>
    
    <div>
        <apex:form id="inpform" >
            <apex:outputPanel id="out">           
                
             <apex:actionFunction action="{!Save}"  name="saveActivity" rerender="out"/> 

             <div style="display: none;">
                <apex:inputField value="{!SourcingAudit.Account__c}" />
                 ['Name'='Sourcing Audit for {!Account.Name} on system.today().format())' ], true)}">
             </div>
              
              <apex:outputPanel >  
                    <label style="display:block;margin-top:12px;margin-left:10px" for="account_name">Account</label>
                    <span style="margin-left:10px;" id="account_name">{!Account.Name}</span>
                    
                     <label style="display:block;margin-top:12px;margin-left:10px;" for="Contact__c">Select a Contact</label>
                    <apex:inputField style="margin-left:10px;" id="Contact__c" value="{!SourcingAudit.Contact__c}" required="true"/>
                </apex:outputPanel> 
               
                <br></br>
                     
                <apex:outputLabel style="margin-left:10px;"  value="Bale Quality Issue"/>            
                <apex:selectRadio required="true" value="{!SourcingAudit.Quality_Bale_Issue__c}" >
                <apex:selectOption itemValue="Yes" itemlabel="Yes"/>
                <apex:selectOption itemValue="No" itemlabel="No"/>
                </apex:selectRadio>  
                
                <br></br>
                
                <apex:outputLabel style="margin-left:10px;" value="Bale Inspected"/> 
                <apex:selectRadio required="true"  value="{!SourcingAudit.Bales_Inspected__c}" >
                <apex:selectOption itemValue="Yes" itemlabel="Yes"/>
                <apex:selectOption itemValue="No" itemlabel="No"/>
                </apex:selectRadio>
                 <br></br>
            
                 <apex:outputLabel style="margin-left:10px;" value="Volume Review"/>
                <apex:selectRadio required="true"  value="{!SourcingAudit.Volume_Reviewed__c}" >
                <apex:selectOption itemValue="Yes" itemlabel="Yes"/>
                <apex:selectOption itemValue="No" itemlabel="No"/>
                </apex:selectRadio>      
                 <br></br>
                
                 <apex:outputLabel style="margin-left:10px;" value="Weight Issue"/> 
                <apex:selectRadio required="true"   value="{!SourcingAudit.Weight_Issues__c}" >
                <apex:selectOption itemValue="Yes" itemlabel="Yes"/>
                <apex:selectOption itemValue="No" itemlabel="No"/>
                </apex:selectRadio>
                <br></br>
                
                <apex:outputLabel style="margin-left:10px;" value="Admin Review"/> 
                <apex:selectRadio required="true"  value="{!SourcingAudit.Admin_review__c}" >
                <apex:selectOption itemValue="Yes" itemlabel="Yes"/>
                <apex:selectOption itemValue="No" itemlabel="No"/>
                </apex:selectRadio>      
                
                 <br></br>
                
                 <apex:outputLabel style="margin-left:10px;" value="Referral"/> 
                <apex:selectRadio required="true"  value="{!SourcingAudit.Referral__c}" >
                <apex:selectOption itemValue="Yes" itemlabel="Yes"/>
                <apex:selectOption itemValue="No" itemlabel="No"/>
                </apex:selectRadio>
    
                <apex:outputPanel >
                    <label style="display:block;margin-top:12px;margin-left:10px;" for="comments">Comment</label>
                    <apex:inputTextarea style="margin-left:10px;" id="Audit_Comment__c" rows="3" cols="30" value="{!SourcingAudit.Audit_Comment__c}" />
                    
              </apex:outputPanel>
  
            </apex:outputPanel> 
        </apex:form>
    </div>
</apex:page>
 
public class SourcingAuditAccountController{
    private Sourcing_Audit__c  SourcingAudit;
    private Account Account;
    public String defaultCheck{get;set;}
    public string calljavascript {get; set;}
    public string message {get; set;} 

    public SourcingAuditAccountController(ApexPages.StandardController controller) 
    {
        Account = [SELECT Id, Name FROM Account WHERE Id =: controller.getId()];
        SourcingAudit = new Sourcing_Audit__c();
        SourcingAudit.Account__c = Account.Id;
     
    if(defaultCheck==NULL)
    {defaultCheck='Winchester';}
    
    }    

    public string PostSaveMessage {get; set;}
    public string Close {get; set;}

   
    public Sourcing_Audit__c  getSourcingAudit() {
        return SourcingAudit ;
    }    

    public Account getAccount() {
        return Account;
    }
   
    public PageReference Save()
    
    {
             
     RecordType rt;
        if(SourcingAudit.Audit_Location__c!= null)
            rt = [Select Id From RecordType Where DeveloperName = 'Warehouse' And sObjectType = 'Sourcing_Audit__c'];
        else
            rt = [Select Id From RecordType Where DeveloperName = 'Facility' And sObjectType = 'Sourcing_Audit__c'];
        SourcingAudit.RecordTypeId = rt.Id;
        
        System.debug('In Save method.');
        
        try {
        
        Insert SourcingAudit;
        PageReference pg = new PageReference('/'+SourcingAudit.Id);
        pg.setRedirect(true);  
        return pg;  
                    
        } 
        catch (Exception ex) {
            string message = ex.getMessage();
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Fatal, message));
            return null;
        }
    }
   
}

 
Best Answer chosen by Michael Hedrick 2
Kumaresan.ManickamKumaresan.Manickam
Can you try keeping <apex:messages /> component on top of the page(current position) and bottom of the page as well so that when they get an error it gets display in top and bottom side of the page so that they are aware without navigating to top of the page. Will that work.

All Answers

Kumaresan.ManickamKumaresan.Manickam
I do not see neither the button HTML nor the error message block in the page code you posted.Where exactly those located?
Michael Hedrick 2Michael Hedrick 2
I was cleaning up the code before posting and obviously cleaned up too much.  This error message states which field(s) require attention.
<apex:page docType="html-5.0" standardController="Account" extensions="SourcingAuditAccountController"  standardStylesheets="true" lightningStylesheets="true" showheader="false">
  
<div id="startLat">
</div>

<div id="startLon">
</div>

 <script>
    window.onload = function() {
        var startPos;
        var geoSuccess = function(position) {
            startPos = position;
            document.getElementById('{!$Component.inpform.pb.pbs.startLat}').value=startPos.coords.latitude;
            document.getElementById('{!$Component.inpform.pb.pbs.startLon}').value=startPos.coords.longitude;
            //alert('@@@ ' + document.getElementsByClassName('findclass')[0].value);
           document.getElementsByClassName('Latclass')[0].value =startPos.coords.latitude;
           document.getElementsByClassName('Lonclass')[0].value =startPos.coords.longitude;

        };
        navigator.geolocation.getCurrentPosition(geoSuccess);
    };
  </script>
  
    <script type='text/javascript' src='/canvas/sdk/js/publisher.js'/>  
    <script>
        Sfdc.canvas.publisher.subscribe({name: "publisher.showPanel", onData:function(e) { 
            Sfdc.canvas.publisher.publish({name:"publisher.setValidForSubmit", payload:"true"}); 
        }});

        Sfdc.canvas.publisher.subscribe({ name: "publisher.post", onData: function(e) { 
            Sfdc.canvas.publisher.publish({name:"publisher.setValidForSubmit", payload:"false"}); 
            saveActivity();
        }});
    </script>
    
    <div>
        <apex:form id="inpform" >
            <apex:outputPanel id="out">
                <apex:messages /> 
               
                      
                <apex:actionFunction action="{!Save}"  name="saveActivity" rerender="out" oncomplete="refreshFeed();"/> 

             <div style="display: none;">
                <apex:inputField value="{!SourcingAudit.Account__c}" />
                 ['Name'='Sourcing Audit for {!Account.Name} on system.today().format())' ], true)}">
             </div>
             
              
              <apex:outputPanel >  
                    <label style="display:block;margin-top:12px;margin-left:10px" for="account_name">Account</label>
                    <span style="margin-left:10px;" id="account_name">{!Account.Name}</span>
                    
                     <label style="display:block;margin-top:12px;margin-left:10px;" for="Contact__c">Select a Contact</label>
                    <apex:inputField style="margin-left:10px;" id="Contact__c" value="{!SourcingAudit.Contact__c}" required="true"/>
                </apex:outputPanel> 
               
                <br></br>
                     
                <apex:outputLabel style="margin-left:10px;"  value="Bale Quality Issue"/>            
                <apex:selectRadio required="true" value="{!SourcingAudit.Quality_Bale_Issue__c}" >
                <apex:selectOption itemValue="Yes" itemlabel="Yes"/>
                <apex:selectOption itemValue="No" itemlabel="No"/>
                </apex:selectRadio>  
                
                <br></br>
                
                <apex:outputLabel style="margin-left:10px;" value="Bale Inspected"/> 
                <apex:selectRadio required="true"  value="{!SourcingAudit.Bales_Inspected__c}" >
                <apex:selectOption itemValue="Yes" itemlabel="Yes"/>
                <apex:selectOption itemValue="No" itemlabel="No"/>
                </apex:selectRadio>
                 <br></br>
            
                 <apex:outputLabel style="margin-left:10px;" value="Volume Review"/>
                <apex:selectRadio required="true"  value="{!SourcingAudit.Volume_Reviewed__c}" >
                <apex:selectOption itemValue="Yes" itemlabel="Yes"/>
                <apex:selectOption itemValue="No" itemlabel="No"/>
                </apex:selectRadio>      
                 <br></br>
                
                 <apex:outputLabel style="margin-left:10px;" value="Weight Issue"/> 
                <apex:selectRadio required="true"   value="{!SourcingAudit.Weight_Issues__c}" >
                <apex:selectOption itemValue="Yes" itemlabel="Yes"/>
                <apex:selectOption itemValue="No" itemlabel="No"/>
                </apex:selectRadio>
                <br></br>
                
                <apex:outputLabel style="margin-left:10px;" value="Admin Review"/> 
                <apex:selectRadio required="true"  value="{!SourcingAudit.Admin_review__c}" >
                <apex:selectOption itemValue="Yes" itemlabel="Yes"/>
                <apex:selectOption itemValue="No" itemlabel="No"/>
                </apex:selectRadio>      
                
                 <br></br>
                
                 <apex:outputLabel style="margin-left:10px;" value="Referral"/> 
                <apex:selectRadio required="true"  value="{!SourcingAudit.Referral__c}" >
                <apex:selectOption itemValue="Yes" itemlabel="Yes"/>
                <apex:selectOption itemValue="No" itemlabel="No"/>
                </apex:selectRadio>
    
                <apex:outputPanel >
                    <label style="display:block;margin-top:12px;margin-left:10px;" for="comments">Comment</label>
                    <apex:inputTextarea style="margin-left:10px;" id="Audit_Comment__c" rows="3" cols="30" value="{!SourcingAudit.Audit_Comment__c}" />
                    
              </apex:outputPanel>
  
            </apex:outputPanel> 
        </apex:form>
    </div>
</apex:page>

 
Kumaresan.ManickamKumaresan.Manickam
Can you try keeping <apex:messages /> component on top of the page(current position) and bottom of the page as well so that when they get an error it gets display in top and bottom side of the page so that they are aware without navigating to top of the page. Will that work.
This was selected as the best answer
Michael Hedrick 2Michael Hedrick 2
Thanks for the suggestion Kumaresan.  I think I can do that but on a mobile phone that may appear pretty cluttered.  I thought I could get the page to scrtoll to the top using JS but it has not been working.
Michael Hedrick 2Michael Hedrick 2
I ended up adding page blocks and sections which helped resolve the issue.
Thanks,
M