You need to sign in to do that
Don't have an account?
Trisha Hinojosa
SOQL: return first created record in object X for a list of users
Hello,
I have a custom object called "Matters". It's records are created by a product integration so I have to use a picklist field and user lookup field as a proxy for "created by". The picklist field is Salesperso__c and was replaced by a lookup field Salesperson__c at a later date.
I'm stuck trying to iterate through a list so I get the first matter for every user.
This is what I was able to put togehter so far:
SELECT Id, Name, Salesperso__c, Salesperson__c, CreatedDate FROM Matter__c order by createdDate ASC limit 1
This only gives me the first matter ever made. Not the first matter for each salesperson.
How can I incorporate something like this: list <user> fmatter = [select id from matter order by createdDate ASC limit 1];
Should I? Do I have to do two separate queries, because I have 2 different data types for the salesperson field?
New to SOQL.
Thank you!
EDIT: I also tried the following query below, but it is not allowed: "[object Object]: Ordered field must be grouped or aggregated: CreatedDate"
SELECT Id, Name, Salesperso__c, Salesperson__c, CreatedDate FROM Matter__c
GROUP BY Salesperson__c, Salesperso__c
ORDER BY createdDate ASC limit 1
...when I add CreatedDate to group by: "Malformed Query:
[object Object]: Salesperson__c, Salesperso__c, createdDate ORDER BY createdDate ^ ERROR at Row:1:Column:116 field 'createdDate' can not be grouped in a query call"
I know I'm probably not understanding something elemental. I apologize for my ignorance.
I have a custom object called "Matters". It's records are created by a product integration so I have to use a picklist field and user lookup field as a proxy for "created by". The picklist field is Salesperso__c and was replaced by a lookup field Salesperson__c at a later date.
I'm stuck trying to iterate through a list so I get the first matter for every user.
This is what I was able to put togehter so far:
SELECT Id, Name, Salesperso__c, Salesperson__c, CreatedDate FROM Matter__c order by createdDate ASC limit 1
This only gives me the first matter ever made. Not the first matter for each salesperson.
How can I incorporate something like this: list <user> fmatter = [select id from matter order by createdDate ASC limit 1];
Should I? Do I have to do two separate queries, because I have 2 different data types for the salesperson field?
New to SOQL.
Thank you!
EDIT: I also tried the following query below, but it is not allowed: "[object Object]: Ordered field must be grouped or aggregated: CreatedDate"
SELECT Id, Name, Salesperso__c, Salesperson__c, CreatedDate FROM Matter__c
GROUP BY Salesperson__c, Salesperso__c
ORDER BY createdDate ASC limit 1
...when I add CreatedDate to group by: "Malformed Query:
[object Object]: Salesperson__c, Salesperso__c, createdDate ORDER BY createdDate ^ ERROR at Row:1:Column:116 field 'createdDate' can not be grouped in a query call"
I know I'm probably not understanding something elemental. I apologize for my ignorance.
I get this error: "The query has to start with 'FIND' or 'SELECT'."
Tried the query below, but got a parsing error: "Unknown error parsing query"
SELECT min(CreatedDate), Salesperso__c, Salesperson__c, Id, Name
FROM Matter__c
GROUP BY Salesperso__c, Salesperson__c, Id, Name
HAVING Salesperso__c, Salesperson__c IS NOT NULL
LIMIT 1000