0

Hi I created a custom List view button with Visualforce. The intension of the custom button is to support bulk approve. Expected behaviour is, when an user press the button, it should load visualforce page and then onload it calls an apex action function. Then oncomplete it should return to the previous page(the list view).

But, when an user logged in and press the button, it keeps loading the visulforce page itself. If the user go back to the list view again, and click the button again, it works fine. The issue happens only in the first click of the user's session.

I'm new to visualforce. Appreciate any help or hint. Please see the related code below.

Visualforce Page

<apex:page standardController="Custom_Object__c" extensions="CustomObjectBulkApproval" recordSetVar="customObjectRecords" >
    <apex:form >
        <apex:actionFunction name="approve" action="{!approveActionFunction}" oncomplete="redirectToListView();" />
    </apex:form>
 
    <script type = "text/javascript">
        window.onload = function() { 
            console.log('window.onload');
            approve();
        };
  
        function redirectToListView(){
            console.log('going back');
            sforce.one.back(true); 
        }
    </script>
</apex:page>

CustomObjectBulkApproval apex class

public class CustomObjectBulkApproval {
    ApexPages.StandardSetController controller;
    String errorOnSubmit;

    public ParticipantActivityBulkApproval(ApexPages.StandardSetController controller) {
        this.controller = controller;
    }

    public void approveActionFunction(){
        return; // do nothing at the moment
    }
}

Also please see the console output first time when the user click the button.

enter image description here

1

Don't use window.onload, instead use a modern event listener:

window.addEventListener('load',approve);

If you override window.onload, weird things can happen.

Second, without a rerender attribute, the page will reload, potentially causing an infinite loop:

<apex:actionFunction rerender="" name="approve" action="{!approveActionFunction}" oncomplete="redirectToListView();" />

Make sure you're not reloading the page automatically.

2
  • I tried adding event Listener and rerender as well. But still force.one.back(true) loads the same page, and continues. What is strange is this happens only when you click the button first time. If you navigate to list view and click the button again, it works fine. – Isuru Buddhika Herath Mar 31 at 13:02
  • @IsuruBuddhikaHerath Maybe there's an intermediate page load? Have you checked the network logs on your browser to check? – sfdcfox Mar 31 at 15:30
0

I couldn't resolve the issue with apex:actionFunction. Instead calling the same method as an action of the visualforce page worked for me. Following is the updated code.

Visualforce Page

<apex:page standardController="Custom_Object__c" extensions="CustomObjectBulkApproval" recordSetVar="customObjectRecords" action="{!approveActionFunction}">
</apex:page>

CustomObjectBulkApproval apex class

public class CustomObjectBulkApproval {
    ApexPages.StandardSetController controller;

    public ParticipantActivityBulkApproval(ApexPages.StandardSetController controller) {
        this.controller = controller;
    }

    public void approveActionFunction(){
        return new ApexPages.Action('{!list}').invoke();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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