0

Here's the deal: I have a search field that searchs the object selected from the list, presenting the results below in a table. But in my next step, i want to add the lookup icon next to the text field opening a new windows to search and select (just like the Lookup API). I've already tried to change the inputText to inputField but it gives the error that "inputField can only be used with sObjects". I'm new to Salesforce and i've been trying find a sollution for hours already, so I need your help. Heres my page and my controller:

<apex:page standardController="Payment__c" extensions="PaymentExtension">
  <apex:form >
      <apex:pageBlock >
            <apex:pageBlockSection columns="1">
                  <apex:pageBlockSectionItem >
                        <apex:selectList value="{!searchCategory}" size="1">
                              <apex:inputText value="{!searchCategory}"/>
                              <apex:selectOption itemValue="Country__c" itemlabel="Country"/>
                              <apex:selectOption itemValue="City__c" itemlabel="City"/>
                              <apex:selectOption itemValue="Town__c" itemlabel="Town"/>
                              </apex:selectList>
                  </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom">
                  <apex:commandButton value="Search" action="{!find}" reRender="result"/>
            </apex:pageBlockButtons>
      </apex:pageBlock>

      <apex:pageBlock id="result" >
      <apex:pageBlockTable value="{!result}" var="res">
                  <apex:repeat value="{!objectFields}" var="field"> 
                        <apex:column value="{!res[field]}"/> 
                  </apex:repeat>
      </apex:pageBlockTable>       
      </apex:pageBlock>
</apex:form>
</apex:page>

Controller

public with sharing class PaymentExtension {
        public PaymentExtension(ApexPages.StandardController controller) {

        }

          public String searchKeyWord {get;set;}
          public String searchCategory {get;set;}
          public List<String> objectFields {get;set;}    
          public sObject sObjectToBind {get;set;}  
          public List<sObject> result {get;set;}


          public void Search()
          {
                searchCategory = 'Member__c';
          }

          public void find()
          {    
                /*    Getting fields for the sObject   */ 
            objectFields =  new List<String>();  
            Map<String , Schema.SObjectType> globalDescription = Schema.getGlobalDescribe();   
            Schema.sObjectType sObjType = globalDescription.get(searchCategory);             
            sObjectToBind = sObjType.newSObject();  
            Schema.DescribeSObjectResult r1 = sObjType.getDescribe();  

            Map<String , Schema.SObjectField> mapFieldList = r1.fields.getMap();  
            for(Schema.SObjectField field : mapFieldList.values())  
            {  
                Schema.DescribeFieldResult fieldResult = field.getDescribe();  
                if(fieldResult.isAccessible())  
                {  
                    objectFields.add(fieldResult.getName());
                }  
            }  

            /*    Building Query with the fields    */
            /*Integer i = 0;
            String fieldsToFetch = '';
            for(String temp:objectFields)
            {       
                Integer len = objectFields.size();
                if(i==len-1)
                {
                      fieldsToFetch = fieldsToFetch + temp;
                }
                else
                {
                      fieldsToFetch = fieldsToFetch + temp + ',';
                }
                i++;
            }
                String sql = 'Select ' + fieldsToFetch + ' From ' + searchCategory + ' WHERE Name LIKE \'%' + searchKeyword + '%\'';
                result = Database.Query(sql);*/


                String fieldsToFetch = '';
                if (searchCategory == 'City__c') {
                    ObjectFields.clear();
                    ObjectFields.add('Name');
                    ObjectFields.add('Description__c');
                    ObjectFields.add('Country__c');
                    fieldsToFetch = 'Name,Description__c,Country__c';
                    String sql = 'Select ' + fieldsToFetch + ' From ' + searchCategory + ' WHERE Name LIKE \'%' + searchKeyword + '%\'';
                    system.debug(sql);
                    result = Database.Query(sql);
                    }

                    system.debug(fieldsToFetch);
                    system.debug(searchCategory);
                    system.debug(searchKeyword);
                    system.debug(ObjectFields);

          }

          /*    Getting and Setting values for SearchCategory radio button    */
          public String getsearchCategory()
          {
                return searchCategory;
          }

          public void setsearchCategory(String searchCategory)
          {
                this.searchCategory = searchCategory;
          }

    }

The code is only implement to function with one objects, its only for example (City__c in this example).

