0

I've to open the pagereference page in new tab-based based on condition only.

This can not possible by <apex:commandbutton> .

Thus I've used the <apex:commandlink> outside <apex:commandbutton>. As per my controller logic in public pageReference InsertPat(), if condition matched then only I've to redirect to new tab, otherwise I've show the alert message in the same tab.

But when I clicked on the command link button then my else block executing but I'm getting null in alert(error). what is the reason?

Is any other way to achieve this? i.e. redirect to new tab based on condition

<apex:page standardController="Account" extensions="accext">
<apex:outputpanel id="panelToRefresh" >
<Script> 
    var error='{!PHQ9_errMsg}';
    function alertError(){
    alert('111');
    if(error != null ) 
    {
        alert(error);
    }

    }
</Script>
</apex:outputpanel>
 <apex:form >

// this is working but this is not redirecting to new tab when required
<apex:commandButton value="Insert Record" action="{!InsertPat}" style="float:right;" oncomplete="alertError();" rerender="panelToRefresh"/>

         Or
//This is not redirecting but showing null value in alert(error);    
<apex:commandLink action="{!InsertPat}" target="{ !IF(true), ''  ,_blank}">
<apex:commandButton value="Insert Record" style="float:right;" oncomplete="alertError();" rerender="panelToRefresh"/></apex:commandLink>        


 </apex:form>
 </apex:page>

Controller :

public with sharing class accext {

Account acc; Account accRec;
public accext(ApexPages.StandardController controller) {
     this.acc= (Account)controller.getRecord();

    accRec= [select id,Name,Rating from Account where Id=:acc.Id];

}
 public string PHQ9_errMsg{get;set;}

 public pageReference InsertPat()
 {
      if(accRec.Rating == 'Hot')
    {
        //This should be open in new tab
        Pagereference pr = New PageReference('/mypage');
        pr.setRedirect(true);
        return pr;
    }
    else{
        //this should not open in new tab. only show alert message in same 
            tab
        PHQ9_errMsg = 'error message';
        return null;
    } 
  }
  }
0

I had a heck of a time trying to get it to work using VF so I resorted to javascript:

Updated action method:

public pageReference InsertPat() {
    if(accRec.Rating == 'Hot') {
        //This should be open in new tab
        nextPage = Page.DisplayImage;
        return null;
    } else {
        //this should not open in new tab. only show alert message in same tab
        PHQ9_errMsg = 'error message';
        return null;
    } 
}

Page:

<apex:page standardController="Account" extensions="accext">
    <apex:outputpanel id="panelToRefresh" >
        <Script> 
            var error='{!PHQ9_errMsg}';

            function alertError(){
                if(error != null && error != '') 
                {
                    alert(error);
                } else {
                    var url = '{!nextPage.URL}'
                    window.open(url);
                }
            }
        </Script>
    </apex:outputpanel>
    <apex:form >
        <apex:commandLink action="{!InsertPat}"
                          reRender="panelToRefresh" oncomplete="alertError()">
            <apex:commandButton value="Insert Record" style="float:right;"/>
        </apex:commandLink>
    </apex:form>
</apex:page>

This works but there are some considerations to keep in mind:

  1. I believe window.open does not work in lightning
  2. You don't really have control if it opens in a new tab vs a new window as this is a browser setting and is up to each user. By default, this is typically in a new tab reference.
  3. When I tried this, I got a pop-up blocker warning and I had to enable pop-ups to get the new window to open.

None of the standard ways I tried using target=_blank on a command link; including both the one you provided that you said worked and other samples I found here on the stackexchange were able to open a page reference returned by the controller in a new tab.

|improve this answer|||||

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.