+ Start a Discussion
notsosmartnotsosmart 

Return to Page from a Custom Lookup Selection when called from another Custom Lookup

I have Custom  Lookups for Contacts and Accounts.  They work great independently when called from a Visualforce Page.

Except, when the calling page is one of the Lookups.

 

In the case of calling the account lookup from the contact lookup to populate the field of a contact add, it won't return to the contact lookup page at all.

 

The Contact Lookup page is below  (Account Lookup is modled after that)

 

<apex:page controller="CustomContactLookupController"
title="Search"
showHeader="false"
sideBar="false"
tabStyle="contact"
id="pg">

<script type="text/javascript">
function openLookup(baseURL, width, modified, searchParam){
var originalbaseURL = baseURL;
var originalwidth = width;
var originalmodified = modified;
var originalsearchParam = searchParam;

var lookupType = baseURL.substr(baseURL.length-3, 3);
if (modified == '1') baseURL = baseURL + searchParam;

var isCustomLookup = false;

// Following "001" is the lookup type for Account object so change this as per your standard or custom object
if(lookupType == "001"){

var urlArr = baseURL.split("&");
var txtId = '';
if(urlArr.length > 2) {
urlArr = urlArr[1].split('=');
txtId = urlArr[1];
}

// Following is the url of Custom Lookup page. You need to change that accordingly
baseURL = "/apex/CustomAccountLookup?txt=" + txtId;

// Following is the id of apex:form control "myForm". You need to change that accordingly
baseURL = baseURL + "&frm=" + escapeUTF("{!$Component.CustomContactLookup}");
if (modified == '1') {
baseURL = baseURL + "&lksearch=" + searchParam;
}

// Following is the ID of inputField that is the lookup to be customized as custom lookup
if(txtId.indexOf('Account') > -1 ){
isCustomLookup = true;
}
}

// jmc added because above does not set it - this will cause all lookups to override
isCustomLookup = true;

if(isCustomLookup == true){
openPopup(baseURL, "lookup", 350, 480, "width="+width+",height=480,toolbar=no,status=no,directories=no,menubar=no,resizable=yes,scrollable=no", true);
}
else {
if (modified == '1') originalbaseURL = originalbaseURL + originalsearchParam;
openPopup(originalbaseURL, "lookup", 350, 480, "width="+originalwidth+",height=480,toolbar=no,status=no,directories=no,menubar=no,resizable=yes,scrollable=no", true);
}
}
</script>

<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" onclick="!Search" reRender="searchResults">

<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&colon;top.window.opener.lookupPick2('{!FormTag}','{!TextBox}_lkid','{!TextBox}','{!a.Id}','{!a.Name}', false)" rendered="{!NOT(ISNULL(a.Id))}">{!a.Name}</apex:outputLink>
</apex:column>
<apex:column >
<apex:facet name="header">
<apex:outputPanel >Account</apex:outputPanel>
</apex:facet>
<apex:outputLink disabled="true" value="javascript&colon;top.window.opener.lookupPick2('{!FormTag}','{!TextBox}_lkid','{!TextBox}','{!a.Id}','{!a.Account.Name}', false)" rendered="{!NOT(ISNULL(a.Id))}">{!a.Account.Name}</apex:outputLink>
</apex:column>
<apex:column >
<apex:facet name="header">
<apex:outputPanel >Title</apex:outputPanel>
</apex:facet>
<apex:outputLink disabled="true" value="javascript&colon;top.window.opener.lookupPick2('{!FormTag}','{!TextBox}_lkid','{!TextBox}','{!a.Id}','{!a.Title}', false)" rendered="{!NOT(ISNULL(a.Id))}">{!a.Title}</apex:outputLink>
</apex:column>
<apex:column >
<apex:facet name="header">
<apex:outputPanel >Email Name</apex:outputPanel>
</apex:facet>
<apex:outputLink disabled="true" value="javascript&colon;top.window.opener.lookupPick2('{!FormTag}','{!TextBox}_lkid','{!TextBox}','{!a.Id}','{!a.email}', false)" rendered="{!NOT(ISNULL(a.Id))}">{!a.email}</apex:outputLink>
</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 location="top">
<apex:commandButton action="{!savecontact}" value="Save"/>
</apex:pageBlockButtons>
<apex:pageMessages />

<apex:pageBlockSection columns="2">
<apex:inputField value="{!contact.LastName}" required="true"/>
<apex:inputField value="{!contact.FirstName}" required="true"/>
<apex:inputField value="{!contact.Title}"/>
<apex:inputField value="{!contact.Email}"/>
<apex:inputField value="{!contact.AccountId}"/>
<apex:inputField value="{!contact.Department}"/>
<apex:inputField value="{!contact.Phone}"/>
<apex:inputField value="{!contact.MobilePhone}"/>

<apex:inputField value="{!contact.Fax}"/>
<apex:inputField value="{!contact.ReportsToId}"/>
<apex:inputField value="{!contact.AssistantName}"/>
<apex:inputField value="{!contact.AssistantPhone}"/>
<apex:inputField value="{!contact.Description}"/>
<apex:inputField value="{!contact.BirthDate}"/>
<apex:inputField value="{!contact.Date_of_Last_Personal_Contact__c}"/>
</apex:pageBlockSection>
</apex:pageBlock>

