+ Start a Discussion
JessBJessB 

Save Button on VF referencing one field on standard Lead Record - how to save to Lead Record?

I have a VisualForce page to Change the Lead Status, that is opened from the Lead Record page.

 

The idea is that instead of making someone on their mobile device have to click "Edit" and scroll to find the Lead Status field, and then save, we want them to be able to click a button at the top of the Lead record page that opens the VisualForce page with the one field, "Lead Status", that the user can change, and then by hitting the "Save" button, it returns them to the Lead record page with the new saved value

 

This would be great if we could create a Cancel button on the VF page that wouldn't save anything, and just return the user to the Lead record page.

 

I am a total newbie to VF, so I am not sure if there is some kind of Trigger or S-Control that needs to be created, and then added to this code?

 

I feel like this is probably something simple that someone out there could help me get working correctly (since it's one field to update).

 

Here is the VF page:

<apex:page standardcontroller="Lead">
    <apex:form >
    <a name="skiplink"><img src="/s.gif" height='1' width='1' alt="Content Starts Here" class="skiplink skipLinkTargetInner zen-skipLinkTarget" title="Content Starts Here"/></a><div class="bPageTitle"><div class="ptBody"><div class="content"><img src="/s.gif" alt="Lead"  class="pageTitleIcon" title="Lead"/><h1 class="pageType noSecondHeader">Change Status</h1><div class="blank">&nbsp;</div></div><div class="links"><a href="javascript&colon;openPopupFocusEscapePounds(%27/HelpAndTrainingDoor?loc=help&amp;target=leads_massaction.htm&amp;section=Leads%27, %27Help%27, 1024, 768, %27width=1024,height=768,resizable=yes,toolbar=yes,status=yes,scrollbars=yes,menubar=yes,directories=no,location=yes,dependant=no%27, false, false);" title="Help for this Page (New Window)"><span  class="helpLink">Help for this Page</span><img src="/s.gif" alt=""  class="helpIcon"/></a></div></div><div class="ptBreadcrumb"></div></div>
   <h1>Select New Status</h1>
   <apex:inputfield value="{!Lead.Id}"/>
   <apex:inputfield value="{!Lead.Status}"/>
   <apex:commandbutton value="Save" action="{!Save}"/> <apex:commandbutton value="Cancel" action="{!Cancel}"/>
    </apex:form>
</apex:page>

 

JHayes SDJHayes SD

Hi JessBess,

 

You can accomplish this by creating a controller extension class and adding it to your apex:page element.  I tested the following code and it worked for me.  Please remember that you will need to include the id=<Lead id> parameter in the page URL.

 

Controller extension class:

 

public class TestLeadCancel {
	
	ApexPages.StandardController sc = null;
      
    public TestLeadCancel(ApexPages.StandardController ctlr)  
    {  
       sc = ctlr;             
    }  
             
    public PageReference save()  
    {  
        Lead ld = (Lead) sc.getRecord();
        update ld;
        PageReference pr = new PageReference('/' + ld.Id);  
        pr.setRedirect(true);  
        return pr;    
        
    }
    
    public PageReference cancel()
    {
    	Lead ld = (Lead) sc.getRecord();
    	PageReference pr = new PageReference('/' + ld.Id);
    	pr.setRedirect(true);  
        return pr;     	
    }      
}

 

 

Updated VF page: 

 

<apex:page standardcontroller="Lead" extensions="TestLeadCancel">
    <apex:form >
    <a name="skiplink"><img src="/s.gif" height='1' width='1' alt="Content Starts Here" class="skiplink skipLinkTargetInner zen-skipLinkTarget" title="Content Starts Here"/></a><div class="bPageTitle"><div class="ptBody"><div class="content"><img src="/s.gif" alt="Lead"  class="pageTitleIcon" title="Lead"/><h1 class="pageType noSecondHeader">Change Status</h1><div class="blank">&nbsp;</div></div><div class="links"><a href="javascript&colon;openPopupFocusEscapePounds(%27/HelpAndTrainingDoor?loc=help&amp;target=leads_massaction.htm&amp;section=Leads%27, %27Help%27, 1024, 768, %27width=1024,height=768,resizable=yes,toolbar=yes,status=yes,scrollbars=yes,menubar=yes,directories=no,location=yes,dependant=no%27, false, false);" title="Help for this Page (New Window)"><span  class="helpLink">Help for this Page</span><img src="/s.gif" alt=""  class="helpIcon"/></a></div></div><div class="ptBreadcrumb"></div></div>
   <h1>Select New Status</h1>
   <apex:inputfield value="{!Lead.Id}"/>
   <apex:inputfield value="{!Lead.Status}"/>
   <apex:commandbutton value="Save" action="{!Save}"/> <apex:commandbutton value="Cancel" action="{!Cancel}"/>
    </apex:form>
</apex:page>

 

Regards,

Jeremy

JessBJessB

What URL are you referring to, when you say to include id=<Lead id>?

 

I think this is where my error lies because my VF page, included on my Lead record page, reloads with a smaller version of the Lead record page after I click Save or Cancel, and not refreshing the entire record?

 

Here is what the Lead detail page looks like after I click the Save or Cancel button:

JHayes SDJHayes SD

Ahhh I see, you are using an inline Visualforce page.  The ID that I was referring to in my earlier post assumed you were accessing your VF page as a standalone page.  In that case, the URL that pointed to the VF page would need to look like this:

 

https://na11.salesforce.com/apex/TestLeadCancelPage?id=XXXX

 

...where XXXX would be the record id of the Lead record you wanted to navigate back to.   Let me try to work out an example based on the screenshot you posted and I'll follow up.

JessBJessB

You are AMAZING!

JHayes SDJHayes SD

So I changed the code to the following which fixes the inline VF page refresh issue in your screenshot.  Are you looking to redirect to a different page that doesn't include the inline VF page?  If so let me know.

 

Updated page:

 

<apex:page standardcontroller="Lead" extensions="TestLeadCancel">
    <apex:form >
    <apex:outputPanel id="reloadPanel" rendered="{!reloadNeeded}" >
		   	<script type="text/javascript">
				// redirect the top level window
				window.top.location.href = '{!pageURL}';
			</script>	
		</apex:outputPanel>
    <a name="skiplink"><img src="/s.gif" height='1' width='1' alt="Content Starts Here" class="skiplink skipLinkTargetInner zen-skipLinkTarget" title="Content Starts Here"/></a><div class="bPageTitle"><div class="ptBody"><div class="content"><img src="/s.gif" alt="Lead"  class="pageTitleIcon" title="Lead"/><h1 class="pageType noSecondHeader">Change Status</h1><div class="blank">&nbsp;</div></div><div class="links"><a href="javascript&colon;openPopupFocusEscapePounds(%27/HelpAndTrainingDoor?loc=help&amp;target=leads_massaction.htm&amp;section=Leads%27, %27Help%27, 1024, 768, %27width=1024,height=768,resizable=yes,toolbar=yes,status=yes,scrollbars=yes,menubar=yes,directories=no,location=yes,dependant=no%27, false, false);" title="Help for this Page (New Window)"><span  class="helpLink">Help for this Page</span><img src="/s.gif" alt=""  class="helpIcon"/></a></div></div><div class="ptBreadcrumb"></div></div>
   <h1>Select New Status</h1>
   <apex:inputfield value="{!Lead.Id}"/>
   <apex:inputfield value="{!Lead.Status}"/>
   <apex:commandbutton value="Save" action="{!Save}"/> <apex:commandbutton value="Cancel" action="{!Cancel}"/>
    </apex:form>
</apex:page>

 

 

Updated controller:

 

public class TestLeadCancel {
	
	ApexPages.StandardController sc = null;
	Lead ld;
    
    public String pageUrl {set;}
	public Boolean reloadNeeded {get; set;}
      
    public TestLeadCancel(ApexPages.StandardController ctlr)  
    {  
       sc = ctlr;
       ld = (Lead) ctlr.getRecord();                 
    }  
    
    public String getPageUrl() {
		PageReference pr = new ApexPages.StandardController(ld).view();
		return pr.getUrl();
	}
             
    public PageReference save()  
    {  
        ld = (Lead) sc.getRecord();
        update ld;        
        reloadNeeded = true;
        return null;            
    }
    
    public PageReference cancel()
    {
    	ld = (Lead) sc.getRecord();
    	reloadNeeded = true;
        return null;     	
    }      
}