System.sobjectexception: sobject row was retrieved via soql without querying the requested field: Asset.Product2 - salesforce

I have a problem on the query on my test class. I have put AssetId__r.Product2Id on the 1st query, and put Product2Id in the Asset query. but still the error persist. as the error says it needs Asset.Product2 which is an SObject , not a field and I am lost on how to fix this error. does anyone encounter this error before? please I need your help.

would be easier if you'd post the actual query.
You probably have something like that in your code:
String name = myObject.AssetId__r.Product2.Name;
but the query SELECT AssetId__r.Product2Id, ... FROM MyTable WHERE ...
So what you can change is put more product fields in the query. You can go up to 5 dots "up".
SELECT AssetId__r.Product2.Id,
AssetId__r.Product2.Name,
AssetId__r.Product2.ProductCode
FROM myObject
WHERE ...
It will work and then Apex that uses it can treat AssetId__r.Product2 as a normal Product2 object, as if it was queried separately. It'll have Id, Name, ProductCode fields set.
The query is also "safe". If AssetId__c is null or Product2Id is null - it'll still execute OK. You'll have to do null checks in Apex (or in query's WHERE clause)

Related

Access a field from another table using the object relation

I am new to SalesForce and SOQL so sorry in advance if the question has already been answered, if yes link it to me.
The aim of my SOQL query is to get all the contract information to generate PDF.
There are tables: Contract, Contact and Account
In the Contract table there are fields: Maitre_d_apprentissage__c, MaitreApprentissageNom1__c, MaitreApprentissagePrenom1__c, Apprenti__c, ApprentiNom__c, ApprentiPrenom__c
There are relationships:
Apprenti__r which link Apprenti__c to Contact table
Maitre_d_apprentissage__r which link Maitre_d_apprentissage__c to Contact table
When I looked at table, I saw that MaitreApprentissageNom1__c was equal to Maitre_d_apprentissage__r.LastName and ApprentiNom__c was equal to Apprenti__r.LastName. So I conclude I could get other information of Apprenti__c and Maitre_d_apprentissage__c from the Contact Table following the same principle. So I added to my query Apprenti__r.Date_de_naissance__c and Maitre_d_apprentissage__r.Date_de_naissance__c to get the Date_de_naissance__c field which is in my Contact table.
I see in the results that the query succeeds in getting the information but some values have changed column (lines 6 and 7), you can see the difference between query 1 and query 2. In the first query I only return the Apprenti__r.Date_de_naissance__c and in the second query I return Apprenti__r.Date_de_naissance__c and Maitre_d_apprentissage__r.Date_de_naissance__c
Query 1:
SELECT ApprentiNom__c, ApprentiPrenom__c, Apprenti__r.Date_de_naissance__c, MaitreApprentissageNom1__c, MaitreApprentissagePrenom1__c
FROM Contract
Result 1:
Query 2:
SELECT ApprentiNom__c, ApprentiPrenom__c, Apprenti__r.Date_de_naissance__c, MaitreApprentissageNom1__c, MaitreApprentissagePrenom1__c, Maitre_d_apprentissage__r.Date_de_naissance__c
FROM Contract
Result 2:
I would like to understand from where is coming the problem and how to correct it. Thank you in advance.
It's possible that it's just your query editor displaying stuff incorrectly. You can see it got confused with 2 lookups to Contact table, why there's even a column header "Contact.Date_de_naissance__c" (and why it's there twice). And they aren't shown in the order you requested...
What editor you're using? You could try built-in "Developer Console" or http://workbench.developerforce.com/
What do you need it for? In Apex order of fields won't matter, in REST API query the values fetched via lookup will come as JSON sub-objects so there will always be a way to figure out exactly which value is coming from which relation.
In Dev Console try to run this and check if it solves your fears:
System.debug(JSON.serializePretty([SELECT
ApprentiNom__c, ApprentiPrenom__c,
Apprenti__r.Date_de_naissance__c,
MaitreApprentissageNom1__c, MaitreApprentissagePrenom1__c,
Maitre_d_apprentissage__r.Date_de_naissance__c
FROM Contract]));
Then add Maitre_d_apprentissage__r.LastName to query and see what changed, what stayed as is.

Selecting custom fields of "who" object using SOQL gives no such column error

I am looking for select custom fields of "who" object using SOQL in slaesforce:
As I am testing following SOQL, its working fine.
Select t.Who.FirstName, t.Status From Task t Where t.ActivityDate = TODAY AND t.Who.Type = 'Lead'
but while add any custom field of lead object(who object here) in select statement, as mentioned following query, its give error as
No such column 'LeadExtraInfo__c' on entity 'Name'.
even LeadExtraInfo field exist into Lead object
Select t.Who.FirstName, t.Who.LeadExtraInfo__c t.ActivityDate From Task t Where t.ActivityDate = YESTERDAY AND t.Who.Type = 'Lead'
The Who and What relationships on Task are not normal relationships -- they are polymorphic. This means that they can refer to different types of objects.
In the case of Who, it can refer to a Lead or a Contact. As such, you can only get to a limited subset of the fields in the relationship, namely the ones mentioned in this doc page.
To get to other fields on Lead, you'll have to query the Task first to get its WhoId, then query Lead where the Id equals that WhoId value to get those other fields.

Salesforce SOQL REST Query with empty fields

