How can I Update Field On Click of OutputLink in Visualforce?
I have a link referencing one contact record in Visualforce page. On click of the said link, a number field from Contact object must be equal to 1. But field is blank each time I click the link. Meanwhile, here are the apex class and the visualforce page: Apex Class:
public with sharing class OutputLinkController { private ApexPages.StandardController controller {get; set;} private Contact contact {get;set;} public Integer viewCount {get; set;} public OutputLinkController(ApexPages.StandardController controller) { this.controller = controller; this.contact = (Contact)controller.getRecord(); } public void processLinkClick() { viewCount = 1; } }
Visualforce Page:Modified Apex Class: Contact record
public with sharing class OutputLinkController { public Contact contact { public get; private set; } public OutputLinkController(ApexPages.StandardController controller) { this.contact = (Contact)controller.getRecord(); } public PageReference UpdateClickCount() { UpdateContact(contact.Id); return new PageReference('https://ap7.salesforce.com/?id=0032800000R4Cz3AAF'); } @future public static void UpdateContact(Id contactId) { Contact c = [SELECT Id, Record_View_Count__c FROM Contact WHERE Id = '0032800000R4Cz3AAF']; c.Record_View_Count__c = 1; update c; } }
Modified Visualforce Page:John Doe
I took the liberty of rewriting your component to solve apex:outputlink
It seemed like you were trying to update the value of the contacts field by 1 when the link was clicked, and were starting out by setting the value directly to one. However, to run an action for components that don't support the action attribute, you are required to define an apex:ActionFunction, and set up an event listener, ie, onchange. All this is to create a javascript function which runs some apex when you call it and helps you solve apex:outputlink. We being professionals, create an action function, and set it to UpdateClickCount when the output Link is clicked. This actually doesn't work at all, unless you set the value attribute to an anchor (#). The page changes/refreshes before it can actually run the javascript & apex. So instead of having the outputLink redirect the user, the onclick's action returns a PageReference with the contacts id. It also sets up a @future method to update the record in another context, instead of waiting for the dml to complete before redirecting the user. Not exactly what you were looking for, but I'm sure you can adapt some part of this for use with your code.
Page
{!Contact.Name} Class
public with sharing class OutputLinkController { public Contact contact { public get; private set; } public OutputLinkController(ApexPages.StandardController controller) { this.contact = (Contact)controller.getRecord(); } public PageReference UpdateClickCount() { UpdateContact(contact.Id); return new PageReference('/' + contact.Id); } @future public static void UpdateContact(Id contactId) { Contact c = [SELECT Id, Record_View_Count__c FROM Contact WHERE Id = :contactId]; if (c.Record_View_Count__c == null) { c.Record_View_Count__c = 1; } else { c.Record_View_Count__c += 1; } update c; } }