How to access a lookup field through SOQL in salesforce? - salesforce

3 Standard Objects: A, B, C
A has a field f1 lookup with B object.
B has a field f2 lookup with C object.
C has a field f3 which is a text.
How to access f3 field value through Object A?

Assuming A, B and C are standard objects, and f1, f2, f3 are standard fields; and all of them are API names, your SOQL query would look something like:
SELECT f1.f2.f3 FROM A
If A, B and C are custom objects, and f1, f2 and f3 are custom fields; and again all of them are API names, it would look like:
SELECT f1__r.f2__r.f3__c FROM A__c
Also remember to add some kind of WHERE or LIMIT clause to avoid hitting governor limits.
More info on SOQL relationships.

Related

SOLR field query skip records if..statement

I have a lot of products in solr, all of them with a field_code and a field_stock, and i want to exclude records like this, with a field query.
-field_code:(A OR B) OR (-(field_code:C AND field_stock:false) AND -(field_code:D AND field_stock:false))
so, all products with field_code either A or B
OR
all products with field_code C OR D and both not in stock, shoudl be excluded
everything else should be returned ?
Update:
I have added another field to the query, and it does not work.
i have field_code , field_stock and the new field_type
I need to remove all products from the query, that:
does not have: field_code = A OR B OR C OR D
and
field_stock = 0 (-field_stock:1)
AND
field_type = 'joe'
So something like:
SELECT * FROM table WHERE (field_code NOT IN (A,B,C,D) and field_type = 'joe' AND field_stock=0)
So that all records is returned, but the above ? Make any sense ?
When using a negative query you have to subtract the query from something - just having a negative query won't do anything by itself (if you ONLY have a single, negative query without any boolean operators Solr helpfully prefixes the set of all documents in front, since that's probably what you meant).
Something like this would probably match what you're describing:
(*:* -field_code:(A OR B)) OR
(*:* -(field_code:(C OR D) AND field_stock:false))
I'm assuming that field_code is multi-valued, since the second term after OR won't make sense otherwise.

Migrate Documents to Another Collection

I have a solr collections on the merger, we would like to ask the next question is as follows
I have two collections, c1 and c2,
C1 colleciton there are 10 data, id is from c1_0 to c1_9,
C2 colleciton also has 10 data, id is from c2_0 to c2_9,
I now want to c1 id c1_ format data into the c2, I implemented the following order, it seems no effect, and why?
I c1 designated in the new router.field=id
http://localhost:8081/solr/admin/collections?action=CREATE&name=c1&numShards=3&replicationFactor=3&maxShardsPerNode=3&collection.configName=myconf&router.field=id
I refer to https://cwiki.apache.org/confluence/display/solr/Collections+API#CollectionsAPI-api12
Solr version 6.3.0
I have a problem? Or understanding wrong?

How to sort a Doctrine DQL Query by the number or members of a relation?

I'm trying to create a query for retrieving objects from a Doctrine database, sorted by the number of members of a specific one-to-many relationship.
More specifically: I have two Entities: Person and Federation. A person can be a member of one federation (Person has 'federation' relationship), and a federation may have n people (Federation as 'people' relationship).
I would like to create a DQL Query that would return the list of Federations, ordered by how many people are members of that Federation. Something along these lines:
SELECT f FROM AcmeStatsBundle:Federation f ORDER BY [number of members of f.people]
That would be the first step. There is an additional second step, which I don't know if is possible to achieve with a single query, which would be filtering the members of the relation prior the counting. Like so:
SELECT f FROM AcmeStatsBundle:Federation f ORDER BY [number of (f.people p where p.attr = value)]
That second one would be the optimal result, but the first one satisfies my needs, if the second case is not feasibly in a single query.
Thanks in advance.
You could do something along the lines of:
public function getFederationsOrderedByNumberOfPeople()
{
return $this->createQueryBuilder('f')
->addSelect('COUNT(p) AS HIDDEN personCount');
->leftJoin('f.people', 'p');
->groupBy('f')
->orderBy('personCount', 'DESC');
}
The HIDDEN keyword was added in Doctrine 2.2, it means that the selected field won't be in the results, and in this case that means you just get your entities back instead of an array.
Reference to DQL SELECT documentation: http://doctrine-orm.readthedocs.org/en/latest/reference/dql-doctrine-query-language.html#dql-select-examples
There are 5 aggregate functions in DQL you can use (Doctrine 2.2): AVG, COUNT, MIN, MAX and SUM.
The following query should work:
SELECT f
FROM AcmeStatsBundle:Federation f
LEFT JOIN f.people p
GROUP BY f.id
ORDER BY COUNT(p)
WHERE p.attr = :some_value
For more DQL trickery I suggest you look up official Doctrine docs.

In Grails... How to union two tables in a single controller?

I have two tables, but they are installed as plugin in Grails.
Columns in T1 are: a1, b1, c1, d1
Columns in T2 are: a2, b2, c2, d2
I need to select columns a*, b*, c*, d* (=1,2) from both tables in a controller as union and sort all of them by the column d, how can I do that?
Furthermore, how can the pagination work as treating about result as a single table?
Pls help. Appreciate!!
Not sure if this is helpful or not. We hit a similar (though not exact) problem in the past, and we solved it by creating a view. You can create the view to do your union and select, and then create the new domain class that maps to the view.
You won't be able to use grails auto create for the table, which is another limitation.
On pagination:
groovy.sql.Sql rows() and eachRow() have 2nd and 3rd parameters max and offset that you can paginate with all like in ordinary list.
Retrieve total count with another SQL query or some other way. Construct a PagedResultList from a data page rows() and int totalCount - and you got an object that you can use as a model.

SQL unite fields to one result

I know this is a "not build in" or "the way dba thinks" but a programmer approach, how could one request from 3 fields to get the one that is not null, into a result field.
Let's say we have a table with f1,f2,f3,f4,f5.
Let's say f2,f3,f4 are the same type.
Let's say the content of the table be tuples of
(key1,null,null,value1,value2)
(key2,null,value3,value4,value5)
(key3,null,null,null,value6)
Now if we return the first tuple then we get (key1) we get (key1,value1,value2)
If we ask for key2 we get (key1,value3,value5)
If we ask for key3 we get (key1,null,value6)
How is it possible to get the fields in the priority of if you have value in f2, then its set into the returned field, only then if we have value in f3 then its set into the middle returned field, only then if we have value in f4 then its set into the middle returned field
The main goal is to get the result into a single field and prevent the overhead work needed at the result end.
As Bernd_k suggested, COALESCE is your friend here
SELECT [key], COALESCE(f2, f3, f4), f5
FROM YourTable
Select IsNull(IsNull(f1, f2), f3))

Resources