0

We have 2 processes that our inside sales team needs to complete when working a new lead. Each of these processes is accessed via a custom button on the lead record that opens a VisualForce page. They then complete fields in the VisualForce page and "save" when complete.

I have created "started" and a "completed" date/time fields on the Lead record, and would like them to be updated based on the activities of the process. Simply put, when the rep clicks the button to start the process (opening the VisualForce page), I want to update the "started" field, and when the click save on the VisualForce page) I want it to populate the "completed" field. (Alternately, I could have a workflow to update completed based on the fields themselves being updated).

How would you go about doing this? I can't seem to get a workflow to kick off when the button is pushed, and I'm still new enough to the VF pages that I don't know if there is a piece of code for updating.

  • what happens if rep hits 'cancel' on the opened page? – cropredy Jan 4 '16 at 20:48
  • right now? nothing. I'm not sure on that portion of it yet. – Matt Lyman Jan 4 '16 at 20:59
0

Usually I would do this through a controller, so my button would look like

<apex:commandButton value="End" action="{!updateEnd}" />

And controller

public pageController()
{
    if (record.Start__c == null)
        record.Start__c = System.Now();
}
public PageReference updateEnd()
{
    record.Completed__c = System.Now();
    update record; 
    return null;
}

However, if you are not using a custom controller, you can probably accomplish the same thing in an actionFunction, using javascript to set start time's value to the current time and the actionfunction for the button.

  • custom button might be as simple as /apex/mypage?id={!lead.id} in which case there is no commandButton to start; use page action method instead to record 'start'; need also to handle 'cancel' event – cropredy Jan 4 '16 at 20:49
  • 1
    @crop1645 Ah, I didn't read the question carefully enough. Updating the start time when the page is opened should be simple enough through the controller, will update answer. This also solves the cancel issue, since nothing needs to be committed to the database in that case. – IllusiveBrian Jan 4 '16 at 21:01
0

Assuming you are passing Id as a url parameter. you can update the started field in action attribute of VF page and the completed field on click of command button. Here is the code:

VF Page:

<apex:page controller="UpdateField" action="{!updateStarted}">
  <apex:form >
      <apex:commandButton value="End" action="{!updateCompleted}"/>
  </apex:form>

</apex:page>

Controller:

public with sharing class UpdateField {
    public string LeadId;
    public PageReference updateCompleted() {
        List<Lead> LeadList = [Select Id,Completed__c from Lead where Id =: LeadId limit 1];
        if(LeadList.size() > 0){
        if(LeadList[0].Completed__c == null){
            LeadList[0].Completed__c = system.today();
            update LeadList[0];
        }
        }
        return null;
    }



    public pagereference updateStarted(){
        LeadId = ApexPages.currentPage().getParameters().get('id');
        List<Lead> LeadList = [Select Id,Started__c from Lead where Id =: LeadId limit 1];
        if(LeadList.size() > 0){
        if(LeadList[0].Started__c == null){
            LeadList[0].Started__c = system.today();
            update LeadList[0];
        }
        }
        return null;
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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