2

My usecase is that I'm trying to populate a parent record using a lookup window - all constructed using Javascript. However, when the lookup field is populated, the onchange or the onblur event of actionsupport isn't firing. I will need to use this value that's populated in this window and then fire an action method to get some more values and re-render a section below. However, the onchange isn't firing at all. Any thoughts on how to do it?

<apex:pageBlockSectionitem >
   <apex:outputLabel value="Parent Name" style="width:20px; height:20px"/>
   <apex:outputPanel layout="inline" style="vertical-align:middle">
      <apex:actionRegion>
         <apex:inputText  value="{!acct.parentid}" id="targetId" >
            <apex:actionSupport event="onchange" 
               action="{!checkParent}" 
               rerender="pbs1"  />
         </apex:inputText>
      </apex:actionRegion>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <apex:inputText size="20" id="targetName" onFocus="this.blur()" value="{!parentName}" />
      <a  href="#" 
         onclick="openLookupPopup('{!$Component.targetName}', '{!$Component.targetId}'); 
         return false">
         <apex:image style="vertical-align:middle;width:24px; height:24px" 
            value="{!$Resource.lookup}" />
      </a>
   </apex:outputPanel>
</apex:pageBlockSectionitem>
</apex:pageblockSection>
<apex:pageblockSection id="pbs1" title="Family Information" columns="2" collapsible="false">
<script>
   var newWin=null;
   function openLookupPopup(name, id)
   {
       var url="/apex/LookupPopup?namefield=" + name + "&idfield=" + id ;
       newWin=window.open(url, 'Popup','height=500,width=600,left=100,top=100,resizable=no,scrollbars=yes,toolbar=no,status=no');
       if (window.focus) 
       {
           newWin.focus();
       }

       return false;
   }

   function closeLookupPopup()
   {
      if (null!=newWin)
      {
         newWin.close();
      }  
   }

         function fillIn(name, id)
             {
            var winMain=window.opener;
              if (null==winMain)
                   {
                   winMain=window.parent.opener;
                }
          var  
            ele=
       winMain.document.getElementById('{!$CurrentPage.parameters.namefield}');
            ele.value=name;
            ele=
      winMain.document.getElementById('{!$CurrentPage.parameters.idfield}');
             ele.value=id;
             winMain.closeLookupPopup();
        }



</script>

Sorry I had to break up the lines to accommodate within the page. The code is working fine, it's just that I need to make the Onchange action fire after populating the lookup field so that I can perform the steps as stated above.

Please help.


Updated as of 11/24: 1:08pm CT: Per the suggestions provided here, I tried to manually fire the change event. However, id field in the parent page needs to fire for the change event. How do I do that from the lookup (child page). I'm using this code below, but targetId is the ID in the parent page and not child and I only have this value in the URL($CurrentPage.parameters.idfield). How do I get to do this?

j$( 'input[id$=targetId]' ).change(); 



      function fillIn(name, id) 
       { 
          var winMain=window.opener; 
        if (null==winMain) 
              { 
              winMain=window.parent.opener; 
              } 
             var     
ele=winMain.document.getElementById('{!$CurrentPage.parameters.namefield}'); 
                 ele.value=name; 

ele=winMain.document.getElementById('{!$CurrentPage.parameters.idfield}'); 
                  ele.value=id; 
                 alert('id populated'); 
                //new line added here
                j$( 'input[id$=targetId]' ).change(); 
                winMain.closeLookupPopup(); 
               }
4
  • Your actionSupport tag (as written above) is using onBlur which is an event fired after the field loses focus, onChange is an event fired when a field's value changes. You need to use onChange because the user's browser won't ever fire the blur event on the field because it never had focus. Can you confirm that you are using the change event in your code and maybe update the question to be more accurate if so?
    – Mark Pond
    Commented Nov 24, 2015 at 20:02
  • @MarkPond - I changed it to onchange event and it's still not working...Let me update the above post now to show it correctly. I saw in some forum that it's not working for onchange and there was a suggestion to use onblur. Hence, I tried it out.
    – JJSHHS
    Commented Nov 24, 2015 at 20:22
  • What steps did you take to verify that the onchange event isn't firing? Does your page have a pageMessages tag where errors show up? Does your component rerender the pageMessages tag to surface problems? I'd first make sure the change event fires within the parent page by just changing the value manually and then make sure that there aren't validation rules or required fields or other errors preventing the rerender call to the checkParent method from succeeding.
    – Mark Pond
    Commented Nov 24, 2015 at 20:28
  • @MarkPond - Yes, the page does have a pageMessages tag. When I see the debug logs, I see that that once the Lookup page gets closed, the parent page is not being loaded at all. However, without using the lookup window, when I change the value in the field on the parent page (the checkParent method fires), I'm able to capture that in the debug logs.
    – JJSHHS
    Commented Nov 24, 2015 at 20:53

2 Answers 2

1
<apex:inputField id="relatedrecordinputtext" value="{!acct.parentid}"> 
    <apex:actionSupport event="onchange" action="{!GroupPopulated}" rerender="pbs1"/>         
 </apex:inputField>

I Think this will work for you but it does not work then you can manually fire the change event.

 $("input").trigger("change"); //using Jquery

or JAVASCRIPT

  var two = document.getElementById('two');
  two.checked = ! two.checked;
  two.dispatchEvent('change');
3
  • 1
    In this case, using jQuery is a good idea, because differences in IE and other browsers would cause some heartache.
    – sfdcfox
    Commented Nov 24, 2015 at 7:49
  • @sfdcfox yes thats why i give both options. Thanks. Commented Nov 24, 2015 at 8:05
  • @TusharSharma - No, the action support isn't working. As I said I tried the onchange event and it wouldn't work. The only thing I see is you have used apex:inputfield whereas ideally it needs to work for apex:inputhidden (for my usecase) and this is certainly NOT working when I use JS to populate the lookup fields. Even if I manually fire the change event, I still need to call the "groupPopulated" method in the controller and then re-render fields. How would I do that? I'm not sure if I've explained my use case well.
    – JJSHHS
    Commented Nov 24, 2015 at 17:29
1

In order to set the value on the parent page from your own lookup dialog you should leverage the JS function that Salesforce uses itself when this takes place, window.lookupPick2.

The "selector" in your LookupPopup window will call this function on the parent page that launched the popup dialog using the opener reference and set the value of both the text field (for display) as well as the corresponding _lkid hidden field which contains the ID of the record that was selected (populates the reference field with an Id).

<apex:outputLink value="javascript:top.window.opener.lookupPick2('{!FormTag}','{!TextBox}_lkid','{!TextBox}','{!a.Id}','{!a.Name}', false)">{!a.Name}</apex:outputLink>

The merge fields for FormTag and TextBox represent the ids of the elements in the parent page.

  // used by the visualforce page to send the link to the right dom element
  public string getFormTag() {
    return System.currentPageReference().getParameters().get('frm');
  }

  // used by the visualforce page to send the link to the right dom element for the text box
  public string getTextBox() {
    return System.currentPageReference().getParameters().get('txt');
  }

Example code borrowed from Jeff Douglas' blog post about rolling your own lookup window. (probably worth a read, if you haven't yet.)

http://blog.jeffdouglas.com/2011/08/12/roll-your-own-salesforce-lookup-popup-window/

1
  • I got the lookup fields to populate as you can see. But the issue is attaching a jquery onchange event to field on the parent (from within the lookup child window) and thereby "hoping" the actionsupport to trigger on the parent page. Hope this clarifies my qstn.
    – JJSHHS
    Commented Nov 24, 2015 at 19:46

You must log in to answer this question.

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