1
<apex:page id="employeeLookupPage" standardController="Employee__c" extensions="employeeMultiSelectLookupController" showHeader="false">
<script type="text/javascript">
  function closeAndPassParams(valueTosend){
   alert('inside function');
   alert(valueTosend);
   var parent = window.opener.document;
   alert(parent);
   var targetField = parent.getElementById('{!$CurrentPage.parameters.namefield}');
   alert(targetField);
   targetField.value = valueTosend;
   self.close();
  }
</script>
<apex:form id="EmployeeForm">
    <apex:pageBlock title="Employee List" id="employeeList">
    <apex:pageBlockSection id="Section" showheader="false" columns="1">
            <!--apex:pageblocksectionitem id="SectionItem" >
                <apex:outputLabel value="First Name or Last Name Search" for="searchStringPanel" style="font-weight:Bold;padding-right:10px;" />
                <apex:outputPanel id="searchStringPanel"-->
                    <apex:inputText id="srchString" value="{!searchString}"/>
                    <apex:commandButton title="Go!" value="Go!" reRender="employeePanel" action="{!search}"/>
                <!--/apex:outputPanel>
            </apex:pageblocksectionitem-->
        </apex:pageBlockSection>
        <apex:outputPanel id="employeePanel">
            <apex:pageBlock id="employeeBlock">
                <apex:inputHidden value="{!allSelectedEmpIds}" id="targetId" />
                <apex:inputHidden value="{!empName}" id="namefield" />
                <apex:pageBlockTable value="{!employeeList}" var="e" id="tblResults">
                    <apex:inputHidden value="{!e.employee.Id}" id="namefield" />
                    <apex:column id="selected">
                        <apex:inputCheckbox value="{!e.isSelected}" id="check" >
                        </apex:inputCheckbox>
                    </apex:column>
                    <apex:column >
                        <apex:facet name="header">First Name</apex:facet>
                        <apex:outputText value="{!e.employee.First_Name__c}" id="fname"  />
                    </apex:column>
                    <apex:column headerValue="Last Name">
                        <apex:outputText value="{!e.employee.Last_Name__c}"/>
                    </apex:column>
                    <apex:column headerValue="Email">
                        <apex:outputText value="{!e.employee.Email__c}"/>
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlock>
        </apex:outputPanel>
        <apex:pageBlockButtons location="bottom">
            <div align="Right" draggable="false">
                <apex:commandButton value="Ok" id="selectEmp" action="{!processSelected}">
                    <apex:actionSupport event="onclick" reRender="none" onComplete="closeAndPassParams('{!empName}');" />
                </apex:commandButton>
                <apex:commandButton value="Cancel" onclick="window.top.close()" />
            </div>
        </apex:pageBlockButtons>
    </apex:pageBlock>
</apex:form>

--Lookup page end--- ---Main Page Start--

var popupLookup=null; var textempId; var textempNames; function openEmpLookup(empNames) {
var url="/apex/employeeLookupPage?namefield=" + empNames;
popupLookup=window.open(url, 'Popup','height=500,width=600,left=100,top=100,resizable=no,scrollbars=yes,toolbar=no,status=no'); return false; } ---Main Page end--- --Lookup Controller--- public with sharing class employeeMultiSelectLookupController { public List employeeList {get;set;} public String allSelectedEmpIds{get;set;} public String empName{get;set;} public boolean canLookup {get;set;} public String searchString{get;set;} private final Opportunity opp; String soql;

public class employeeWrapper{
    public boolean isSelected{get;set;}
    public Employee__c employee {get;set;}
    public employeeWrapper(Employee__c e){
        employee = e;
        isSelected = false;
    }
}

public employeeMultiSelectLookupController(ApexPages.StandardController contrl){
    this.empName = 'abc';
}

public void search() {
    getEmployees();
}

public List<employeeWrapper> getEmployees(){
/*    if(employeeList == null){*/
    if(searchString != null){            
        soql = 'SELECT Id,Email__c,First_Name__c,Last_Name__c,FullName__c FROM Employee__c ' + 'WHERE FullName__c LIKE \'%'+searchString+'%\' order by FullName__c';
    }else{
        soql = 'SELECT Id,Email__c,First_Name__c,Last_Name__c,FullName__c FROM Employee__c';
    }       
    employeeList = new List<employeeWrapper>();
    for(Employee__c emp: Database.query(soql)){
            employeeList.add(new employeeWrapper(emp));
        }
//    }       
    return employeeList;
}

public String processSelected(){
    //PageReference np;
    allSelectedEmpIds = '';
    empName = '';
    for (employeeWrapper eEmp: employeeList) {
        System.debug('checkbox selected'+ eEmp.isSelected);
        if(eEmp.isSelected){
            allSelectedEmpIds += ';'+eEmp.employee.Id;
            empName += ';'+eEmp.employee.FullName__c;
        }
    }       
    return empName;
}

public PageReference cancel(){
    return null;
}

}

--Main Controller-- Public Ext2{ public String emailToList {get;set;}
private final Opportunity opp {get;set;}
public String selectedEmpIds{get;set;} public Ext2(ApexPages.StandardController standardController) { this.opp = (Opportunity)standardController.getRecord(); }

}

I have a requirement wherein users should have the ability to associate email recipients(both salesforce and non salesforce users) and upload a document to 'Notes and Attachment' related list. I had a created a custom object which holds both salesforce and non salesforce users. I was able to achieve the custom lookup following blogs from

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

