You need to sign in to do that
Don't have an account?
Subodh Shukla
How to search a string in selected object and its selected field??
Hi all,
I have a vf page where i have 2 selectlist.
1.Shows all object
2.shows the field of selected object.
i want to search a given string in selected object selected field how can i achieve these any suggestion plz.
Apex class:
I have a vf page where i have 2 selectlist.
1.Shows all object
2.shows the field of selected object.
i want to search a given string in selected object selected field how can i achieve these any suggestion plz.
Apex class:
public class objectController { public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe(); public String selectedObject {get; set;} public String selectedField {get; set;} public String newValue {get; set;} public String searchValue{get;set;} Public objectController() { selectedObject = 'account'; } public List<SelectOption> getObjectNames() { List<SelectOption> objNames = new List<SelectOption>(); List<String> entities = new List<String>(schemaMap.keySet()); entities.sort(); for(String name : entities) { objNames.add(new SelectOption(name,name)); } return objNames; } public List<SelectOption> getObjectFields() { Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe(); Schema.SObjectType ObjectSchema = schemaMap.get(selectedObject); Map<String, Schema.SObjectField> fieldMap = ObjectSchema.getDescribe().fields.getMap(); List<SelectOption> fieldNames = new List<SelectOption>(); for (String fieldName: fieldMap.keySet()) { fieldNames.add(new SelectOption(fieldName,fieldName)); //fieldMap.get(fieldName).getDescribe().getLabel();//It provides to get the object fields label. } return fieldNames; } public PageReference submit() { String temp = selectedObject + ' ' + selectedField; DetailTest__c omc = new DetailTest__c(); omc.Obj_field__c= temp; omc.New_Value__c= newValue ; insert omc; return null; } public void search(){ /* String queryStr='select '+ id + 'from ' + selectedObject; List<Sobject> results=Database.query(queryStr); */ } }Visualforce page:
<apex:page showHeader="false" standardStylesheets="false" sidebar="false" applyHtmlTag="false" applyBodyTag="false" docType="html-5.0" controller="objectController"> <apex:form > <html xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <head> <apex:stylesheet value="{!URLFOR($Resource.SLDS103, 'assets/styles/salesforce-lightning-design-system-vf.css')}" /> </head> <div class="slds"> <form class="slds-form--inline"> <div class="slds-form-element"> <div class="slds-form-element__control"> <label class="slds-form-element__label slds-m-left--xx-large" for="select-01">Select Object</label> <apex:actionRegion > <apex:selectList value="{!selectedObject}" size="1"> <apex:selectOptions value="{!ObjectNames}"/> <apex:actionSupport event="onchange" rerender="myFields"/> </apex:selectList> </apex:actionRegion> <label class="slds-form-element__label slds-m-left--xx-large" for="select-01">Select Field</label> <apex:actionRegion > <apex:outputPanel id="myFields"> <apex:selectList value="{!selectedField}" size="1"> <apex:selectOptions value="{!ObjectFields}"/> </apex:selectList> </apex:outputPanel> </apex:actionRegion> <label class="slds-form-element__label slds-m-left--xx-large" for="text-input-01">Input Value</label> <apex:inputText label="New Value" value="{!newValue }"/> </div> <label class="slds-form-element__label slds-m-left--xx-large" for="text-input-01">Search Value</label> <apex:inputText label="New Value" value="{!SearchValue }"/> </div> <div class="slds-button slds-m-left--xx-large"> <apex:commandButton value="Submit" action="{!submit}"/> </div> </form> </div> </html> </apex:form> </apex:page>
I'm not sure is this is what you are looking for but the way I'll resolve this is with an actionFunction on the visualforce which calls the controller to get the result from your search and show it in a label on the visualforce:
Visualforce Page:
Controller class:
I'll also recommend to use SOSL instead of SOQL, here a link "Dynamic SQOL (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dynamic_sosl.htm)",
Hope this helps you.
Emmanuel Cruz
All Answers
I'm not sure is this is what you are looking for but the way I'll resolve this is with an actionFunction on the visualforce which calls the controller to get the result from your search and show it in a label on the visualforce:
Visualforce Page:
Controller class:
I'll also recommend to use SOSL instead of SOQL, here a link "Dynamic SQOL (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dynamic_sosl.htm)",
Hope this helps you.
Emmanuel Cruz
Thanks for reply its really helpfull.
how can i show all records in a table.
Add an output panel after the output label with the number of results: Change your search method as the next code: Finally, add the method to create the components dynamically: This will show all records in a table. For more information about the dynamic method that I did, please refer to the visualforce guide: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_dynamic_vf_components_implementation.htm (https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_dynamic_vf_components_implementation.htm" target="_blank)
Regards