+ Start a Discussion
dreamrealdreamreal 

Need help, some advice

Here's the situation:

 

I have a visualforce page that is accessed from a button on Account. Lets call this mainPage.

What should happen is that this page opens several other visualforce pages, several subPages.

Each subpage corresponds to a specific record. The mainPage will somehow query to find the id's of the record each subpage references (ie. salesforce.com/apex/subpage?id=1234567). And then somehow the mainpage would open each link.

 

Here's the problem:

I dont know how to open multiple windows.

I have a few ideas/observations:

- I dont believ I can use the controller - there is no way to pass the id's to the visualforce page, and my attempts at a 'hack' using escape=false failed

- I think I can do this using Javascript, except I'm not sure I could query to find the id's I need to open these new windows

- I dont understand what Ajax is, but I have a feeling it allows me to do this?

- if I cant do this could I somehow call an apex method in the controller class to pass the id's to javascript on the visualforce page?

- is there anyway of opening a page referencing a specific id without using urls? slightly annoying.

 

I'm obviously a little confused and not certain how all these components play together in salesforce. If you could advise me on this I'd very much appreciate it.

Best Answer chosen by Admin (Salesforce Developers) 
SteveBowerSteveBower

To the second poster: I think the issues are different.  See if this article could help you:  

http://blog.sforce.com/sforce/2009/10/passing-javascript-values-to-apex-controller.html

 

To the first question.  That's interesting.   You're right in that I don't think the controller can force the opening of new Browser Tabs or windows.   (Which I presume is what you want).  So, it will have to come from the Javascript side.

 

What I think you could do is to go to your main VF page and do whatever interactions are needed to kick off the process.  Now, you're in your controller and you've queried the ID's that you want to open.   If you put those id's in List of Id's, then you could use it in your VF page upon return to populate a set of OutputLink tags within a repeat loop.  Sort of:

 

<apex:repeat rendered="{!not(theListOfIDs.isempty())}" value="{!theListOfIDs}" var="anId">

     <apex:outputLink target="_blank" value="{!URLFOR($Page.mySubPage,anId)}" styleClass="abcdefg"/>

</apex:repeat>

 

So, when you return to your page you'll have a bunch of invisible (because there's nothing being rendered for the user visible portion of the outputLink) links that go to the various VF pages that you want to launch.  (target="_blank" will launch them in a new tab/window)

 

The only difficulty is that these links haven't been "clicked on".  So, you'd need to write a small piece of javascript that, on page load, finds all the anchor tags with a class of "abcdefg" and fires their "onclick()" function.    I'm no jquery expert, but I think it would be a one line thing.

 

 

 

So, this is really just off the top of my head (just typing code in the window, it's probably not right)  and I may be misunderstanding your question.  And, perhaps there's some dead simple way that I'm not seeing at the moment because my brain fastened on this mechanism as a cute idea. :-)

 

But, perhaps it helps.  Best, Steve.

All Answers

krishnagkrishnag

hi i am about to post the same issue but different scenarion. I need to pass some input variables to visualforce using javascript. i can using HTMl but when i use the apex:input i dint know what to give the value attribute.

SteveBowerSteveBower

To the second poster: I think the issues are different.  See if this article could help you:  

http://blog.sforce.com/sforce/2009/10/passing-javascript-values-to-apex-controller.html

 

To the first question.  That's interesting.   You're right in that I don't think the controller can force the opening of new Browser Tabs or windows.   (Which I presume is what you want).  So, it will have to come from the Javascript side.

 

What I think you could do is to go to your main VF page and do whatever interactions are needed to kick off the process.  Now, you're in your controller and you've queried the ID's that you want to open.   If you put those id's in List of Id's, then you could use it in your VF page upon return to populate a set of OutputLink tags within a repeat loop.  Sort of:

 

<apex:repeat rendered="{!not(theListOfIDs.isempty())}" value="{!theListOfIDs}" var="anId">

     <apex:outputLink target="_blank" value="{!URLFOR($Page.mySubPage,anId)}" styleClass="abcdefg"/>

</apex:repeat>

 

So, when you return to your page you'll have a bunch of invisible (because there's nothing being rendered for the user visible portion of the outputLink) links that go to the various VF pages that you want to launch.  (target="_blank" will launch them in a new tab/window)

 