</apex:tab>
</apex:tabPanel>
</apex:outputPanel>
</apex:form>
</apex:page>

 

------------

I modeled this after code that was better than the Standard Lookups but inherited a few of the pesky behavors.

Such as:

-- On initial call from a populated field - populated search field with the existing field and instigating that search.

---- I wanted a full search instigated without the user clearing the search field and pressing GO.

--On Add  - not calling that specific search for selection

 

The comments are for those purposes.

 

It does, however, not show an error on add when required field is missing because it's jumping back to the search w/o  validating.  If you select the add tab again, you'll see it's still there.  I just haven't put in the error checking yet.  If that comes to you easily, you can share, but I'm not especially concerned wit it yet.

 

You'll love these processes so it's worth you debugging and sharing with me.

 

I'm glad to be able to payback for all the help you guys have given me.

 

Thanks.

  

 

 

 

reddygari403reddygari403

<apex:page controller="CustomAccountLookupController" title="Search" showHeader="false" sideBar="false" tabStyle="Account" 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&colon;top.window.opener.lookupPick2('{!FormTag}','{!TextBox}_lkid','{!TextBox}','{!a.Id}','{!a.Name}', false)" rendered="{!NOT(ISNULL(a.Id))}">{!a.Name}</apex:outputLink>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:outputPanel>
</apex:actionRegion>

</apex:tab>

<!-- NEW ACCOUNT TAB -->
<apex:tab label="New Account" name="tab2" id="tabTwo">

<apex:pageBlock id="newAccount" title="New Account" >

<apex:pageBlockButtons >
<apex:commandButton action="{!saveAccount}" value="Save"/>
</apex:pageBlockButtons>
<apex:pageMessages />

<apex:pageBlockSection columns="2">
<apex:repeat value="{!$ObjectType.Account.FieldSets.anji}" var="f">
<apex:inputField value="{!Account[f]}"/>
</apex:repeat>
</apex:pageBlockSection>
</apex:pageBlock>

</apex:tab>
</apex:tabPanel>
</apex:outputPanel>
</apex:form>
</apex:page>

 

 

 

 

 

class:

public with sharing class CustomAccountLookupController {

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

public CustomAccountLookupController() {
account = new Account();
// 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<Account> performSearch(string searchString) {

String soql = 'select id, name from account';
if(searchString != '' && searchString != null)
soql = soql + ' where name LIKE \'%' + searchString +'%\'';
soql = soql + ' limit 25';
System.debug(soql);
return database.query(soql);

}

// save the new account record
public PageReference saveAccount() {
insert account;
// reset the account
account = new Account();
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');
}

}

reddygari403reddygari403

<apex:page controller="MyCustomLookupController" id="Page" tabstyle="Contact">

<script type="text/javascript">
function openLookup(baseURL, width, modified, searchParam){
var originalbaseURL = baseURL;
var originalwidth = width;
var originalmodified = modified;
var originalsearchParam = searchParam;

var lookupType = baseURL.substr(baseURL.length-3, 3);
if (modified == '1') baseURL = baseURL + searchParam;

var isCustomLookup = false;

// Following "001" is the lookup type for Account object so change this as per your standard or custom object
if(lookupType == "001"){

var urlArr = baseURL.split("&");
var txtId = '';
if(urlArr.length > 2) {
urlArr = urlArr[1].split('=');
txtId = urlArr[1];
}

// Following is the url of Custom Lookup page. You need to change that accordingly
baseURL = "/apex/CustomAccountLookup?txt=" + txtId;

// Following is the id of apex:form control "myForm". You need to change that accordingly
baseURL = baseURL + "&frm=" + escapeUTF("{!$Component.myForm}");
if (modified == '1') {
baseURL = baseURL + "&lksearch=" + searchParam;
}

// Following is the ID of inputField that is the lookup to be customized as custom lookup
if(txtId.indexOf('Account') > -1 ){
isCustomLookup = true;
}
}


if(isCustomLookup == true){
openPopup(baseURL, "lookup", 350, 480, "width="+width+",height=480,toolbar=no,status=no,directories=no,menubar=no,resizable=yes,scrollable=no", true);
}
else {
if (modified == '1') originalbaseURL = originalbaseURL + originalsearchParam;
openPopup(originalbaseURL, "lookup", 350, 480, "width="+originalwidth+",height=480,toolbar=no,status=no,directories=no,menubar=no,resizable=yes,scrollable=no", true);
}
}
</script>

<apex:sectionHeader title="Demo" subtitle="Custom Lookup" />

<apex:form id="myForm">
<apex:PageBlock id="PageBlock">
<apex:pageBlockSection columns="1" title="Custom Lookup">
<apex:inputField id="Account" value="{!contact.AccountId}" />
</apex:pageBlockSection>
</apex:PageBlock>
</apex:form>

</apex:page>

class:

==========

 

public with sharing class MyCustomLookupController {

public Contact contact {get;set;}

public MyCustomLookupController() {
contact = new Contact();
}

}