function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Arnaud CombesArnaud Combes 

lookup field in apex and report

Hi everyone,

I'm trying to create a report with some custom objects and I can't manage to do it, certainly because of an issue in my apex code...

To explain, I have created 3 custom objects : Quote, Items and Delivery.

The object « Quote » always has a record, and for 1 record in « Quote », there can be 0 to n records in Items and in Delivery.
So Quote is like a Parent with 2 children : Items ans Delivery.

I have added a lookup relation on Quote to Items and Delivery, and also created a Lookup on Items and Delivery to Quote.
Then I have created a new report, with Primary object : Quote, and in Edit layout, I have added fields of Items and Delivery thanks to « Add fields related via lookup ».

But now, when I run my report, it is empty… If I only display Quote fields I have some records, but when I add fields from Items or Delivery, I have no record in my report.

Maybe, I don’t insert data as it has to be done in Apex.
I do it like this : 

Quote__c insertQuote = new Quote__c();
insertQuote.ECOM_CustomerId__c = ‘abcd’;
insert insertQuote;
Item__c insertQuoteItem = new Item__c();
insertQuoteItem.Quantity__c = 1;
insertQuoteItem.Quote__r = insertQuote;
insert insertQuoteItem;

But on different forums, I see as I need to use Quote__r, and sometimes Quote__c = Id…
Is there something wrong ? I completely lost, and can’t see what is wrong !!

I just begin with salesforce, and even with the help I found on forums, I didn't manage to fix my issue.
Don't hesitate to ask me if it is not clear !
Thank you very much for you help.

Regards,

 
Best Answer chosen by Arnaud Combes
Antonio ManenteAntonio Manente
When assigning it, use:
 
insertQuoteItem.Quote__c = insertQuote.Id

That should properly assign it. The field Quote__c will give you the Id of the related quote. You can use Quote__r when querying if you'd like a field of the quote returned: 

For example:
 
QuoteLine q = [SELECT Id, Quote__c FROM QuoteLine];


QuoteLine l = [SELECT Id, Quote__r.Id FROM QuoteLine];

In these two examples, q.Quote__c == l.Quote__r.Id ( excuse the object names, this is just for demonstration )

I'd recommend checking out the workbook and going through some of the tutorials ( https://developer.salesforce.com/docs/atlas.en-us.apex_workbook.meta/apex_workbook/ ) They can really help alot

All Answers

Antonio ManenteAntonio Manente
When assigning it, use:
 
insertQuoteItem.Quote__c = insertQuote.Id

That should properly assign it. The field Quote__c will give you the Id of the related quote. You can use Quote__r when querying if you'd like a field of the quote returned: 

For example:
 
QuoteLine q = [SELECT Id, Quote__c FROM QuoteLine];


QuoteLine l = [SELECT Id, Quote__r.Id FROM QuoteLine];

In these two examples, q.Quote__c == l.Quote__r.Id ( excuse the object names, this is just for demonstration )

I'd recommend checking out the workbook and going through some of the tutorials ( https://developer.salesforce.com/docs/atlas.en-us.apex_workbook.meta/apex_workbook/ ) They can really help alot
This was selected as the best answer
Arnaud CombesArnaud Combes
Thank you very much Antonio, it works now !!!
 
Antonio ManenteAntonio Manente
Awesome, please mark a best answer if your problem was solved