Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Hi I created a Visual force page for opportunities and displayed check box what I am looking after if checkbox == true then a date/Time field in opportunity need to populate with today's date.

Can anybody help me out with some sample piece of code please

share|improve this question
    
stackoverflow.com/questions/8578748/… The above link can help you get started ! –  Mohith Shrivastava Dec 18 '12 at 11:38
add comment

2 Answers 2

up vote 2 down vote accepted

Apex method:

public Opportunity oop { get; set; }
public Boolean myCheckbox { get; set; }

// Constructor
public YourClass()
{
    // Defaulf value of the checkbox
    myCheckbox = false;
}

public PageReference setDatetime()
{
    if(myCheckbox)
        oop.MyDatetime__c = Datetime.now();
    else
        oop.MyDatetime__c = null;

    return null;
}

Visualforce:

Checkbox: 
<apex:inputCheckbox value="{!myCheckbox}">
    <apex:actionSupport event="onchange" reRender="none" action="{!setDatetime}"/>
</apex:inputCheckbox>

share|improve this answer
    
Hi mastOr this thanks for the reply, im not displaying any input fields in visual force page only displaying the records. if check box is checked and saved then date/time field should populate in UI page and record need to be save –  rakesh Dec 18 '12 at 13:02
    
HI, just updated my code. If checkbox is clicked - the date field populated with current date/time. I think this is what you mean an need? –  mast0r Dec 18 '12 at 13:44
    
hi mastOr im sorry abt i didnt clearly mentioned the question im really sry abt that, i displayed the opp records on vf page with <apex:inputCheck box/> but not the custom field when the checkbox is true then the date/time field in opp should populate with today date(Datetime.now()). –  rakesh Dec 18 '12 at 13:52
    
Just updated my example for use with a normal checkbox. Hope this help. –  mast0r Dec 18 '12 at 14:04
add comment

What you are trying to do is quite common, I have few articles about this topic. I think the closest to what you are looking for is: http://www.eltoro.it/ArticleViewer?id=a07A000000NPRjKIAX

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.