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
sbrmathsbrmath 

How can use target=_blank and rerendered together in a CommandLink

Here is the code... 
*****page ****
<apex:commandLink rendered="{!result.isXYZAccount == 'NO-ADD'}" value="{!result.isXYZAccount}" action="{!loadGuest}"  rerender="searchResultsPB,errorPanel" />
<apex:actionSupport event="onclick" action="{!disableButtons}" rerender="searchResultsPB,errorPanel"/>
 
***controller****
 public PageReference loadGuest() {
     PageReference pageref = null;
        String winetid = getRequestWINetID();
        String statusCode = '1';
        AccountDisplay accountDisplayFromResults = null;
        Account a;
        
        if(results != null) {
            for(AccountDisplay acc : results) {
                if(acc != null && acc.account != null && acc.account.i_winet_id__c == winetid) {
                    System.debug('Found my AccountDisplay Object');
                    accountDisplayFromResults = acc;
                    break;
                }
            }
            if(accountDisplayFromResults != null) {
                a = GuestLoadRequester.LoadGuest(accountDisplayFromResults.account, baseurl);
                if(a != null && a.Id != null) {
                    System.debug('Inserted Account has id: ' + a.Id);
                    accountDisplayFromResults.id = a.id;
                    accountDisplayFromResults.isSMARTAccount = 'YES';
                    accountDisplayFromResults.Account = a;
                    pageref = new PageReference('/' + a.id);
                } else if (a != null) { // a is not in smart database
statusCode = '2';                
                }
            }
        }
        
        if(pageref == null) {
         pageref = new PageReference('/apex/GuestLoadStatus?winetid=' + winetid + '&status=' + statusCode);
        }
        return pageref;
    } 
 
The goal is to spawn a new window when i click on the link created by CommandLink and at the same time refresh the parent page.
I have tried putting target=_blank for commandLink and it does not work(does not spawn new window), am i missing something? 
Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

I don't think you'll be able to do this using your current method.

 

The problem is that when the browser encounters a target on a link, it interprets this as open a new window (or find an existing window with that name) and display the content.  When you click the link, the current page is no longer involved.

From a VisualForce perspective, when you open a new window you have an entirely new instance of the controller backing it - you can't share controllers across multiple pages.

 

JavaScript is probably your best bet - add another function to your onclick handling that opens the new window and sets its location accordingly.  Of course, this presupposes you can pass enough information on the location URL to generate the new window contents correctly.

 

 

All Answers

bob_buzzardbob_buzzard

I don't think you'll be able to do this using your current method.

 

The problem is that when the browser encounters a target on a link, it interprets this as open a new window (or find an existing window with that name) and display the content.  When you click the link, the current page is no longer involved.

From a VisualForce perspective, when you open a new window you have an entirely new instance of the controller backing it - you can't share controllers across multiple pages.

 

JavaScript is probably your best bet - add another function to your onclick handling that opens the new window and sets its location accordingly.  Of course, this presupposes you can pass enough information on the location URL to generate the new window contents correctly.

 

 

This was selected as the best answer
SteveBowerSteveBower

I don't know if this will help, but here's something I did recently which has some similarity.  The goal was to display Contacts within a VF page and present (along with a bunch of other information) an Edit button for each contact in case the user wanted to modify some data about that Contact while not leaving the VF page.  

 

I wanted to present a commandLink with one action to kick off the Edit of the Contact in a new tab/window, and another to modify the state of the current controller to indicate that a reload was going to be needed once the user was done editing the Contact in the other tab.

 

The new window is opened via javascript on the ActionStatus that's used by the CommandLink.

 

 

.... snip .... (Note, this is within a PageBlockTable)

 

<apex:column headerValue="Edit">

 

<apex:actionRegion id="theEditRegion"> <apex:commandLink action="{!doEdit}" value="Edit" rerender="selectedContacts" rendered="{!not(CurrentContact.needsReload)}" status="theEditStatus"> <apex:param name="editId" value="{!CurrentContact.con.Id}"/> </apex:commandLink> <apex:actionStatus for="theEditRegion" onstop="openEditWindow('{!URLFOR($Action.Contact.Edit,CurrentContact.con.Id)}')" id="theEditStatus"/> </apex:actionRegion>

<apex:commandLink action="{!doReload}" value="Reload" rerender="selectedContacts" rendered="{!CurrentContact.needsReload}" > <apex:param name="editId" value="{!CurrentContact.con.Id}"/> </apex:commandLink> </apex:column> .... end of snip ....

 

 The onOpenEditWindow Javascript function is:

 

 

function openEditWindow(x) { // Now we want to strip off the retURL that we were given and use our own. var ques = x.indexOf('?'); var url = x.substring(0,ques) + '?retURL=/apex/CloseThisPage'; window.open(url); }

 

The page: /apex/CloseThisPage has nothing in it but:

 

<apex:page>

     <script type="text/javascript">

         window.close()

    </script>

</apex:page>

 

This way, when the user saves or cancels the newly opened Edit tab, the user is navigated back to this page which closes the window.  The browser then brings the users focus back to the tab with the VF on it which launched that now closed tab.

 

The user will see the Reload button now instead of the Edit button, and when they use it, the parameter for that Id is passed so the current controller can go and re-query the data for that Contact from the system.

 

This isn't exactly what you're trying to do, but perhaps it will spawn some ideas?  Best, Steve.

 

 

sbrmathsbrmath
I used combination of hidden variable and javascript to get this done.
 
<apex:inputHidden value="{!pageref}" id="pageref" />
<apex:commandLink styleClass="actionLink" rendered="{!result.isXYZAccount == 'NO-ADD'}" value="{!result.isXYZAccount}" action="{!loadGuest}" oncomplete="window.open(document.getElementById('{!$Component.resultForm.pageref}').value);" rerender="searchResultsPB,errorPanel"/>