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
CushtyCushty 

visualforce page help redirecting

Hi,

I am having a crack at creating a very simply visualforce page where a user can Set a reason for Recycling a lead or Disqualifying a lead..

The user would click a button on the lead object which redirects them to a simple page which shows two fields (dependant picklist) of the Stage and then a reason of that lead.
They only see one option from the picklist and select a reason then click save where thery are redirected back to the lead which is updated.

A bit like closing a lead...

Here is the page; -

<apex:page standardController="Lead">
    <apex:form >
        <apex:pageBlock title="Recycled Reason">
                     <apex:pageblockbuttons >
                     <apex:commandbutton action="{!save}" value="Save"></apex:commandbutton>
                     <apex:commandbutton action="{!cancel}" value="Cancel"></apex:commandbutton>
                     </apex:pageblockbuttons>
                     
                <apex:pageBlockSection columns="1">
            <apex:inputField value="{!Lead.Lead_Lifecycle_Stage__c}" required="true"/>
            <apex:inputField value="{!Lead.Recycle_Reasons__c}" required="true"/>
             </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

So far I just have two fields on a page which are the picklist and the picklist reason....
 
Best Answer chosen by Cushty
LBKLBK
Hi Cushty,

You need an extension controller to setup Page redirect.

Here is the class file and modified VF page for you.

APEX Controller
public class extLead {
	public Lead lead;

	public extLead(ApexPages.StandardController controller) {
	this.lead = (Lead)Controller.getRecord();
	
	 }
    
    Public PageReference save(){
		upsert lead;
		PageReference LeadPage = new PageReference('/' + (String)lead.id);
		LeadPage.setRedirect(true);
		return LeadPage;
    }
	Public PageReference cancel(){		 
		PageReference LeadPage = new PageReference('/' + (String)lead.id);
		LeadPage.setRedirect(true);
		return LeadPage;
    }
}
}

VF Page
 
<apex:page standardController="Lead" extensions = "extLead">
    <apex:form >
        <apex:pageBlock title="Recycled Reason">
                     <apex:pageblockbuttons >
                     <apex:commandbutton action="{!save}" value="Save"></apex:commandbutton>
                     <apex:commandbutton action="{!cancel}" value="Cancel"></apex:commandbutton>
                     </apex:pageblockbuttons>
                     
                <apex:pageBlockSection columns="1">
            <apex:inputField value="{!Lead.Lead_Lifecycle_Stage__c}" required="true"/>
            <apex:inputField value="{!Lead.Recycle_Reasons__c}" required="true"/>
             </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
You have to setup a Custom link or button in your page layout to reach this page. I guess you can manage that.

Hope this helps.
 

All Answers

LBKLBK
Hi Cushty,

You need an extension controller to setup Page redirect.

Here is the class file and modified VF page for you.

APEX Controller
public class extLead {
	public Lead lead;

	public extLead(ApexPages.StandardController controller) {
	this.lead = (Lead)Controller.getRecord();
	
	 }
    
    Public PageReference save(){
		upsert lead;
		PageReference LeadPage = new PageReference('/' + (String)lead.id);
		LeadPage.setRedirect(true);
		return LeadPage;
    }
	Public PageReference cancel(){		 
		PageReference LeadPage = new PageReference('/' + (String)lead.id);
		LeadPage.setRedirect(true);
		return LeadPage;
    }
}
}

VF Page
 
<apex:page standardController="Lead" extensions = "extLead">
    <apex:form >
        <apex:pageBlock title="Recycled Reason">
                     <apex:pageblockbuttons >
                     <apex:commandbutton action="{!save}" value="Save"></apex:commandbutton>
                     <apex:commandbutton action="{!cancel}" value="Cancel"></apex:commandbutton>
                     </apex:pageblockbuttons>
                     
                <apex:pageBlockSection columns="1">
            <apex:inputField value="{!Lead.Lead_Lifecycle_Stage__c}" required="true"/>
            <apex:inputField value="{!Lead.Recycle_Reasons__c}" required="true"/>
             </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
You have to setup a Custom link or button in your page layout to reach this page. I guess you can manage that.

Hope this helps.
 
This was selected as the best answer
CushtyCushty
Hi,

Thanks for this worked well!
I made the button which directs you to the page which works ok.  Is it possible to restrict the picklist to only show one of the options?

 
LBKLBK
I doubt that. But, I will still check about it.

I can suggest a better option.

If you want to restrict the user to have only one value for the Pick List, do not show the pick list to the user at all.

Instead you manually assign the value inside your extension controller, just before calling UPSERT.

hope this helps.

Mark it as Best Answer, if it does.
CushtyCushty
I ran into trouble because what I wanted to do ideally is: -
The user doesn't update a picklist field which is the leads status and was read only, instead they can only close the lead (select one opition from the picklist) and add a reason for the closure (dependant picklist of status).
Using validation rules to stop the user selecting the wrong options seemed a bit messy so I tried this way where the click of a button loads the visualforce page where the user only gets the single option to select closed and then select their reason and save.  However this will not work if the lead status field is read only.  
do you think its still possible to keep the field as read only and then when the visualforce page is loaded show only one option or would it be better to use record types to restrict the picklist otions here?
LBKLBK
If the field is read only, you may not edit from Visualforce page (I am not sure though).

If you just want to show the field to the user you can do that.

Otherwise, just allow the user to edit the comments and take care of the status in the controller.

 
CushtyCushty
I have now broken the dependancy of the picklist so the status and comments are separate fields and then made the status an output field so it just shows on the visualforce page.
How do I amend the status via the controller so that if the user saves its updated to recycled otherwise cancel it does not
 
LBKLBK
After line 5 in the above controller code, add the following line.
 
this.lead.Lead_Lifecycle_Stage__c = 'YOUR_STAUS_HERE';

Replace YOUR_STAUS_HERE with your actual Close status.
CushtyCushty
Genius
I've created a reason field which is not a dependant and this workd for just what I need it. thanks
LBKLBK
Great. Mark this question as Solved.