+ Start a Discussion
prasad_more1.3158103303596086Eprasad_more1.3158103303596086E 

Open Custom Visualforce page on focus of City field in Address Information section

Hi,

 

When adding/editing account record, I want to auto-populate Billing State/Province and Billing Country fields when Billing City is selected.

 

I have developed a Visualforce for searching Cities. This Visualforce page should open in a popup window when focus is on Billing City field. In the popup window, User will search for City and select City. When City is selected in popup window, selected City is displayed in Billing City field. I have the data for  State/Province and Country for Cities displayed in popup window. I should be able to pass State/Province and Country for selected City from popup window and auto-populate Billing State/Province and Billing Country on account page for selected City. Is this possible with layout provided for Standard Account page?

 

The Interactive Address lookup application from AppExchange Market place does the same thing. It populates all the address fields when address is selected in popup window and popup window is displayed when focus is on Billing Street field.

 

Thanks,

Prasad

 

 

sf_ckempsf_ckemp

You should be able to do this by creating a Visualforce component on the record's Edit page that uses Javascript to traverse the DOM to find the right field on that page and inject a lookup button to open the popup window.  The popup window would use Javascript's window.opener to fill in the Billng State/Province and Country fields appropriately.

 

cK 

prasad_more1.3158103303596086Eprasad_more1.3158103303596086E

Thank you for your reply.

 

I created a Visualforce component and added it in Visualforce page and modified Account page layout to add the page as inline visualforce page. But Inline visualforce page is displayed in record detail page only i.e. view mode. It is NOT displayed in record add/edit page. The same thing applies for Custom button and Link. They are displayed in record detail page and not in record add/edit page.

 

Can you guide me on how to add visualforce component on record add/edit page? If you have the steps on how to do it or any URL which explains it, please forwared it to me.

 

Thanks,

Prasad

 

sf_ckempsf_ckemp

In that case, you might want to add the JavaScript as a Home Page Component and expose it via the sidebar.  You can do that via Setup > App Setup > Customize > Home > Home Page Components and create a new Custom Component of type HTML Area and click the Show HTML checkbox and put your JavaScript in a script tag there. 

 

The HTML for custom buttons/links can be injected via JavaScript DOM traversal in the same fashion as mentioned in the last post.

 

cK

prasad_more1.3158103303596086Eprasad_more1.3158103303596086E

Hi ckemp,

 

Thank you very much for sending detailed steps on how to inject JavaScript using Home Page Custom Component on record Add/Edit page. I'm able to display lookup link beside City field for opening custom Visualforce page.

 

After displaying hyperlink for opening custom Visualforce page for lookup on City, I'm facing issue when trying to populate City field on Add Account page with selected City name in popup window. I get "Permission denied" error when I try to call JavaScript method on Add Account page(Parent) from poup window for lookup on City(child).

 

What I observed is that domain in the URL for custom Visualforce is changed from "ap1.salesforce.com" to "prasadmore.ap1.visual.force.com". Note, "prasadmore" in the URL is the Namespace Prefix added in Developer Settins for creating managed package. I think because domain is changed, I'm getting "Permission denied" error.

 

Can you tell me why domain name is changed when custom Visualforce page is opened from Account Add page.

 

Following is the Javascript I added in Home Page Component for displaying lookup beside City field on Add Account page and opening custom Visualforce lookup page. After searching for cities in custom Visualforce lookup page, when user clicks on City name hyperlink, I'm calling "closeLookupPopup" Javascript(present Add Accoount page) from popup page and I get "Permission denied" error.

 

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript">
 var j$ = jQuery.noConflict();
        var documentTitle = document.title;
 var newWin=null;
var CityFieldId = '00N90000002rqNu';
var StateFieldId = '00N90000002rqNp';
var CountryFieldId = '00N90000002rqNz';
if ((documentTitle.indexOf("Edit:") > 0) && ((documentTitle.indexOf("Account") == 0) || (documentTitle.indexOf("Lead") == 0)))
 {
 
  j$(document).ready(function()
         {
var lookupLink = "<a href=\"#\" onclick=\"javascript&colon; return openLookupPopup('00N90000002rqNu', '00N90000002rqNp', '00N90000002rqNz');\"  title=\"City Lookup (New Window)\"><img src=\"/s.gif\" alt=\"City Lookup (New Window)\" class=\"lookupIcon\" title=\"City Lookup (New Window)\"></a>";
   var innerText = "";
   j$.each(j$("label[for]"), function() {
    innerText = this.innerText;
    if (innerText == "Billing City")
     j$("#" + this.htmlFor).after(lookupLink); 
   });
         });
 }
function openLookupPopup(cityFieldId, stateFieldId, countryFieldId)
 {
         var url="/apex/SearchGeographicInformation?nameCityField=" + cityFieldId + "&nameStateField=" + stateFieldId + "&nameCountryField=" + countryFieldId;
CityFieldId = cityFieldId;
StateFieldId = stateFieldId;
CountryFieldId = countryFieldId;
         /*openPopup(url, "lookup", 350, 480, "width=600,height=480,toolbar=yes,status=no,directories=no,menubar=no,resizable=no,scrollable=no", true);*/
newWin = window.open(url, 'Lookup','height=500,width=600,left=100,top=100,resizable=no,scrollbars=no,toolbar=no,status=no');
if (window.focus)
{
newWin.focus();
}
return false;
     }
                 
     function closeLookupPopup(cityName, stateName, countryName)
     {
         var eleCityName = document.getElementById(CityFieldId);
var eleStateName = document.getElementById(StateFieldId);
var eleCountryName = document.getElementById(CountryFieldId);
eleCityName.value = cityName;
eleStateName.value = stateName;
eleCountryName.value = countryName;
if (null!=newWin)
         {
            newWin.close();
         } 
     }
</script>

sf_ckempsf_ckemp

In that case, it really only leaves you with two options:

 

(1) Move that Visualforce page out of the Managed Package it is in.  Visualforce pages inside Managed Packages are given a namespace to ensure there are no conflicts when duplicate names exist from different packages (i.e. two different app publishers calling a page myPage - how does Salesforce know where to go without namespaces?)

 

(2) Recreate the standard Account Add page as a custom Visualforce page and included it in the Managed Package.  That way you will be using the same domain name and JavaScript's security will not give you a "permission denied" error.

 

cK

prasad_more1.3158103303596086Eprasad_more1.3158103303596086E

Thank you very much, ckemp. Your suggestion for creating lookup using Custom Home Page Component was very helpful. We have resolved the issue with domain. Thank you again for your help.

Kirill_YunussovKirill_Yunussov

When enabling "Show Custom Sidebar Components on All Pages", how can you only display specific components, instead of all of them?   We have 10 components, and I only want to show the one with the Javascript.