You need to sign in to do that
Don't have an account?
Baguiar2
Enter values using Javascript in VF page
Guys,
Trying to have a field in a simple VF page that is hidden and once the VF page is loaded, it grabs the current date and time nad once SUBMITTED a field will be saved with that data. The field is the Tour_given_date__c.
I ahve the Jscript working, but it does not set the value to the field.
<apex:page standardController="Lead" id="p1" showHeader="false" sidebar="false" > <apex:pageBlock title="" id="pb1"> <apex:form id="f1" > <table width="60%" columns="2"> <td align="left"><p><b>First name: </b><br/><apex:inputField value="{!Lead.FirstName}" /></p></td> <td align="left"><p><b>Last name:</b><apex:inputField value="{!Lead.LastName}" /></p></td><tr/> <td align="left"><p><b>Email:</b> <br/><apex:inputField value="{!Lead.email}" /></p></td> <td align="left"><p><b>Phone:</b> <br/><apex:inputField value="{!Lead.phone}" /></p></td><tr/> <td align="left"><p><b>Zip Code:</b> <br/><apex:inputField value="{!Lead.Zip_Code__c}" /></p></td> <td align="left"><div id="a" style="display:none;"><p><b>Tourgivendate</b> <br/><apex:inputField value="{!Lead.Tour_given_time__c}" id="dt"/></p></div></td> </table> <p><b>Who is giving you the tour? (Please ask the Front desk manager)</b><br/> <apex:inputField value="{!Lead.Tour_Given_by__c}" /></p><br/><br/> <apex:commandButton value="Save" action="{!save}" onclick="AssignnDatevalu();" /> </apex:form> <script> var currentTime = new Date() var month = currentTime.getMonth() + 1 var dayOK = currentTime.getDate() var year = currentTime.getFullYear() var hours = currentTime.getHours() var minutes = currentTime.getMinutes() var dianoite = "AM" if (hours >= 12){ dianoite = "PM" } if (minutes < 10){ minutes = "0" + minutes } if (hours > 12){ hours = (hours - 12) } function AssignnDatevalu() { document.getElementById('dt').value=month + "/" + dayOK + "/" + year + " " + hours + ":" + minutes + " " + dianoite; return true; } </script> </apex:pageBlock> </apex:page>
Thanks for the hELP!
Hi,
You have to pass the all component ids in JavaScript
Try the below code snippet as reference:
<apex:page standardController="Lead" id="p1" showHeader="false" sidebar="false" >
<apex:pageBlock title="" id="pb1">
<apex:form id="f1" >
<table width="60%" columns="2">
<td align="left"><p><b>First name: </b><br/><apex:inputField value="{!Lead.FirstName}" /></p></td>
<td align="left"><p><b>Last name:</b><apex:inputField value="{!Lead.LastName}" /></p></td><tr/>
<td align="left"><p><b>Email:</b> <br/><apex:inputField value="{!Lead.email}" /></p></td>
<td align="left"><p><b>Phone:</b> <br/><apex:inputField value="{!Lead.phone}" /></p></td><tr/>
<td align="left"><p><b>Zip Code:</b> <br/><apex:inputField value="{!Lead.Zip_Code__c}" /></p></td>
<td align="left"><div id="a" style="display:none;"><p><b>Tourgivendate</b> <br/><apex:inputField value="{!Lead.Tour_given_time__c}" id="dt"/></p></div></td>
</table>
<p><b>Who is giving you the tour? (Please ask the Front desk manager)</b><br/> <apex:inputField value="{!Lead.Tour_Given_by__c}" /></p><br/><br/>
<apex:commandButton value="Save" action="{!save}" onclick="AssignnDatevalu();" />
</apex:form>
<script>
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var dayOK = currentTime.getDate()
var year = currentTime.getFullYear()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
var dianoite = "AM"
if (hours >= 12){
dianoite = "PM"
}
if (minutes < 10){
minutes = "0" + minutes
}
if (hours > 12){
hours = (hours - 12)
}
function AssignnDatevalu()
{
document.getElementById('p1:pb1:f1:dt').value=month + "/" + dayOK + "/" + year + " " + hours + ":" + minutes + " " + dianoite;return true;
}
</script>
</apex:pageBlock>
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
All Answers
Why not just update the value in the save method?
OR
call the same function using
depending on your needs
Starz!
Thanks again, but isn't that what is already being called on the "onclick" ? The Function is already there . SI the issue being the function being called after the Save method ?
B
sorry, I thought the onclick was something that was working...
what is the issue?
the field not getting updated before save and thus the record not displaying the correct data....
are you trying to save the date time when clicked in the record that is being saved?
if so, you may have to write an extension to handled the save event and write the date there. Not sure on the order of execution with the controller happening before or after the onclick but I would say it appears it is not at the same time.
Pretty much the "onclick" is not working to set the value on the field before being saved. I wnat the Tour_given_date__C field to have the current date & time populated before the user hits the submit button.
So there is no way to do this without having to wirte the extension to the SAVE event from the standard controller ? Is that it or I didn;t get it at all ? :)
B
ensure the java is actually working by throwing an alert('HI"); into the script (just in case)
you may be able to do something like this....
Hi,
You have to pass the all component ids in JavaScript
Try the below code snippet as reference:
<apex:page standardController="Lead" id="p1" showHeader="false" sidebar="false" >
<apex:pageBlock title="" id="pb1">
<apex:form id="f1" >
<table width="60%" columns="2">
<td align="left"><p><b>First name: </b><br/><apex:inputField value="{!Lead.FirstName}" /></p></td>
<td align="left"><p><b>Last name:</b><apex:inputField value="{!Lead.LastName}" /></p></td><tr/>
<td align="left"><p><b>Email:</b> <br/><apex:inputField value="{!Lead.email}" /></p></td>
<td align="left"><p><b>Phone:</b> <br/><apex:inputField value="{!Lead.phone}" /></p></td><tr/>
<td align="left"><p><b>Zip Code:</b> <br/><apex:inputField value="{!Lead.Zip_Code__c}" /></p></td>
<td align="left"><div id="a" style="display:none;"><p><b>Tourgivendate</b> <br/><apex:inputField value="{!Lead.Tour_given_time__c}" id="dt"/></p></div></td>
</table>
<p><b>Who is giving you the tour? (Please ask the Front desk manager)</b><br/> <apex:inputField value="{!Lead.Tour_Given_by__c}" /></p><br/><br/>
<apex:commandButton value="Save" action="{!save}" onclick="AssignnDatevalu();" />
</apex:form>
<script>
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var dayOK = currentTime.getDate()
var year = currentTime.getFullYear()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
var dianoite = "AM"
if (hours >= 12){
dianoite = "PM"
}
if (minutes < 10){
minutes = "0" + minutes
}
if (hours > 12){
hours = (hours - 12)
}
function AssignnDatevalu()
{
document.getElementById('p1:pb1:f1:dt').value=month + "/" + dayOK + "/" + year + " " + hours + ":" + minutes + " " + dianoite;return true;
}
</script>
</apex:pageBlock>
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
Thanks Navatar. That did it! ANd the hint to pass all the components Ids to the Jscript helped me a lot with other stuff.
Now, I tried to also navigate to LOGOUT right after the record is saved and it did not work. Here is what I added:
Also tried this way on the Javascript:
no success. It saves the record, enters the Current date and time, but it does not logout.
THANKS!!!