EDIT: So, I have something like this:

Select List

I need this (the red square lookup button)

Lookup button

To open something like this

Lookup window

And populate the search field with the letters selected in the select list in the first image. I already tried to use the inputField but the salesforce gives me a error that says "inputField needs a sObject", and i'm out of ideas.

I already tried to implement a the new lookup window with the textField, but i failed, I'm out of ideas and can't find any useful content with Google. Thanks in advance!

1
  • your question can be interpreted in multiple ways (at least from the fact that nobody reacted, I am guessing it is difficult to understand). In order to get it clear, is it possible to post an example of how you intend the screen to look like? Commented Oct 16, 2014 at 15:57

1 Answer 1

0

You can either program this from scratch (visualforce) or use the standard functionality in a smart way. I'll focus in the latter as you are aiming for that one (and it's less complex as well).

In order to use the standard lookup function, you need to use a 'real' lookup field to that object. It doesn't have to be a real record in the database, but you can reuse an in-memory record of an object that has the particular lookup.

In this particular case, it looks like you will be requiring lookups to multiple different objects (country, city town). That means that you will require multiple 'dummy' fields. Unless you have a ready-made object that has all the 3 lookup fields, you have to create a dummy custom object that you can use for this (and also for similar future requirements). Let's call it Dummy_Object__c.

Create the Dummy_Object___c and on that object create the 3 lookups Country__c, City__c and Town__c. In your code, create an instance of the Dummy_Object__c

public PaymentExtension(ApexPages.StandardController controller) {
   Dummy_Object__c dummy = new Dummy_Object__c();
}

Then on your page, you will have to include multiple fields at the spot of the 'searchCategory' inputtext, and only render the one that corresponds with the selected item. You will have to include an actionsupport inside the selectlist to rerender the searchCategory field. You might want to switch the field order to make it more intuitive for the user. Something like this:

        <apex:pageBlockSection columns="1" >
              <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Select search category" />
                    <apexoutputPanel id="searchPanel>
                      <apex:selectList value="{!searchCategory}" size="1">
                          <apex:selectOption itemValue="Country__c" itemlabel="Country"/>
                          <apex:selectOption itemValue="City__c" itemlabel="City"/>
                          <apex:selectOption itemValue="Town__c" itemlabel="Town"/>
                          <apex:actionSupport event="onchange" action="{!null}" rerender="searchPanel"/>
                      </apex:selectList>
                      <apex:inputField value="{!dummy.Country__c}" rendered="{!searchCategory='Country}" />
                      <apex:inputField value="{!dummy.City__c}" rendered="{!searchCategory='City}" />
                      <apex:inputField value="{!dummy.Town__c}" rendered="{!searchCategory='Town}" />
                    </apexoutputPanel>
              </apex:pageBlockSectionItem>
        </apex:pageBlockSection>

This will result in a lookup being displayed, including the corresponding popup. Whatever is selected in the popup is returned back into the field on the dummy object, so in your controller you can use it for e.g. loading more details.

I am not sure if this completely fits your requirements, but this is what you can do with standard lookups. The alternative, as said, is to build something from scratch using VisualForce.

7
  • When I try to create the custom object (like PaymentObject__c var = new PaymentObject__c(); ) it gives me the error "Error: Compile Error: Invalid type: PaymentObject__c at line 4 column 35" . Ins't suppose it creates a new custom object?
    – danielpm
    Commented Oct 17, 2014 at 10:54
  • No you need to create the actual custom object via the GUI (including the lookup fields). After that, you can create an instance ('var') in your code. But you don't have to save it to the database. There's no other way to get that lookup behaviour. Commented Oct 17, 2014 at 10:59
  • I think i'm not understanding right (sorry, I'm working with apex since 2 days). Can you do a 'dummy version' of your last comment? Thanks for the effort
    – danielpm
    Commented Oct 17, 2014 at 11:07
  • Go to Setup -> Create -> Objects and create a new object named e.g. PaymentObject__c. Then create 3 lookup fields on that object (to the Country, City, Town objects). After that, the statement PaymentObject__c var = new PaymentObject__c(); will work. Commented Oct 17, 2014 at 11:17
  • BTW this is quite an advanced scenario for your 3rd day of APEX programming.. Commented Oct 17, 2014 at 11:18

You must log in to answer this question.

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