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
Ravindar AdminRavindar Admin 

How to fill a field value from another field in same object before the record is saving in visualforce page?

I  created a Visualforce page with custom object fields.
I have date field(date_of_birth__c) & Number field(Age__c).
I am looking to fill the Age__c value before the record is saving into the database. 
I can take formula field to calculate the value. But If the date_of_birth__c does not enter, so able to enter age__c manually so I took the age__c field as Number.
Best Answer chosen by Ravindar Admin
Shruti SShruti S
User-added image

Just as Prady told, its risky to do date calculation in the client side. Hence did not do the age calculation in Javascript instead did it in the Apex to avoid locale issue. I called the Apex method which calculates the age on the onchange of the Date of Birth field and via Remote Actions. The method returns the Age and then I populate it in the Age textbox using jQuery.

Apex Controller
public class EmployeeExtension {
    public EmployeeExtension( ApexPages.StandardController controller ) {
        
    }
    
    @RemoteAction
    public static Integer calculateAge( String dateOfBirth ) {
        Date dob = Date.parse( dateOfBirth );
        Integer age;

        if( Date.today().month() > dob.month() ) {
            age = Date.today().year() - dob.year();
        }
        else if( 
            Date.today().month() == dob.month() && 
            Date.today().day() >= dob.day() 
        ) {
            age = Date.today().year() - dob.year();
        }
        else {
            age = Date.today().year() - dob.year() - 1 ;
        }
        
        return age;
    }
}

Visualforce
<apex:page id="page" standardController="Employee__c" extensions="EmployeeExtension">
    <apex:form id="form">
        <apex:pageBlock id="pgblk" mode="edit" title="Employee Edit">
            <apex:pageBlockSection id="pgblksec" title="Information" columns="2">
                <apex:inputField id="inpFldDateOfBirth" value="{!Employee__c.Date_of_Birth__c}" onchange="calculateAge()"></apex:inputField>
                <apex:inputField id="inpFldAge" value="{!Employee__c.Age__c}"></apex:inputField>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
    
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    <script type="text/javascript">
        var calculateAge = function() {
            var dobVal   = $( "[id*='inpFldDateOfBirth']" ).val();
            
            EmployeeExtension.calculateAge(
                dobVal,
                function( result, event ) {
                    $( "[id*='inpFldAge']" ).val( result );
                }
            );
        };
    </script>
</apex:page>

 

All Answers

Prady01Prady01
Hi there, If you have to calculate the field age on the fly then you will have to use some DOM manupulation techniques. This can done using JS or jquery.

But be vary the date is tricky field, The date field on the object will chnage its format in the VF page based on the user locale. You will have to take this also into consideration.

Thank you
Pradeep
Shruti SShruti S
User-added image

Just as Prady told, its risky to do date calculation in the client side. Hence did not do the age calculation in Javascript instead did it in the Apex to avoid locale issue. I called the Apex method which calculates the age on the onchange of the Date of Birth field and via Remote Actions. The method returns the Age and then I populate it in the Age textbox using jQuery.

Apex Controller
public class EmployeeExtension {
    public EmployeeExtension( ApexPages.StandardController controller ) {
        
    }
    
    @RemoteAction
    public static Integer calculateAge( String dateOfBirth ) {
        Date dob = Date.parse( dateOfBirth );
        Integer age;

        if( Date.today().month() > dob.month() ) {
            age = Date.today().year() - dob.year();
        }
        else if( 
            Date.today().month() == dob.month() && 
            Date.today().day() >= dob.day() 
        ) {
            age = Date.today().year() - dob.year();
        }
        else {
            age = Date.today().year() - dob.year() - 1 ;
        }
        
        return age;
    }
}

Visualforce
<apex:page id="page" standardController="Employee__c" extensions="EmployeeExtension">
    <apex:form id="form">
        <apex:pageBlock id="pgblk" mode="edit" title="Employee Edit">
            <apex:pageBlockSection id="pgblksec" title="Information" columns="2">
                <apex:inputField id="inpFldDateOfBirth" value="{!Employee__c.Date_of_Birth__c}" onchange="calculateAge()"></apex:inputField>
                <apex:inputField id="inpFldAge" value="{!Employee__c.Age__c}"></apex:inputField>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
    
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    <script type="text/javascript">
        var calculateAge = function() {
            var dobVal   = $( "[id*='inpFldDateOfBirth']" ).val();
            
            EmployeeExtension.calculateAge(
                dobVal,
                function( result, event ) {
                    $( "[id*='inpFldAge']" ).val( result );
                }
            );
        };
    </script>
</apex:page>

 
This was selected as the best answer