I'm having an issue getting a button to execute javascript correctly on a Visualforce page. The button is supposed to execute an Apsona Merge Action, but instead, it just refreshes the page.

When I put the javascript into a Custom Button, it works fine. So I know the javascript itself is working, but there must be something wrong with the way I have it here:

<apex:includeScript value="https://service.apsona.com/sfdc/apsona_inline.min.js"/>
<script type="text/javascript">
    // script to make sure Apsona has session ID
    sforce.connection.sessionId = "{!$Api.Session_ID}";

    // hack that prevents Salesforce from always putting focus
    // on the first date field
    window.onload = function() { }
</script>

<button onclick="apsona.runAction ('buttonMerge', { 
'actionId': 'a2Of0000000RJ1BEAW', 
'recordId': '{!Campaign.Id}', 
'linkFieldId': 'Id', 
'element': this 
 });">VIP CRCF</button>

Any suggestions?

up vote 2 down vote accepted

I blieve if you add return false; it will stop refreshing the page.. and to keep the code clear you can move the method into Javascript function and call it from the onclick Handler

<apex:includeScript value="https://service.apsona.com/sfdc/apsona_inline.min.js"/>
<script type="text/javascript">
    // script to make sure Apsona has session ID
    sforce.connection.sessionId = "{!$Api.Session_ID}";

    // hack that prevents Salesforce from always putting focus
    // on the first date field
    window.onload = function() { }

    function callApsona(){
        apsona.runAction ('buttonMerge', { 
                'actionId': 'a2Of0000000RJ1BEAW', 
                'recordId': '{!Campaign.Id}', 
                'linkFieldId': 'Id', 
                'element': this 
        });
    }
</script>

<button onclick="callApsona();return false;">VIP CRCF</button>
  • That did it. Thanks! – TheDevAdmin Jun 14 '16 at 13:40

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.