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 

updating the formula filed(of datatype date) on visualforce page

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) 
LakshmanLakshman

Here is an example to do this using javascript:

Sample Apex class:

 

public class InstantDateChanger{
public CustomObject__c m{get;set;}
public InstantDateChanger()
{
    m= new CustomObject__c();
}

}

 Visulaforce Page:

 

<apex:page controller="InstantDateChanger">
<script>
function changeDateFormula()
{
    var actualValue = document.getElementById("{!$Component.myFRM.pblock.pSection.valueToBeChanged}").value;
    var d = new Date(actualValue);
    var newDate = new Date();
    newDate.setDate(d.getDate()+4);
    var new_date = newDate.getDate();
    var new_month = newDate.getMonth()+1;
    var new_year = newDate.getFullYear();
    var newValue = new_month+'/'+new_date+'/'+new_year;
    document.getElementById("{!$Component.myFRM.pblock.pSection.changeMe}").innerHTML = newValue;
}
</script>
<apex:form id="myFRM">
    <apex:pageBlock id="pblock">
    <apex:pageBlockSection id="pSection" columns="1">
      <apex:inputField  value="{!m.Datefiled12__c}" id="valueToBeChanged" onchange="changeDateFormula()"/>

      <apex:outputText value="{!m.Updatedate__c}"  id="changeMe"></apex:outputText>

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

</apex:page>

 In my custom obejct named CustomObject__c, I have taken Datefiled12 as data type date and Updatedate as data type formula returning date and as per your scenario it works well for me.

 

Let me know if it works for you.

 

Regards,

Lakshman

 

 

All Answers

Andy BoettcherAndy Boettcher

If that field is a formula - you can't edit it directly.  The user will have to save the page with the change on it - and then when the page refreshes the new Formula value will be displayed.

 

If you want to provide instant feedback to your user - you can do so but not without using a little Javascript.  If you use the "onchange" property of your data-entry field (Datefiled12) and have another field on the page that you update from there - you could go that way.

 

That, or you could have an outputText field on your page that you update via a rerender - and recalculate the date + 4 value in a controller GETer.

 

-Andy

LakshmanLakshman

Here is an example to do this using javascript&colon;

Sample Apex class:

 

public class InstantDateChanger{
public CustomObject__c m{get;set;}
public InstantDateChanger()
{
    m= new CustomObject__c();
}

}

 Visulaforce Page:

 

<apex:page controller="InstantDateChanger">
<script>
function changeDateFormula()
{
    var actualValue = document.getElementById("{!$Component.myFRM.pblock.pSection.valueToBeChanged}").value;
    var d = new Date(actualValue);
    var newDate = new Date();
    newDate.setDate(d.getDate()+4);
    var new_date = newDate.getDate();
    var new_month = newDate.getMonth()+1;
    var new_year = newDate.getFullYear();
    var newValue = new_month+'/'+new_date+'/'+new_year;
    document.getElementById("{!$Component.myFRM.pblock.pSection.changeMe}").innerHTML = newValue;
}
</script>
<apex:form id="myFRM">
    <apex:pageBlock id="pblock">
    <apex:pageBlockSection id="pSection" columns="1">
      <apex:inputField  value="{!m.Datefiled12__c}" id="valueToBeChanged" onchange="changeDateFormula()"/>

      <apex:outputText value="{!m.Updatedate__c}"  id="changeMe"></apex:outputText>

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

</apex:page>

 In my custom obejct named CustomObject__c, I have taken Datefiled12 as data type date and Updatedate as data type formula returning date and as per your scenario it works well for me.

 

Let me know if it works for you.

 

Regards,

Lakshman

 

 

This was selected as the best answer
streetstreet

Its working perfectly for my requirement thanks alot lakshman.