I'm trying to execute a dynamic soql query using variable objects.
In my visualforce page i have tow apex:selectlist, the first one contains a list of objects, when i select one object from this list, i refresh the second list to display selected object's fields.
The apex:inputText contains text to search in selected field.
visual force code:
<apex:selectList id="listObjects" value="{!selectedObject}" size="1">
<apex:selectOptions value="{!allObjetcs}"></apex:selectOptions>
</apex:SelectList>
<apex:selectList id="listFields" value="{!selectedField}" size="1">
<apex:selectOptions value="{!allFields}"></apex:selectOptions>
</apex:SelectList>
<label>Text to search : </label><apex:inputText id="textResearch" value="{!textResearch}" />
<button id="searchButton" type="button">{!$Label.SEARCH}</button>
apex code :
public void search() {
result = new List<SearchWrapper>();
System.debug('>>>>>> ALK - in search ');
String query = 'Select Id, ' + selectedField + ' from ' + selectedObject + ' where ' + selectedField + ' like \'%' + textResearch + '\'%';
System.debug('>>>>>> ALK - Query : ' + query);
List<sObject> = (sObject) Database.query(query);
}
Please how can i cast Database.query(query) and how can i execute this dynamic query.
Thanks for all.
if you are using a local variable in your query you must put ':' before that. like this:
String value = 'test';
List<something> result = [select id from something where name = :value];
Try below code. If field list if multiselect, you might have to iterate over the selectedField variable and form a string of field names separated by comma.
String query = 'select '+String.valueOf(selectedField)+' From '+selectedObject+' WHERE Name = \'' + textResearch+'\'';
List<sObject> recordList = Database.query(query);
public void search() {
result = new List<SearchWrapper>();
String query = 'Select ' + selectedField + ' from ' + selectedObject + ' where ' + selectedField + ' like \'%' + textResearch + '%\'';
System.debug('>>>>>> Query : ' + query);
List<sObject> l = Database.query(query);
}
Related
I would like to know if there is possibility of writing a SOQL similar like we do in SQL
like Distinct of Level_1__c,Level_2__c and Level_3__c
Currently SOQL is
select Case__c, Level_1__c, Level_2__c,Level_3__c FROM ERT_Case_Type__c
How to rewrite to include Distinct of Level_1__c,Level_2__c and Level_3__c
Thanks & Regards,
Carolyn
I think for your use case, you will need Apex. Something like the following:
Map<String, Set<String>> distinctLevels = new Map<String, Set<String>>{
'Level_1__c' => new Set<String>(),
'Level_2__c' => new Set<String>(),
'Level_3__c' => new Set<String>()
};
Set<String> levelApiNames = new Set<String>{'Level_1__c', 'Level_2__c', 'Level_3__c'};
for(ERT_Case_Type__c caseType : [SELECT Case__c, Level_1__c, Level_2__c, Level_3__c FROM ERT_Case_Type__c]){
for(String level : levelApiNames){
distinctLevels.get(level).add(caseType.get(level));
}
}
for(String level : levelApiNames){
System.debug('Number of distinct ' + level + ' values: ' + String.valueOf(distinctLevels.get(level).size());
//System.debug('List of distinct ' + level + ' values: ' + distinctLevels.get(level).toString());
//System.debug(distinctLevels.get(level).size()); //as integer
}
Loop over query results
Loop over each field API name you want to track
Add the values for each field to the set inside the map. The set will only have unique values
Do something with the map values for each field API name
This is not tested, but the high-level outline should work for you.
I am trying to populate a column that is to represent an Internal URL that directs the user to a specific document. The document ID, DOC_ID, is already populated in the Table. I would like to populate the column by prepending a base URL to the Document ID. I do not know how to to do this dynamically and am getting some errors referring to the parameters of CONCAT(), issue being it is understanding it to be a string rather than the column value. My code is as follows:
SET #Insert_DRS_URL_SQL = 'UPDATE'
+ ' '
+ QUOTENAME(#TBL)
+ ' '
+ 'SET DRS_URL = CONCAT(#URLBASE'
+ ','
+ 'DOC_ID'
+ ')'
EXECUTE sp_executesql #Insert_DRS_URL_SQL;
UPDATE
#URLBASE = 'http://blah/blah/blah/id='
DRS_URL is the column I wish to populate.
Basically I need DRS_URL = #BASEURL + DOC_ID
my HTML code for drop down like follow:
<select ng-model="selected_value"
ng-options="(x.version + ' ' + ' : ' + x.status) for x in allData"></select>
my Angular code like follow :
data comes from a rest service
$http.get(......).success(function (data, status){
$scope.allData = data;
$scope.selected_value = $scope.allData[0].version + " : " + $scope.allData[0].status;
});
Data comes to the drop down. but initially drop down does not display the value. It shows if only select the value. Can someone help on this issue.
Add ng-selected="selected_value" to the select element.
An example of what you are trying to achieve can be found in the ngSelected documentation.
I try to dynamically create some LINQ query for searching data in all columns.
Currently, I use the following code to build dynamic LINQ query. However, it's quite buggy when deals with complex column.
var type = property[col.ToLower()].PropertyType;
var isNullableType = type.IsGenericType && type.GetGenericTypeDefinition() == typeof (Nullable<>);
if (type.IsValueType && !isNullableType)
{
filter += col + ".ToString().ToLower().Contains(#" + i + ".ToLower())";
}
else if (isNullableType)
{
filter += col + ".HasValue ? " + col + ".Value.ToString().ToLower().Contains(#" + i + ".ToLower())" + " : false";
}
else
{
filter += "(" + col + " != null ? " + col + " : \"\").ToString().ToLower().Contains(#" + i + ".ToLower())";
}
Do you have any idea to simplify my above code? I'm OK if your suggestion is work only for SQL Server 2008 or later.
Note:
Column data can be any type like integer, string, object, enum and it can be null.
I have an object with number of records in this format with Name in Text format -
Id Name
1 A,B,C
2 A,B
3 A
4 B
5 A,C
6 A,D
I have a multi-select picklist with values as:
A
B
C
D
So if I select B and C in my picklist, then I must have the records from the table in which name consists of B or C or (B and C) i.e. in this case, it should show 4 records:
1 A,B,C
2 A,B
4 B
5 A,C
I am using SQL query with IN and INCLUDES keyword but I am unable to achieve this functionality.
This depends on how many options you have for using soql. One way I can see it is:
Object__c myObject = new Object__c();
object.Name = 'A,B,D';
object.Multi_Select_Name__c = 'A;E';
insert object;
String query = 'select Id from Object__c';
//now we want to do a multi picklist, which is a String separated by ';'
List<String> nameSelections = object.Multi_Select_Name__c.split(';');
if(nameSelections.size() > 0) {
query += ' where ';
for(Integer i = 0; i < nameSelections.size(); i++) {
nameSelections[i] = 'Name like \'%' + nameSelections[i] + '%\'';
}
query += String.join(nameSelections, ' or ');
}
List<Object__c> objects = (List<Object__c)database.query(query);
This will create a query like:
'select Id from Object__c where Name like '%A%' or Name like '%E%'
Selecting all Object__c's where Name contains either A or E, generated from your multiselect.
Syntax errors may apply, I just wrote this real quick :) Ill double check myself.
EDIT:
This code works to build the query
String query = 'select Id from Object__c';
String multiSelect = 'A;E';
List<String> nameSelections = multiSelect.split(';');
if(nameSelections.size() > 0 ){
query += ' where ';
for(Integer i = 0; i < nameSelections.size(); i++) {
nameSelections[i] = 'Name like \'%' + nameSelections[i] + '%\'';
}
query += String.join(nameSelections, ' or ');
}
system.debug(LoggingLevel.INFO, query);