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
streetstreet 

Update the date filed on pagelevel

I have a date filed(Datefiled12) which is displayed in Visualforce page and a formula filed(Updatedate) of datetype "date", whenever Datefiled12  is changed on visual force page then  Updatedate has to updated  with value equal to  Datefiled12 +4 days.

 

 

EG:

when user select the below value on visualforce page

Datefiled12  = 1/5/2012

 

then Updatedate = 1/9/2012

 

Can it be possible on page level without any button click?

 

Best Answer chosen by Admin (Salesforce Developers) 
raseshtcsraseshtcs
<apex:page standardController="Opportunity" sidebar="false" extensions="datectrl">
    <apex:sectionHeader title="Edit Opportunity" subtitle="{!opportunity.name}"/>
    <apex:form >
        <apex:pageBlock title="Edit Opportunity" id="thePageBlock" mode="edit">
            <apex:pageMessages />
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
                <apex:commandButton value="Cancel" action="{!cancel}"/>                
            </apex:pageBlockButtons>
            <apex:actionRegion >
                <apex:pageBlockSection title="Basic Information" columns="1">
                    <apex:inputField value="{!opportunity.name}"/>
                    <apex:pageBlockSectionItem >
                        <apex:outputLabel value="Stage"/>
                        <apex:outputPanel >
                            <apex:inputField value="{!opportunity.stageName}">
                                <apex:actionSupport event="onchange" rerender="thePageBlock"
                                                    status="status"/>
                            </apex:inputField>
                            <apex:actionStatus startText="applying value..." id="status"/>
                        </apex:outputPanel>
                    </apex:pageBlockSectionItem>
                    <apex:inputField value="{!opportunity.amount}"/>
                    <apex:inputField value="{!psDisp.closedate}">
                                <apex:actionSupport event="onchange" rerender="thefield" status="status" action="{!assignDate}"/>                    
                            </apex:inputField>                    
                    <apex:outputField value="{!psDisp.New_Date__c}" id="thefield"/>  
                </apex:pageBlockSection>
            </apex:actionRegion>
            <apex:pageBlockSection title="Closed Lost Information" columns="1"
                                   rendered="{!opportunity.stageName == 'Closed Lost'}">
                <apex:inputField value="{!opportunity.Name}"  required="true"/>

            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 Class:

public class datectrl {
       public Opportunity psDisp{get;set;}
    public datectrl(ApexPages.StandardController controller) {
         psDisp = new Opportunity();
    }
    public Pagereference assignDate(){
    psDisp.New_Date__c = psDisp.closeDate + 5;
    return null;
    }

}

 While testing u need to create a new field new date on opportunity. Also enter all the field values(Name, stage etc) before you test for the same.

All Answers

raseshtcsraseshtcs

Are u displaying the formula field on the VF page as well. In case you are the change will not show on the page until you save the record.

One thing that can be done is show the users a dummy field on the VF page, rerender the field once the user selects some value on the original date field and then in the database the formula field will calculate the value on its own.

 

let me know in case you need more on this

streetstreet

Yes, im displaying the formula filed on VF page. 

 

you mean, rerender the page once the original date field updated.

 

streetstreet

Sample code on it

raseshtcsraseshtcs

I dont have a sample code for the same. Let me give it a try and i ll post it here!!

raseshtcsraseshtcs
<apex:page standardController="Opportunity" sidebar="false" extensions="datectrl">
    <apex:sectionHeader title="Edit Opportunity" subtitle="{!opportunity.name}"/>
    <apex:form >
        <apex:pageBlock title="Edit Opportunity" id="thePageBlock" mode="edit">
            <apex:pageMessages />
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
                <apex:commandButton value="Cancel" action="{!cancel}"/>                
            </apex:pageBlockButtons>
            <apex:actionRegion >
                <apex:pageBlockSection title="Basic Information" columns="1">
                    <apex:inputField value="{!opportunity.name}"/>
                    <apex:pageBlockSectionItem >
                        <apex:outputLabel value="Stage"/>
                        <apex:outputPanel >
                            <apex:inputField value="{!opportunity.stageName}">
                                <apex:actionSupport event="onchange" rerender="thePageBlock"
                                                    status="status"/>
                            </apex:inputField>
                            <apex:actionStatus startText="applying value..." id="status"/>
                        </apex:outputPanel>
                    </apex:pageBlockSectionItem>
                    <apex:inputField value="{!opportunity.amount}"/>
                    <apex:inputField value="{!psDisp.closedate}">
                                <apex:actionSupport event="onchange" rerender="thefield" status="status" action="{!assignDate}"/>                    
                            </apex:inputField>                    
                    <apex:outputField value="{!psDisp.New_Date__c}" id="thefield"/>  
                </apex:pageBlockSection>
            </apex:actionRegion>
            <apex:pageBlockSection title="Closed Lost Information" columns="1"
                                   rendered="{!opportunity.stageName == 'Closed Lost'}">
                <apex:inputField value="{!opportunity.Name}"  required="true"/>

            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 Class:

public class datectrl {
       public Opportunity psDisp{get;set;}
    public datectrl(ApexPages.StandardController controller) {
         psDisp = new Opportunity();
    }
    public Pagereference assignDate(){
    psDisp.New_Date__c = psDisp.closeDate + 5;
    return null;
    }

}

 While testing u need to create a new field new date on opportunity. Also enter all the field values(Name, stage etc) before you test for the same.

This was selected as the best answer
streetstreet

Ok, ill try. will get back to you..

 

Thanks.

streetstreet

Its working, Thanks Alot n for ur effort. 

raseshtcsraseshtcs

Your Welcome!!!