0

I am trying to open standard page from a custom Visualforce Page through javascript using command button.But it reacts differently each time.It works properly sometimes.But in some cases it opens a blank window.

    <apex:outputPanel id="jspanel"> 
        <script>  
        function onControllerReturn() {
     window.open('{!message}','_blank');   

        }
        </script>
    </apex:outputPanel>
    <apex:actionFunction name="doControllerSave"    action="{!merged}" 
     oncomplete="onControllerReturn()" rerender="jspanel" />

<apex:commandButton onclick="doControllerSave()" value="Merge" rerender="none" style="background:#D3D3D3;width:150px;" status="loadStatus"/>

Message contains URL Parameter to be passed.

Class:

public PageReference Merged() {
    update ac1;

    string mergeURL = ''; 
    mergelst = new List<String> (); 
    for(Account a:selectedAccounts){ 
        // mergeURL=a.id; 
        System.debug('size of accounts'+mergeURL); 
        account maxvalue=selectedAccounts.get(selectedAccounts.size() - 1); string
        nextvalue=maxvalue.id; 
        System.debug('lastaccountid'+nextvalue);
        mergeURL +='&cid='+a.Id; // mergelst.add(mergeURL);

    }


    try { 
        message ='/merge/accmergewizard.jsp?goNext=+Next+'+mergeURL ;
    } catch(Exception e)
    { 
        ApexPages.addMessages(e); message = 'Whoops! An error occurred -- ' + e.getMessage(); 
    }
    return null;
}

Need Suggestions.Urgent..

3
  • I would say that you need to examine all possible values of message. It's probably null or empty at some point. Commented Sep 22, 2016 at 20:26
  • You should do window.open('{!JSENCODE(message)}','_blank'); instead of window.open('{!message}','_blank');; because code will break if the message contains any single quote('); Commented Jan 4, 2018 at 7:34
  • This will open a window but won't show anything in that because the message contains an invalid URL ApexPages.addMessages(e); message = 'Whoops! An error occurred -- ' + e.getMessage();; Commented Jan 4, 2018 at 7:38

2 Answers 2

0

message might get empty values sometimes. to get updated values on message variable. try this code.

Points to remember 1. script should be rerendered then you will get modified values from controller

<apex:outputpanel id="panel">
<script>
    function openNewWindow() {
        alert('{!url}');
    }
</script>
</apex:outputpanel>    

Apex Class:

public class SectionExtension {
    public String url {get;set;}

    public SectionExtension(ApexPages.StandardController controller) {
        url = 'init val';
    }

    public Pagereference newWindow() {
        system.debug('##########');
        url = '/modified val';
        return null;
    }

}
0

On your command button remove Oncomplete attribute and addrerender tag

<apex:commandButton onclick="doControllerSave()" value="Merge" rerender="none" style="background:#D3D3D3;width:150px;" status="loadStatus"/>

Because sometimes it reload the page before actionfunction method so it will not work.

8
  • @ tushar,doing so gets back to the same page Commented Sep 23, 2016 at 5:44
  • @Pri Check in method if your variable has some value also check if you are getting any error in console. Commented Sep 23, 2016 at 5:48
  • I have updated my code of that [email protected] I debug it I am getting correctly Commented Sep 23, 2016 at 5:56
  • @Pri I think there is ome type here ` message ='/merge/accmergewizard.jsp?goNext=+Next+'+mergeURL` you have included +Next+ in quote as well. Also would you share your updated VF page code. Commented Sep 23, 2016 at 6:15
  • I want that to open always when page redirects.thats why. Commented Sep 23, 2016 at 7:01

You must log in to answer this question.

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