0

The focus stays on the first field on a page load,which is the one highlighted in the image "Who prompted you.."

But I would like to change the default focus ,whenever there is an error(system or a validation)

For example:
When there is an error on page save,like as below, the focus should shift to the error field,like in this case Business name.

The field I am referring is "existingbusiness" which is a lookup field. When user types in a text and presses enter,the lookup field errors out as it finds a multiple match.This is achieved by the actionfunction which calls a donotSubmit function. The objective is to change the focus to this error field and shift the the default focus from the first field on the form.

enter image description here

VF page:

<apex:page extensions="PNTReferralInfoController" standardController="Referral__c" tabStyle="Account" title="PNT Referral Information" id="thePage" >
<script type='text/javascript'>

function setFocusOnLoad()
{
    document.getElementById('{!$Component.theForm.block.sec.reason}').focus();

}}
window.onload=setFocusOnLoad;
</script>
<script type='text/javascript'>
function noenter(ev)  {
if (window.event && window.event.keyCode == 13 || ev.which == 13) {
    donotSubmit();
    return false;
} else {
    return true;
}
}
</script>

<apex:form id="theForm">
<apex:pageBlock title="Referral Information" id="block" mode="edit">
    <apex:pageMessages id="errorMessages"/>
    <apex:pageBlockSection columns="1" collapsible="false" id="sec">



        <apex:pageblock id="ExistingBusinessAccountInformation" mode="mainDetail" >
            <apex:pageBlockSection columns="1" collapsible="false" rendered="{!showexistingbusinessdetails}">

                <h1>Option 1: Select EXISTING Business</h1>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Business Name" for="existingBusiness" />
                    <apex:actionRegion >
                        <apex:inputField value="{!referral.Referrer_Organisation__c}" required="false" id="existingBusiness" onkeypress="return noenter(event)" >
                            <apex:actionSupport event="onchange"  action="{!existingBusiness}" reRender="Newbusiness,message2"  />
                        </apex:inputField> 
                    </apex:actionRegion>
                </apex:pageblocksectionitem>
            </apex:pageBlockSection>
        </apex:pageBlock>


<apex:pageBlockButtons location="bottom" > 
<apex:actionFunction name="donotSubmit" action="{!donotSubmit}" immediate="true"/>
<apex:commandButton id="previous" value="Previous" action="{!back}" immediate="true" />
<apex:commandButton id="savepage" value="Save" action="{!saveReferralInformation}" />
<apex:commandButton id="resetBtn" value="Cancel/Start Again" immediate="true" action="{!reset}"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>

Apex controller:

public class PNTReferralInfoController {
    public ApexPages.StandardController standardController {get;set;} 
    public Referral__c referral {get;set;}
    public String personId {get;set;}
    public String contactId {get;set;}
    public Account person {get;set;}   
    public Account client {get;set;}

    public PNTReferralInfoController( ApexPages.StandardController controller){ 
        standardController = controller;
        referral = (Referral__c)standardController.getRecord();
        personId = ApexPages.currentPage().getParameters().get('personId');
        person = this.getPerson(personId);        
        if(person != null){referral.Client__c = person.Id;}
        client = [SELECT id,Lead_Source__pc,Eye_Condition__pc,Reason_for_contact__pc,Other_Vision_Condition_Information__pc  from Account where id=:personId];

        referrer = new Account();
        contactRecordtype = [SELECT Id FROM RecordType WHERE SobjectType = 'Account' AND DeveloperName = 'Contact' limit 1];
        referrer.RecordTypeId = contactRecordtype.Id;   
        tempContact = new Contact();
        newBusiness = new Account();
        newBusinessAccount = new Account();
        newRelationship = new Relationship__c();
        linkBusinessAccounttoReferrer = new AccountContactRelation();
        removelinkedbusinessdetails = true;
        showexistingbusinessdetails = false;
        shownewbusinessdetails = false;
        bid = false;
        matchText = NULL;
        Search_results = 1;
        Search_results_ba_multi =-1;
        Search_results_ba_blank =-1;

    }


    public PageReference saveReferralInformation(){
        try{
            person.Reason_for_contact__pc = client.Reason_for_contact__pc;
            person.Last_PNT_Run_Date__pc = Datetime.now();

            upsert person;
        }
        catch(DmlException ex){ApexPages.addMessages(ex);}

        PNTClientInfoController.savePNTFormDataSnapShot(person.id,referral.Id);
        return new PageReference('/'+ person.Id);
    }

    public PageReference reset(){
        person = null;
        PageReference pageRef = Page.PNT_Client;
        pageRef.setRedirect(true);
        return pageRef;
    }

    public PageReference back(){
        PageReference pageref = new PageReference('/apex/pnt_client?id='+ personId);
        pageref.setRedirect(true);
        return pageref;

    } 

    public PageReference donotSubmit()
    {

        showexistingbusinessdetails = true;
        return null;
    }

}

1 Answer 1

0

use oncolplete() on actionFunction to reset focus by calling the javascript method

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .