4

We have created a custom object"Sample", in that we have created a custom button "New record". When we click that new record button it will go to new page where we create new account. which is similar to account lookup in contact. In that lookup filed we have overrid the lookup which will show thw search and new account creation VF page.

My query is when we create a new account using this button"new account" after click save, I can save the new account, it's working fine.But I can't able to pass this newly created account name automatically to parent page, need to redirect. Kindly anyone tell how to do this.Thanks in advance. Have attached the screen shot for this.

Parent page

Lookup page for search & creating new account

Creating new account page

Apex class:

public with sharing class MyCustomLookupController {
public Id accId;   
public Sample__c  sap {get;set;}
 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 Contact contact {get;set;} 
  public MyCustomLookupController() {
    contact = new Contact();
    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);               
  } 
  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);  
  }
 public PageReference saveAccount() {
    insert account;
    // reset the account
    Id redirectId = account.Id;    
    **PageReference newocp = new PageReference('/apex/MyCustomLookupController?accId='+redirectId);**/*Redirect to Parent page*/     
            newocp.setRedirect(false);
            return newocp;            
  } 
  // 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');
  }
  public MyCustomLookupController(ApexPages.StandardController controller) {
    account = new Account();
        this.sap = (Sample__c) controller.getRecord();
        if (ApexPages.currentPage().getParameters().get('accId') != NULL) {
            accId = ApexPages.currentPage().getParameters().get('accId');
        }
       if ( accId != null ) {
        this.sap.Account__c = (Id)accId;
    }
       /* if (accId != NULL) {
            account = [SELECT ID, name from Account where id = : accId];
        }*/
    }     
}

VF Page 1:

<apex:page standardController="Sample__c" id="Page" extensions="MyCustomLookupController"> 
  <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/MyCustomLookupController2?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="{!Sample__c.Account__c}" />        
      </apex:pageBlockSection>
    </apex:PageBlock>
  </apex:form> 
</apex:page>

VF page 2:

<apex:page controller="MyCustomLookupController"  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: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.CustomAccountLookup}" var="f">
              <apex:inputField value="{!Account[f]}"/>
            </apex:repeat>
          </apex:pageBlockSection> 
        </apex:pageBlock>

      </apex:tab>
    </apex:tabPanel>
  </apex:outputPanel>
  </apex:form>
</apex:page>
1
  • Hey were you able to find the solution for this
    – Heena
    Jul 29, 2016 at 6:22

3 Answers 3

1

It looks like you've got the right idea but I've never used one controller as an extension and as a main controller. Have you checked that the appropriate constructors are being run for each page? I suspect the re-use of the same controller is causing an issue, the best option might be to separate this into two controllers so the one for the first page just does the parameter checking:

public with sharing class MyControllerExntension {
  public Sample__c sap {get; set;}

  public MyControllerExntension(ApexPages.StandardController controller) {
    sap = (Sample__c) controller.getRecord();

    if (ApexPages.currentPage().getParameters().get('accId') != NULL) {
        sap.Account__c = (Id)accId;
    }
  }
}

You may also need to save record for the lookup field on the VF page to reflect the account name, though don't quote me on that!

2
  • thanks for the reply. My query is while saving the new account it must go to the parent page where we clicked lookup icon. which is similar to search in lookup .For example in the search lookup window is if we type "account1" which already exist, then we click on that account name it is populated to Account name field in Contact.Like this when I click save, the newly created account name must be return back to Account name field in parent page. NOTE: I can able to pass the newly created account name within the same page(ie,.lookup window) but i need to return it back to parent page Jul 31, 2013 at 10:06
  • -where i click lookup icon Jul 31, 2013 at 10:14
1

Are you able to override the page where you clicked the lookup icon? If so, then putting code similar to the code that LaceySnr posted in an extension to the parent page (not this page) might work.

E.g if the parent page is a Contact page, then you override the contact creation page and the code will be myContact = (Contact) controller.getRecord() and then myContact.Account__c = (Id)accId ...

0

I implemented below code for new contact creation I hope this will help you

Create one visualforce page with below details and update save account method and add one new exit method to controller class

  <apex:page controller="CustomContactLookupController" action="{!exitAction}">
   <apex:form id="myForm">
  <!-- <apex:inputhidden value="{!ContactInfo.name}" id="contactName"/> -->
  <!-- <apex:inputhidden value="{!ContactInfo.id}" id="contactid"/> -->
  <script type="text/javascript"> 
top.window.opener.lookupPick2('{!FormTag}','{!TextBox}_lkid','{!TextBox}','{!ContactInfo.id   }','{!JSENCODE(ContactInfo.name)}', false);
  </script>
   </apex:form> 
  </apex:page>



 // save the new Contact record
 public PageReference saveContact() {
  try{
    contact.AccountId=accountId;
     insert contact;
    PageReference exitWindow = new PageReference('/apex/exitPage'); 
     exitWindow.setRedirect(false);
    return exitWindow;
     }
   catch(Exception e){ 
    // reset the Contact
    //  contact = new Contact();
   return null;}
    }
        public PageReference exitAction(){
         if(contact.AccountId !=null && contact.lastname !=null){
      ContactInfo=[select id,name from contact where accountid =:contact.AccountId and      lastname =:contact.lastname and firstname =:contact.firstname limit 1];}
 return null;
  }

You must log in to answer this question.

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