Jun 28, 2024
53 Views
Comments Off on Custom “Range of Years” for “Standard Date Field” in Salesforce
0 0

Custom “Range of Years” for “Standard Date Field” in Salesforce

Written by

Have you ever noticed that the standard date field shows year Range to only up to 7 years? What if we want that Range to be custom as per our functionality.

For that you need to use the following script:

<script type="text/javascript">
    $(document).ready (
        function() {
            var startYear = 2000;
            var endYear = 2050;
            var optionsString = '';
            if(startYear<endYear) {
                for(i=startYear;i<endYear+1;i++) {
                    optionsString += "<option value=""+i+"">"+i+"</option>";
                }
                $('#calYearPicker').html(optionsString);
            }
            $('#sidebarDiv #hideMyParent').parent().parent().hide();
        }
    );
</script>

**** In var startYear=2000; var endYear=2050; mention your year Range.

Visualforce Page:

<apex:page standardController="Contract" extensions="ContractClass" showHeader="true" sidebar="true">
    <apex:sectionHeader title="Contract Edit" subtitle="New Contract"/>
    <apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"> 
    </apex:includeScript> 
    <span id="hideMyParent"></span>
    <script type="text/javascript">
        $(document).ready(function() {
            var startYear = 2000;
            var endYear = 2050; 
            var optionsString = '';
            if(startYear<endYear) {
                for(i=startYear;i<endYear+1;i++) {
                    optionsString += "<option value=""+i+"">"+i+"</option>";
                }
                $('#calYearPicker').html(optionsString);
            }
            $('#sidebarDiv #hideMyParent').parent().parent().hide();
        });
    </script> 
    <apex:form> 
        <apex:pageBlock title="Contract Edit">
            <apex:pageBlockSection title="Contract Information">
                <apex:outputField value="{!Contract.Ownerid}" html-disabled="true"/>
                <apex:inputField value="{!Contract.StartDate}"/>
            </apex:pageBlockSection>
            <apex:pageBlockButtons>
                <apex:commandButton action="{!save}" value="Save"/>
                <apex:commandButton action="{!saveAndNew}" value="Save &amp; New" />
                <apex:commandButton action="{!cancel}" value="Cancel"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Cheers!!!

Article Categories:
Blog
Priya Singh
https://thestarbiznews.com/

Expertise: Technology Trends, Startups, Digital Disruption Bio: Priya Singh is a tech-savvy millennial with a finger on the pulse of the ever-evolving digital landscape. A graduate of Stanford's Computer Science program and a former engineer at a Silicon Valley giant, Priya has a deep understanding of the technologies shaping our future. Her passion lies in demystifying complex tech jargon and exploring the disruptive potential of emerging trends, making her articles essential reading for anyone who wants to stay ahead of the curve. When not scouring the web for the latest tech tidbits, Priya enjoys traveling to off-the-beaten-path destinations and immersing herself in diverse cultures, always seeking new inspiration and insights.