The only difficulty is that these links haven't been "clicked on".  So, you'd need to write a small piece of javascript that, on page load, finds all the anchor tags with a class of "abcdefg" and fires their "onclick()" function.    I'm no jquery expert, but I think it would be a one line thing.

 

 

 

So, this is really just off the top of my head (just typing code in the window, it's probably not right)  and I may be misunderstanding your question.  And, perhaps there's some dead simple way that I'm not seeing at the moment because my brain fastened on this mechanism as a cute idea. :-)

 

But, perhaps it helps.  Best, Steve.

This was selected as the best answer
dreamrealdreamreal

Thanks a lot Steve.

I'll give this method a try and get back to you!

dreamrealdreamreal

I attempted to do what you said with little success. Is there any way you could just double check what I wrote?

 

here's the page:

<apex:page standardcontroller="Account" extensions="maininvoice">



<apex:repeat rendered="true" value="{!theListOfIDs}" var="anId">

     <apex:outputLink rendered="true" value="{!URLFOR($Page.invoice2,anId)}" styleClass="name"/>


</apex:repeat>


<script type="text/javascript">
window.onload = new function() { hookfunc(); };
function hookFunc(){
 
                var anchortags=document.getElementsByTagName("a");

                for(i=0;i<=anchortags.length;i++)
                {
 
 
                    var currentTag=anchortags[i];
                    if(currentTag.className=="name")
                    {
                        currentTag.click();
                        
                }
 
 
            }
            
        
        }
</script>

</apex:page>

heres the extension:

 

public class maininvoice {

private final Account master;

public maininvoice(ApexPages.StandardController controller) {
this.master = (Account)controller.getRecord();

}

public Phone_Line__c[] gettheListOfIDs(){

Phone_Line__c[] lines = [SELECT id from Phone_line__c where Phone_line__c.Account__c =: ApexPages.currentPage().getParameters().get('id')];

return lines;
}

}

 

SteveBowerSteveBower

Hi, I haven't pulled this into a Salesforce instance and tried it, however visually I think that the controller is wrong.

 

(I also might just put the Javascript down at the bottom of the page just above the final "</apex:page>" tag instead of trying to hook the onLoad function.  I'm not sure if that's a clean cross browser way to do it and how Salesforce mucks with onLoad.)

 

The "ListOfIds" has to actually be a List (returned by a getter), and the results of the query are an array of Phone_Line__c objects instead of ID's.  So, I think you need:  (Again, just typing it in...)

public class maininvoice {

 

public List<Id> theListOfIDs {get; set;}

public maininvoice(ApexPages.StandardController controller) {

theListOfIDs = new List<Id>();

for (Phone_Line__c pl: [select id from Phone_Line__c where Account__c = :controller.getId()]) theListOfIDs.add(pl.id); } }
Best, Steve.
dreamrealdreamreal

So I did that and checked it out. It seems that the list is being generated and the <apex:repeat> section is working.

However the page on a whole is not working. My guess is that there is something wrong with the javscript. I havent worked much with javascript and additionally have no way to debug (huge problem!). Is there anyway you could check this over one more time? If anyone else could check it over I'd also much appreciate that.

 

Extension:

 

public class maininvoice {

public List<Id> theListOfIDs {get; set;}

    public maininvoice(ApexPages.StandardController controller) {
theListOfIDs = new List<Id>();
    for (Phone_Line__c pl: [select id from Phone_Line__c where Account__c = :controller.getid()]) 
          theListOfIDs.add(pl.id);
          

    }
}

 

 

Page:

 

<apex:page standardcontroller="Account" extensions="maininvoice">

<apex:repeat value="{!theListOfIDs}" var="anId">
     <apex:outputLink target="_blank" value="{!URLFOR($Page.invoice2,anId)}" styleClass="name" />
</apex:repeat>


<script type="text/javascript">

                var anchortags=document.getElementByTagName("a");

                for(i=0;i<=anchortags.length;i++){
                    var currentTag=anchortags[i];
                        if(currentTag.className=="name"){
                        
                        currentTag.click();
                        }
                }
 
</script>

</apex:page>

 

 

SteveBowerSteveBower

Offhand it looks right, and I presume that if you do a "View Source" in your browser you see the tags?

 

If you use Firefox you should install the Firebug plugin.  Chrome also has a built in debugger.   Just put the javascript line:

 

debugger;

 

in your javascript whereever you want the debugger to kick in.   Also, are there any bugs showing up in the Javascript console?

 

Best, Steve.