2

Im trying to this example program,i seen this program in stack exchange.The issues are connect one VF page 1 to VF page 2 and click save button in VF page 2,it must populate account name in VF page 1 (Accout Lookup Textfield).I tried but i have getting this error. Please check it am i doing right are wrong.

Error:Visualforce Error
System.StringException: Invalid id: null 
Class.MyCustomLookupController.<init>: line 67, column 1

Apex class:

public with sharing class MyCustomLookupController {

    public Id accId;
    public SAP_SD__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
        account = new Account();
        //return null;
        PageReference newocp = new PageReference('/apex/MyCustomLookupController?accId='+account.Id);
        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 = (SAP_SD__c) controller.getRecord();
        if (ApexPages.currentPage().getParameters().get('accId') != NULL) {
            **accId = ApexPages.currentPage().getParameters().get('accId');**/* 69 line*/
        }
        if (accId != NULL) {
           account = [SELECT ID, name from Account where id = : accId];
        }
    }
}

VF Page 1:

<apex:page standardController="SAP_SD__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="{!contact.AccountId}"  />-->
            <apex:inputField id="Account" value="{!sap.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>

  • That's a lot of code pasted there. Which is line 67? The error is telling you that on that line there is an assignment, and an ID value is expected but you are passing nothing (null). Maybe this is because a variable hasn't been correctly initialised or a Select statement hasn't found a value. – Doug B Jul 22 '13 at 12:51
3

After saving your account, you're redirecting and passing a null value for your accId URL parameter, since you are overwriting your account variable between the insert and the redirect:

public PageReference saveAccount() {
    insert account;
    account = new Account(); // <-- this will effectively set account.Id to null
    PageReference newocp = new PageReference('/apex/MyCustomLookupController?accId='+account.Id);
    newocp.setRedirect(false);
    return newocp;
}

This is eventually causing problems in the page's constructor after the redirect, when you attempt to assign this value to your accId member variable.

Try including the inserted account in your redirect call:

public PageReference saveAccount() {
    insert account;
    Id redirectId = account.Id; // <-- store the inserted record's ID for the redirect
    account = new Account();
    PageReference newocp = new PageReference('/apex/MyCustomLookupController?accId='+redirectId);
    newocp.setRedirect(false);
    return newocp;
}

Update

As far as populating the Account lookup field on your first VF page - you're very close. On the page you're referencing <apex:inputField id="Account" value="{!sap.Account__c}" />, but you're never setting this sap.Account__c value. Simple way to do this would be in your constructor after the redirect returns to the first VF page:

public MyCustomLookupController(ApexPages.StandardController controller) {
    this.sap = (SAP_SD__c) controller.getRecord();
    accId = ApexPages.currentPage().getParameters().get('accId');
    // remove the account variable since it didn't appear to be used anywhere
    // you can add the query back if you need to reference some of its fields on the page
    if ( accId != null ) {
        this.sap.Account__c = (Id)accId;
    }
}

Now, just keep in mind that your SAP_SD__c record will not retain this change until you make an update call from somewhere else in your code. Since you're using an apex:inputField tag, I'm assuming this is desired and the update will be handled by some other button on the page.

  • thanks for reply JCD, I included this code, its working fine, but value is not populated in VF page1(Account lookup textbox) – Siva Ganesh Jul 22 '13 at 14:24
  • Are you populating the sap.Account__c field anywhere? It doesn't look like any of that code is included in this controller. – JCD Jul 22 '13 at 17:20
  • Hi JCD how to populate sap.Account__c fields,please share a code. – Udaya Jul 23 '13 at 3:13
  • @Udaya, please see my updated response. – JCD Jul 23 '13 at 12:12
  • thanks for reply JCD Its working fine but i add this VF page in custom button and run it in custom object,its shows an error System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: SAP_SD__c.Account__c – Udaya Jul 23 '13 at 12:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.