0

How can I put the count value I've assigned for an outputlink tag in visualforce page? I've inserted a script that puts count for an onclick event, but I don't know how to assign the count value to a custom field in Account object.

Here's the portion of the page:

<apex:column headerValue="Account">
     <apex:outputLink value="/{!a.Id}" id="aLink">{!a.Name}</apex:outputLink>
</apex:column>

Javascript:

<script type="text/javascript">
      var count = 0;
      var olink = document.getElementById("aLink");

      olink.onclick = function(){
        count++;
      }
</script>
2
  • Do you need it to be updated in the controller upon click, or upon submission of the form?
    – David Reed
    Feb 23, 2018 at 3:59
  • i need it upon click, then the count value should be stored on the custom field
    – Gaynerys
    Feb 23, 2018 at 4:43

1 Answer 1

1

If you need the count to be updated in your Apex controller in real time (i.e., upon each button click), you can use either JavaScript Remoting to asynchronously update the Account Sobject, or use an <apex:actionFunction> component to use JavaScript to call your controller to make updates.

The <apex:actionFunction> component creates a JavaScript function that you can call to invoke in order to fire a Visualforce controller action. Additionally, you can supply parameters, which are assigned to Visualforce controller instance variables. Example:

<apex:actionFunction action="{! updateCount }" name="updateCountJS ">
    <apex:param name="count" assignTo="{! clickCount }" value="" />
</apex:actionFunction>

This defines a function updateCountJS(count) that you can call in JavaScript, which calls the action method updateCount() in your controller. At that time, the parameter of the JavaScript function would be assigned to the clickCount instance variable in the controller. Your Apex action method can then assign that data to an Account sobject variable or take other action.

JavaScript Remoting requires that the Apex method you call be static, meaning it cannot access the controller's state. To go that route, you'd define a method something like this on your controller:

@RemoteAction
global static void updateCount(Id accountId, Integer count) { 
    // Perform work here to query account and update count.
}

Then, in the Visualforce page's JavaScript, invoke the method:

ControllerName.updateCount('{! a.Id }', count);

These examples are a little bit fuzzy - I am not sure how you're maintaining a count of clicks on a link that appears to navigate away from the page - but I believe something along these lines will suit the situation. Please excuse the terseness of my original answer, which did not provide enough information to be especially useful.

2
  • How can I do this using Javascript remoting? Would it be okay if you show me how to apply this on my case?
    – Gaynerys
    Feb 24, 2018 at 18:57
  • I've added some additional details.
    – David Reed
    Feb 24, 2018 at 19:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .