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
MedhanieHabteMedhanieHabte 

Unable to close visualforce popup after record has been created

Greetings, I've been working with Jeff Douglas' Roll Your Salesforce Lookup (http://blog.jeffdouglas.com/2011/08/12/roll-your-own-salesforce-lookup-popup-window/) as part of an app I've been building in our org. This app serves to replace the dreaded quickcreate system and allows for the inline creation of contact records, should they not be available.

One of the initial issue we have encountered is the inability to look up or add contact records with quotes (') in their first or last name as part of the query. This returns an exception error. A step I took to address was to escapesinglequote, which allows for looking up contacts with ('). Now with Single Quote escaped, I am unable to close the popup when I create new records or select a record with a (') in the result. I'm certain this may have to do with the code in the VF page. I am wondering what steps I need to take.

Are there any steps, I can take to address. Class and Page attached below.

Class
public class ContactLookupControl {

  public Contact contact {get;set;} // new contact to create
  public List<Contact> results{get;set;} // search results
  public string searchString{get;set;} // search keyword

  public ContactLookupControl() {
    contact = new Contact();
    // get the current search string
    searchString = System.currentPageReference().getParameters().get('lksrch');
    runSearch();  
  }

  // performs the keyword search
  public PageReference search() {
    runSearch();
    return null;
  }

  // prepare the query and issue the search command
  private void runSearch() {
    // TODO prepare query string for complex serarches & prevent injections
    results = performSearch(searchString);               
  } 

  // run the search and return the records found. 
  private List<Contact> performSearch(string searchString) {

    String soql = 'select id, name,account.Name,MailingStreet,MailingCity,MailingState,MailingPostalCode,MailingCountry,Phone,Email from contact';
    if(searchString != '' && searchString != null)
      soql = soql +  ' where name LIKE \'%' + searchString +'%\'';
    soql = soql + ' limit 25';
    System.debug(soql);
    return database.query(soql); 

  }

  // save the new contact record
  public PageReference saveContact() {
    insert contact;
    contact=[select id,name,FirstName,LastName,AccountId,MailingStreet,MailingCity,MailingState,MailingPostalCode,MailingCountry,Phone,Email from contact where id=:contact.id];
    // reset the contact
    //contact = new Contact();
    return null;
  }

  // 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');
  }
  }




Page
<apex:page controller="ContactLookupControl"
  title="Search"
  showHeader="false"
  sideBar="false"
  tabStyle="Contact"
  id="pg">
  <apex:form >
  <apex:outputPanel id="page" layout="block" style="margin:5px;padding:10px;padding-top:2px;">
  <apex:tabPanel switchType="client" selectedTab="name1" id="tabbedPanel">
    <!-- SEARCH TAB -->
      <apex:tab label="Search" name="tab1" id="tabOne"> 
      <apex:actionRegion > 
      <apex:outputPanel id="top" layout="block" style="margin:5px;padding:10px;padding-top:2px;">
        <apex:outputLabel value="Search" style="font-weight:Bold;padding-right:10px;" for="txtSearch"/>
        <apex:inputText id="txtSearch" value="{!searchString}" />
          <span style="padding-left:5px"><apex:commandButton id="btnGo" value="Go" action="{!Search}" rerender="searchResults"></apex:commandButton></span>
      </apex:outputPanel>
   
       <apex:outputPanel id="pnlSearchResults" style="margin:10px;height:350px;overflow-Y:auto;" layout="block">
        <apex:pageBlock id="searchResults">
          <apex:pageBlockTable value="{!results}" var="a" id="tblResults">
            <apex:column >
              <apex:facet name="header">
                <apex:outputPanel >Name</apex:outputPanel>
              </apex:facet>
               <apex:outputLink value="javascript:top.window.opener.lookupPick2('{!FormTag}','{!TextBox}_lkid','{!TextBox}','{!a.Id}','{!a.Name}', false);self.close();" rendered="{!NOT(ISNULL(a.Id))}">{!a.Name}</apex:outputLink>    
            </apex:column>
        <apex:column >
              <apex:facet name="header">
                <apex:outputPanel >Account Name</apex:outputPanel>
              </apex:facet>
               {!a.Account.Name}   
            </apex:column>
        <apex:column >
              <apex:facet name="header">
                <apex:outputPanel >Street</apex:outputPanel>
              </apex:facet>
               {!a.MailingStreet}   
            </apex:column>
  <apex:column >
              <apex:facet name="header">
                <apex:outputPanel >City</apex:outputPanel>
              </apex:facet>
               {!a.MailingCity}   
            </apex:column>
 <apex:column >
              <apex:facet name="header">
                <apex:outputPanel >State</apex:outputPanel>
              </apex:facet>
               {!a.MailingState}   
            </apex:column>
 <apex:column >
              <apex:facet name="header">
                <apex:outputPanel >Postal Code</apex:outputPanel>
              </apex:facet>
               {!a.MailingPostalCode}   
            </apex:column>
<apex:column >
              <apex:facet name="header">
                <apex:outputPanel >Phone</apex:outputPanel>
              </apex:facet>
               {!a.Phone}   
            </apex:column>
<apex:column >
              <apex:facet name="header">
                <apex:outputPanel >Email</apex:outputPanel>
              </apex:facet>
               {!a.Email}   
            </apex:column>
          </apex:pageBlockTable>
        </apex:pageBlock>
       </apex:outputPanel>
      </apex:actionRegion>
    </apex:tab>
    <!-- NEW CONTACT TAB -->
    <apex:tab label="New Contact" name="tab2" id="tabTwo">
        <apex:pageBlock id="newContact" title="New Contact" >
          <apex:pageBlockButtons >
            <apex:commandButton action="{!saveContact}" value="Save & Close"/>
          </apex:pageBlockButtons>
          <apex:pageMessages />
          <apex:pageBlockSection columns="2">
              <apex:inputField value="{!Contact.FirstName}"/>
              <apex:inputField value="{!Contact.LastName}"/>
              <apex:inputField value="{!Contact.AccountId}"/>
              <apex:inputField value="{!Contact.MailingStreet}"/>
              <apex:inputField value="{!Contact.MailingCity}"/>
              <apex:inputField value="{!Contact.MailingState}"/>
           <apex:inputField value="{!Contact.MailingPostalCode}"/>
           <apex:inputField value="{!Contact.MailingCountry}"/>
           <apex:inputField value="{!Contact.Phone}"/>
           <apex:inputField value="{!Contact.Email}"/>
          </apex:pageBlockSection>
        </apex:pageBlock>   
        <script>
        if({!Contact.id!=null}){
            //alert('{!Contact.Name} {!Contact.id}');
            top.window.opener.lookupPick2('{!FormTag}','{!TextBox}_lkid','{!TextBox}','{!Contact.Id}','{!Contact.Name}', false);self.close();
        }
        </script>
    </apex:tab>
  </apex:tabPanel>
  </apex:outputPanel>
  </apex:form>
</apex:page>

Hope it helps.
Ashish Dev 3Ashish Dev 3
It looks like some javascript error might have occured. In chrome you can debug in developer console and let us know if result?
Medhanie HabteMedhanie Habte
JSENCODE did the trick. In running my debug I noticed the missing ) generated the syntax error, and it was because the names we're not escaping the unsecure JS characters. I enabled JSENCODE and it worked perfectly! Hope it helps.