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
Baguiar2Baguiar2 

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!

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

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

Starz26Starz26

Why not just update the value in the save method?

 

OR

 

call the same function using

onclick="AssignnDatevalu();"

 

depending on your needs

 

Baguiar2Baguiar2

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

Starz26Starz26

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.

Baguiar2Baguiar2

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

Starz26Starz26

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....

 

 

<apex:actionFunction action="{!save}" name="doSave" rerender="nothing" id="fcnSave"/>


<apex:commandButton value="Save" onclick="AssignnDatevalu(); doSave();" />



</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>

 

Navatar_DbSupNavatar_DbSup

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. 

 

This was selected as the best answer
Baguiar2Baguiar2

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:

<apex:commandButton value="Save" action="{!save}" onclick="AssignnDatevalu();navigateToUrl('/secur/logout.jsp');" />

 

Also tried this way on the Javascript&colon;

 

function AssignnDatevalu()
{
document.getElementById('p1:pb1:f1:dt').value=month + "/" + dayOK + "/" + year + " " + hours + ":" + minutes + " " + dianoite;return true;navigateToUrl('/secur/logout.jsp');
 
}

 no success. It saves the record, enters the Current date and time, but it does not logout.

 

THANKS!!!