If I make an SOQL query such as:
select Id, Username, UserRoleId from User
The API correctly returns the results as XML, however because none of the rows contain a UserRoleId entry this node is completely excluded from the results.
Ideally I would like to be able to create a table from the resultant XML showing all the requested columns, even if they are empty. Is there a way of forcing the API to return even empty columns so that I can do this without resorting to something messy like parsing the SOQL query?
Isn't it a better idea to when you receive an result. Check if the curtain column row is empty. When the column is empty, than you know you have set a empty column by yourself. It is just an idea of my.
Think about a try catch, just an example I use in my application:
try {
parentId = records.getJSONObject(i).getJSONObject("Parent).getString("Name");
catch (JSONException e) {
parentId = "a";
}
When the column name from Parent is empty, then parentId = a. I'd set it manually because I know some other data rolls could have that data.

Ordering and Limiting A Subquery In Salesforce SOQL

I am attempting to retrieve the owner of a case, based on a partial match, where we choose the most recent case that matches the partial match.
This is the query I am attempting:
SELECT User.CustomField__c
FROM User
WHERE User.Id IN (
SELECT OwnerId
FROM Case
WHERE Case.CaseNumber LIKE '%1026'
ORDER BY Case.CreatedDate DESC LIMIT 1)
The following query works on its own, but doesn't seem happy as part of the subquery:
SELECT OwnerId
FROM Case
WHERE Case.CaseNumber LIKE '%1026'
ORDER BY Case.CreatedDate DESC LIMIT 1
Equally, if I drop the ORDER BY and LIMIT it works:
SELECT User.NVMContactWorld__NVM_Agent_Id__c
FROM User
WHERE User.Id IN (
SELECT OwnerId FROM Case
WHERE Case.CaseNumber LIKE '%1026')
Are order / limit queries not allowed in a SOQL subquery?
Just to clarify this issue, the scenario I am dealing with looks like this...
A Salesforce organisation can configure the "display format" for Case Numbers. If they select "4" digits, you get case numbers like:
0001
0125
1234
33456
It is possible to reconfigure your case numbers to get the following case numbers as well as the case numbers above...
000001
001234
033456
I didn't want people to be confused by the LIKE statement, the issue is that 001234 and 1234 are different cases, so if a customer supplies 1234 and I find two records, I want to start off assuming that they are the most recent case.
So either consider the LIKE statement or an IN statement that contains ('001234', '1234')
There is no documentation I could find that specifies that LIMIT and/or ORDER BY do not work with subqueries, but I ran into the same error you mentioned.
However, it may work to start at the Case object and look up to the User, similar to the Lookup Relationships and Outer Joins section in the SOQL documentation. I'm not sure if this would work for you, but it's something you may want to try.
Here's an example:
-- Edit --
SELECT OwnerId, Owner.CustomField__c
FROM Case WHERE
CaseNumber LIKE '%1026'
ORDER BY CreatedDate DESC
LIMIT 1
Turns out Custom Fields are not accessible because OwnerId is a polymorphic key referencing either Group or User. That means the above won't work, sorry.
To work-around this is very complicated. You would have to create a custom lookup field called "User Owner", or something. That would store a lookup reference to the User, if the Owner is a User (this can be checked by comparing the beginning of OwnerId to '005', the User ID prefix). That field would need to be populated using a after insert, after update Trigger. All values for this new field would need to be dataloaded for previously existing Cases. But, once you have this new "User Owner" field, you can access custom fields on User through SOQL, using it.
ORDER BY and LIMIT don't make sense in your subquery because you're not returning records from the subquery. Instead, the subquery just builds a list of IDs used to filter the main query.
If you use a subquery in a way that the subquery records are returned, these clauses are fine. For example, this works:
SELECT Name,
(SELECT FirstName, LastName FROM Contacts ORDER BY LastName LIMIT 10)
FROM Account
I think it's worth adding that with new features of SOQL that were not available when this question was first asked and answered, that the approach of querying from Case should now be viable (with access to custom fields).
In particular the TYPEOF feature provides access to type specific fields through polymorphic lookups: http://www.salesforce.com/us/developer/docs/soql_sosl/Content/sforce_api_calls_soql_select_typeof.htm
It is worth noting that this feature is still in Developer Preview.
SELECT
TYPEOF Owner
WHEN User THEN CustomField__c
END
FROM CASE
Did you try switching your query around to be something like this?
SELECT OwnerId, (select id from user)
FROM Case
WHERE Case.CaseNumber LIKE '%1026'
ORDER BY Case.CreatedDate DESC LIMIT 1

How do you parse a SOQL AggregateResult column with no value?

Let's say you run a SOQL aggregate query that looks like this:
select OwnerId, sum(ExpectedRevenue)val from Opportunity GROUP BY ROLLUP(OwnerId)
For whatever reason, there are no Opportunities with ExpectedRevenue fields populated.
You get a table that looks like this:
val___|OwnerId
|Id1
|Id2
|Id3
4/4 records.
(sidenote: how do you put in tabular data without it looking terrible?)
Take note that the "val" columns are all null and the last OwnerId column is also null.
There are 4 rows because SOQL returns a "total" row as well on rollups.
While looping through the AggregateResult[] that gets returned the code blows up on a line that looks like this: AggregateResult[0].get('val'); with a "System.NullPointerException: Attempt to de-reference a null object"
However, if just 1 of those users has some data, then the whole thing works. So I'm guessing that if no rows have any data in a particular column, that column does not exist at all and calls to retrieve it blow up.
So my question is how do you determine if a column exists to avoid the null reference error?
You have said that the ownerid column and the val columns are all null, therefore AggregateResult[0] is a pointer to a null object and any attempt to get a alue from that object give you the error you are having.
What I expect you want to be doing is before you run
AggregateResult[0].get('val');
you want to have an if statement say
if(AggregateResult.size() > 0)
or possibly
if(AggregateResult[0] != null)
to ensure that you are not attempting to access an empty object.
Try that and it should work. Otherwise post up a bigger code listing to look through.
Paul
If there is no data to summarize for the filters you specify in your where clause you'll get an empty list of AggregateResult back. You can test for this using list isEmpty() in Apex.

Resources