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

apex:actionsupport onclick event is not working



I want to call an apex method from outputilnk.

so, i tried using below code in VF page:

<apex:outputLink value="/{!p.Id}" onclick="CallApexMethod"> {!p.Name} </apex:outputLink>



<apex:actionFunction name="CallApexMethod" action="{!policy5}" onComplete="alert('After apex method') ;"/>


In class:

public PageReference policy5() {
        Update ListPolicy;
        return null;


When I click on the output link , it should actually go to the record detail page.

So, with the above code when I click the link in the same browser Tab then it is working as expected like it is calling apex method and also redirecting to the record detail page.


but if I click on the link to open in new Tab, the above code is not working and its not executing my apex method.


IS there any other way to achieve the functionality i.e. both the ways (in the same tab as well in the new tab)?



Use like this
<apex:outputLink value="your URL" target="_blank">Edit</apex:outputLink>

Thanks and regards,
Trinay Technology Solutions



Thanks for the reply.


I already tried it but of no use, when I click on that link directly then its working.


but if I right click on that link and if I select "Open in new window or Open in new Tab" then its not working.


Try using a commandlink instead of an outputlink, then you can put your method call directly into the link instead of having to use actionFunctions:


<apex:commandLink value="{!p.Name}" action="{!policy5}" oncomplete="alert('After apex method');"/>



And update your policy5 method to return a PageReference, and return your link that way (ie return "/{!p.Id}" in your policy5 method)


Actually, if you wanted to stick to your current way of doing things then there might be a couple of things causing it not to work.


Try changing this

<apex:outputLink value="/{!p.Id}" onclick="CallApexMethod"> {!p.Name} </apex:outputLink>

 to this

<apex:outputLink value="/{!p.Id}" onclick="CallApexMethod();"> {!p.Name} </apex:outputLink>

 Note the (); after your function call.  You have to treat the onclick event like a normal javascript function call, so adding the (); after CallApexMethod should help.


Now, in your action function, change onComplete to oncomplete - event names are case sensitive and I've encountered problems before with events not firing because of case sensitivity.


That should help, having said all that, I'd still go with my original solution because it doesn't require script and so should be more compatible.