Salesforce(apex) Query select with Where toLowerCase - salesforce

I using Salesforce (apex), I need to Query with a WHERE statement with toLowerCase ,i.e. I have a list (MyNames_list) with LowerCase values but the table values are not lowerCase.
In my Query when I check the Name in MyNames_list it will make toLowerCase value and then check it in the MyNames_list.
Something like this:
Name | LastName
BOBY | Testovich1
DANY | Testovich2
Ron | Testovich3
and in my list:
MyNames_list :boby,dany,ron
That still will return me all rows, because WHERE statement will check it with LowerCase.
//only example (not working code)
for(Users user:[SELECT Name,LastName FROM Users
WHERE ( Name.toLowerCase() IN : MyNames_list) ] )
{
//code....
}
Is there a way to do this in Salesforce (apex) Query ?

Salesforce is only case-sensitive for fields that are explicitly case-sensitive (ie an external id that has been marked as case-sensitive)
The field you are querying, Name is not case-sensitive - hence, you don't need to apply toLowerCase(). You will get all matching fields anyway.
So your query could be:
for(User u:[SELECT Name,LastName FROM User WHERE Name IN :MyNames_list)]){
//code....
}

Related

Apply query criteria based on parameters

I need to run a query in a MS Access Database providing some parameters from a form. Imagine the next example:
I have a form which contains:
CheckBox1 and Text1
CheckBox2 and Text2
Button (to run query)
Now imagine a query with two fields: ID, NAME.
I want to filter ID by Text1 only when CheckBox1 is enabled. If not, I want the query not to filter ID in any way (as if the 'query' input was empty).
In the same way, I want to filter NAME by Text2 only when CheckBox2 is enabled. If not, I want the query not to filter NAME in any way (just like ID before).
I've tried so many things for a couple of days and have sniffed tons of internet pages and still don't come up with a solution.
You can use a SQL query such as the following:
select * from YourTable t
where
([Forms]![YourForm]![CheckBox1] = False or t.ID = [Forms]![YourForm]![Text1]) and
([Forms]![YourForm]![CheckBox2] = False or t.NAME = [Forms]![YourForm]![Text2])
(Change YourTable to the name of your table and YourForm to the name of your form; t is merely an alias so that you only have to change the table name in one place in the code).

Search for a value in two columns in two different tables with SQL?

I want a code to search for a value in two columns in two different SQL tables ,
a = raw_input('Enter name here')
cur.execute('SELECT phone FROM participants')
b = cur.fetchall()
if a in b:
print "The name is already exist"
Here I searched in on table (participants). What should I do to search in two tables?
Assuming you are asking for SQL statement, you need to just use select from two tables instead of one. To keep everything in single statement, you can use UNION as described eg. here: https://www.w3schools.com/sql/sql_union.asp
Let's assume you have second table, named friends with phone field also there.
Then, your SQL will be like this:
SELECT phone FROM participants where name = <input name here>
UNION
SELECT phone FROM friends where name = <input name here>
You can add sort at the end, if this is relevant for your case.
This you can also search more than one column in each table, by adding "or" clause, like this:
SELECT phone FROM participants where name = <your input here> or lastname = <your input here>
UNION
SELECT phone FROM friends where name = <your input here> or lastname = <your input here>
Of course, you have to replace with proper search string.
BTW, the code you provided is not searching anything - it is just dumping all phones from the table into "b" variable, which may be very inefficient especially when the table grows large. I strongly suggest to search via SQL and then present your output from script.

neo4j cypher loop to return two matches for each collection element

I want to return for each keyword in collection two most related users to keyword. Query below works fine with only one keyword.
WITH
'meeting' AS keyword
MATCH
(k:keywords)<-[r1:RELATED]-(u:users)
WHERE
k.keyword = keyword
AND NOT u:deactivated
AND NOT u.userid1 = 'administrator'
AND NOT k:deleted
AND r1.datedeleted = 0
AND r1.datedeleted IS NOT NULL
WITH
DISTINCT u.userid1 AS user,
r1.weight AS rel
ORDER BY
rel DESC
LIMIT 2
RETURN
user
LIMIT 20
How to make a loop to the same as query above for each collection element? (e.g. collection below)
WITH
['meeting', 'new', 'tool', 'training', 'it', 'process', 'server'] AS kw_my_channels
More precisely, I need a cypher loop to do my query for each collection element (keywords) returned from a query done before (here manually defined as kw_my_channels).
The issue is to implement a "for" loop, which execute a MATCH for every collection element. In this case the elements are keywords which are related to users. The relation has weights (relevance of the keyword for this user). This weights show how often a keyword was used by the user (count). For every keyword, two users with the highest weights should be found.
An example output should look like:
Keyword | Users | Weight
-------------------------------
meeting | user1, user2 | 80, 75
new | user1, user4 | 40, 34
tool | user5, user8 | 33, 22
You could try:
MATCH (k:keywords)<-[r1:RELATED]-(u:users)
WHERE k.keyword IN ['meeting', 'new', 'tool', 'training', 'it', 'process', 'server']
AND ...
Etc.
UPDATE
Could you do something like this?
WITH
'meeting' AS keyword
MATCH
(k:keywords)<-[r1:RELATED]-(u:users)
WHERE
k.keyword = keyword
AND NOT u:deactivated
AND NOT u.userid1 = 'administrator'
AND NOT k:deleted
AND r1.datedeleted = 0
AND r1.datedeleted IS NOT NULL
WITH
DISTINCT u.userid1 AS user,
r1.weight AS rel
ORDER BY
u.userid1 DESC, rel DESC
RETURN
user
LIMIT <# of distinct users * 2>
Note the removal of the "LIMIT 2", the changing of the "ORDER BY", and the "LIMIT" change at the bottom. Of course, this assumes that each keyword has at least two distinct users attached to it. That might be a non-starter for you.
Are you able to programmatically/dynamically insert a value for <# of distinct users * 2> into the query? It'd be nice if you could use something like "LIMIT COUNT(user) * 2" but that's invalid in Cypher.
Unfortunately, I don't think Cypher currently allows you to use FOREACH for anything but mutating operations.
Hopefully this is of some use to you.

