How to Update Field On Click of OutputLink in Visualforce

785    Asked by MichaelVaughan in Salesforce , Asked on Aug 8, 2021

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:

   Contact record    

Modified Apex 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('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  

How to update apex:outputlink in visualforce?


Answered by Matsuda Hara

It more seemed like you were trying to update the value of the contacts field by 1 when the link is clicked, and were starting out by setting the value directly to one. For VisualForce Template ‘/{!relatedTo.id} doesn't work,here you can use this URL in your Value.

            {!relatedTo.Name}

Here we have second approach to update apex:outputlink

Or you can run an action for components that don't support the action attribute, you need to define an apex:ActionFunction, and set up an event listener, ie, onchange. All this does is create a javascript function which runs some apex when you call it.

We create a action function, and set it to UpdateClickCount when the ouputLink 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; } }




Your Answer

Interviews

Parent Categories