https://bobbuzzard.blogspot.in/2010/09/visualforce-lookup.html#comment-form

I am stuck at the point where I wanted to pass the selected record Id's from lookup page to the main page(screenshot 1) on the click of the 'OK' button of the lookup page. And also need to close the pop up. Any thoughts?

enter image description here

enter image description here

enter image description here

enter image description here

2
  • Can you please include your own markup here? Screencasts are not really very accessible for the broader community.
    – Adrian Larson
    Feb 7, 2017 at 13:57
  • The code is present in different ways in both of those links. Where are you having trouble? Here is the code that passes the Selected value to parent javascript:top.window.opener.lookupPick2('{!FormTag}','{!TextBox}_lkid','{!TextBox}','{!a.Id}','{!a.Name}', false)"
    – Eric
    Feb 7, 2017 at 14:21

2 Answers 2

1

So I understand you are having issues returning multiple selected ids back to your First Page. The blog by Bob Buzzard talks about returning single Id. You just have to tweak it a little bit to achieve your results.

In your Lookup page, on click of OK button, you are calling getMainPage apex method which I believe is fetching which all records are selected. I assume you have a list of strings or ids with the selected ones and you are updating it in your getMainPage method.

So now you can have another property say-

    public String allSelectedEmplIds{get;set;}
    public String allSelectedEmplNames{get;set;}

So you keep all your selected Employee's id in this property semicolon separated.

Boolean isFirst = true;
allSelectedEmplIds = '';
for(EmpWrapper e : employeeList)
{
    if(isFirst)
    {
        allSelectedEmplIds += e.employee.Id;
        isFirst = false;
    }
    else
        allSelectedEmplIds += ';'+e.employee.Id;
}

Same way you would populate the allSelectedEmplNames as well.

So now, you need a oncomplete function in your OK command button:

<apex:commandButton value="Ok" action="{!getMainPage}" oncomplete="closeAndPassParams();"/>

In this closeAndPassParams Javascript function you will send the selected ids back to the First Page.

    function closeAndPassParams()
    {
          var winMain=window.opener;
          if (null==winMain)
          {
             winMain=window.parent.opener;
          }
          var ele=winMain.document.getElementById('{!$CurrentPage.parameters.idfield}'); //We will send the idField param while opening this pop up, stay with me.
          ele.value={!allSelectedEmplIds};
ele = winMain.document.getElementById('{!$CurrentPage.parameters.namefield}');//to display the names
ele.value = {!allSelectedEmplNames}
          winMain.closeLookupPopup();
    }

Done with Lookup page. Now back to the First page,

You have everything sorted out, except that you will need a hidden input text element to store the received ids from the look up page.

<apex:inputHidden value="{!selectedEmplIds}" id="targetId" />

So apparently you need to declare this selectedEmplIds property as well in controller:

public String selectedEmplIds{get;set;}

Now the last thing, you need to pass this apex:inputHidden element's ID to the Lookup page while opening it.

function openLookupPopup()
     {
      var url="/apex/employeeLookupPage?idfield=targetId&nameField=emailToList";//ASSUMING the id of the Email To Field is emailToList.
      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;
        }

You can split the selectedEmplIds string by ; to get the list of Ids when you need it.

if(selectedEmplIds != null)
{
   List<String> empIdList = selectedEmplIds.split(';');
}

That's it. You should be good to go. If not completely, I hope my post would help you to achieve this requirement. Let me know in case of any questions.

6
  • I appreciate your input by taking your valuable time. I was able to get through the IDs. Still I have two more issues. 1. On the popup window on click of the 'Ok' button it doesn't close the window. 2. Also on the close of the pop up window I wanted to display the selected Names. For this I was able to successfully get the Employee Names but not sure how to display the name in the 'Email To List' I have update the code in the original question. Added screenshots for reference.
    – user28452
    Feb 8, 2017 at 15:07
  • I have updated the code to display the names as well. Feb 8, 2017 at 18:21
  • 1.I have two extensions Ext1, Ext2 called on the MainPage. 2.In the MainPage I am opening the lookup page(having Ext2 as the controller). On the click of the 'OK' after selecting employees it is returning the selected emp names in the MainPage. So I believe I donot need to do anything on the Javascript other closing the popup because click of ok button is returning the getter value. 3. So I think it is enough to display the getter value from Ext2 in the Main Page. I am not sure why my getter is not displaying value in the Main page after click of the OK button.
    – user28452
    Feb 10, 2017 at 13:00
  • There seems to be some conflict. Why don't you post another question about this particular issue and close this thread. Feb 10, 2017 at 16:59
  • I think I am almost close. I guess the earlier issue was using apex:commandbutton with action and Onclick. It was always conflicting and I used apex:ActionSupport. With this change, I was able to return the value from Popup page to Parent Page as you described except stuck at one place where the getter setter 'empname' is not returning value from Apex controller to VF page. But If I hard code the value in the constructor for empName it is returning value to the Parent Page properly. I have updated my code in my original question.
    – user28452
    Feb 23, 2017 at 4:21
0

I think the problem is with the below line of code in closeAndPassParams() function in employeelookup page.

ele.value={!allSelectedEmpIds};

This would get compiled on page load. At that time the allSelectedEmpIds variable will be empty. Even after you select the employee, the value w'ont change since it is binded at compile time. I would suggest that you implement the processSelected() in the javascript side and pass the values to the parent window. This will help you pass the updated values everytime the user selects employees.

You must log in to answer this question.

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