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
EMHDevEMHDev 

commandLink with Javascript - setter problem (execution order?)

I have been bashing away at this without success all day and obviously am missing something.  I have a VF page which shows a list of custom objects called Lead Companies, which we use to tie leads from the company together. When a user clicks on one of the lead companies in the list, it displays a list of related leads in another pane (a table cell) on the page.  This works perfectly.

 

When clicking on one of leads, I want to open a new window with the standard salesforce lead detail view.  That way, I don't need to code the page and if there are permissions issues (my page needs to run in system mode), it will be taken care of. 

 

I use a Javascript function to pop up the window.  This works fine from a different part of the page which does something similar (but not the same) for accounts.  However, with the lead, it does not appear to run the setter to get the lead id from the param tag until AFTER calling the Javascript function.  From reading all the docs I can find, it should work.  However I know from previous experience that I must be missing something that will be obvious to those casting a fresh eye over this, so thanks in advance.

 

Javascript function - the alert shows a blank AnyId although the debug log shows that the setter is being called, but it only shows up in the log AFTER I've pressed OK on the alert:

 

<script type="text/javascript">
  function pop(AnyId) {
    var myWindow;
    alert('Id is '+AnyId);
    myWindow = window.open('/'+AnyId,'','width=550,height=550,scrollbars=yes');
  }
 </script>

 Relevant part of Visualforce page:

 

<apex:pageBlock title="Leads in Lead Company" id="leadDetails">    <apex:pageBlockTable value="{!relatedLeads}" var="leads">	    
  <apex:column headerValue="Name"> 
    <apex:commandLink value="{!leads.Name}" onclick="pop('{!LeadId}');" > 
      <apex:param name="LeadId" value="{!leads.Id}" assignTo="{!LeadId}" />
    </apex:commandLink>                
  </apex:column> 
... other columns ...
</apex:pageBlockTable>
</apex:pageBlock>

Controller code:

 

  public String LeadId { 
    get ;
    set {
      LeadId =  '/'+value; 
system.debug('### in LeadId setter: LeadId is '+LeadId);  	 	 
  	}
  } 

If anyone could tell me what I'm doing wrong I'd be very grateful. 

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
jwetzlerjwetzler

Makes perfect sense to me that onclick would get executed before your form is submitted (and thus, your assignTo is populated).  If you switch to oncomplete, so that your javascript fires after the request, the alert works fine.  Although then the pop up doesn't seem to work.  Not sure why, I'm no javascript expert.

 

But why the param anyway?  Why not just pass leads.id to your javascript function?

All Answers

jwetzlerjwetzler

Makes perfect sense to me that onclick would get executed before your form is submitted (and thus, your assignTo is populated).  If you switch to oncomplete, so that your javascript fires after the request, the alert works fine.  Although then the pop up doesn't seem to work.  Not sure why, I'm no javascript expert.

 

But why the param anyway?  Why not just pass leads.id to your javascript function?

This was selected as the best answer
EMHDevEMHDev

I thought that was how I had to pass parameters to Javascript.  You are right - it works perfectly without the param and passing it directly.

 

I knew it would be something simple that would make me kick myself!

 

Thank you, much appreciated.