Select all names where none of that names entries are approved

I have a table that contains an name field and an approved field. However multiple entries can have the same name. I have been trying to write a query that finds all unique names and decides if it has no approved entries
I feel like I need to use a join but I can only really join it with itself. Is there a function that does this that I'm just not finding? Or do i need to use some weird kind of join to make this work.
define variable isApproved as logical no-undo.
for each customer no-lock break by name:
if first-of( name ) then isApproved = no.
if approved then isApproved = yes.
if last-of( name ) and isApproved = no then display name.
end.
I had a problem exactly like yours about 6 months ago. I've used a solution like this. Hope it helps.
DEF TEMP-TABLE tt-name NO-UNDO
FIELD name AS CHAR
FIELD approved AS LOG
INDEX ch-unique IS PRIMARY UNIQUE
name.
/* Create a list of unique customers */
FOR EACH customer FIELDS(name) NO-LOCK:
IF NOT CAN-FIND(FIRST tt-name
WHERE tt-name.name = customer.name) THEN DO:
CREATE tt-name.
ASSIGN tt-name.name = customer.name.
END.
END.
/* Look for at least one customer who matches name and not approved */
FOR EACH tt-name:
IF CAN-FIND(FIRST customer NO-LOCK
WHERE customer.name = tt-name.name
AND NOT customer.approved) THEN
ASSIGN tt-name.approved = NO.
ELSE
ASSIGN tt-name.approved = YES.
END.
/* Display the results */
FOR EACH tt-name
WHERE NOT tt-name.approved:
DISP tt-name WITH WIDTH 333 NO-ERROR.
END.

SOLR - How to handle AND clause between 2 multivalue fields

How would we use AND clause in the SOLR query between 2 fields those are multiple value fields?
For example; following is the picture of database table
JobseekerID CompanyID Email Sent On
=========== =========== ==============
123 ABC 22-Jul-2011
123 XYZ 01-Jul-2011
Now in Sql, if i query
Where CompanyID = "ABC" AND EmailSentOn > 14-Jul-2011 (It will return TRUE)
Where CompanyID = "XYZ" AND EmailSentOn > 14-Jul-2011 (It will return FALSE)
How would we handle it in SOLR? Following is the picture of my SOLR Collection, how i am storing the data
{int name="jobseekerid"}123{/int}
.....................
.....................
.....................
{arr name="CompanyID"}
{str}ABC{/str}
{str}XYZ{/str}
{/arr}
{arr name="EmailSentOn"}
{str}2011-07-22T17:44:00Z{/str}
{str}2011-07-01T05:10:00Z{/str}
{/arr}
How can i query in the SOLR Now for Jobseeker 123. Because for SOLR there is not any relation between CompanyID and EmailSentON. It takes them two separate entities.
When we will query Company ID is "ABC" and EmailSentOn >= 14-July-2011 it will return TRUE and when we will say CompanyID is "XYZ" and EmailSentOn >= 14-July-2011 it will again return TRUE because SOLR will see, does company ID "XYZ" exist and any date in the 'email sent on' is greater than 14-July then it will return true.
But we need it to check if the CompanyID "XYZ" then the company XYZ date is greater than 14-july or not. Which is not, because XYZ sent email date is "01-Jul". So it should return false for that.
I hope you get my point. Can anyone please guide me how would we handle this in SOLR?
The problem here is with the definition of the schema - you don't actually have multiValued data! Instead, you should just have two separate indexed documents with single values for CompanyId and EmailSentOn. Then when you search by date you get back just the contents of one document or another and don't have the results jumbled as you do now.

Resources