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
Vinu VargheseVinu Varghese 

I am getting the following soql query in trigger. would you help me to trace the error.

trigger InvoiceTotal on Invoice_Item__c (after insert, after update) {
    set<ID> setId = new set<ID>();
    List<invoice__c> lstInvoice = new List<invoice__c>();
    
    for(Invoice_Item__c fndId :trigger.new){
        setId.add(fndId.invoice__c);
    }
    
    for(invoice__c parentInvoice :[select id, name, invoice_no__c, (select id, invoice__c, Total_Price__c, name  from Invoice_Item__c) from Invoice__c where Invoice__c.id in :setId]){
        Invoice__c Inv = new Invoice__c();
        inv.id = parentInvoice.id;
        inv.Total_amount__c =parentInvoice.Total_price__c;
        lstInvoice.add(inv);
    }
    update lstInvoice;
}
Best Answer chosen by Vinu Varghese
Agustin BAgustin B
Hi Vinu, great we are making progress, please like the comments in which you are making progress this way we get rewarded for helping.

You have to grab whats inside of the relationship, like this:
  for(invoice__c parentInvoice :[select id, name, invoice_no__c, (select id, invoice__c, Total_Price__c, name  from invoice_Items) from Invoice__c where id in :setId]){
        Invoice__c Inv = new Invoice__c();
        inv.id = parentInvoice.id;
For(Invoice_Item__c item : parentInvoice.invoice_items__r){ 
//here you can acces the field with item.Total_amount__c 
}
        lstInvoice.add(inv);
    }
    update lstInvoice;

You could sum every total of the children, I dont know if it is your objective:
 inv.Total_amount__c +=item.Total_amount__c
 

All Answers

Agustin BAgustin B
HI, which is the error you are getting.
You could use Invoice__c Inv = new Invoice__c(Id=parentInvoice.id); instead of 
Invoice__c Inv = new Invoice__c();
        inv.id = parentInvoice.id;

But please share the error so I can be of more assistance.

If this helps please like and if it solves your issue mark as correct as it may help others.
Vinu VargheseVinu Varghese
the following are the errors:

​​​​​​invoice__c, Total_Price__c, name from Invoice_Item__c) from Invoice__c ^ ERROR at Row:1:Column:84 Didn't understand relationship 'Invoice_Item__c' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the ‘__r’ after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.
 
Agustin BAgustin B
Hi Vinu, alright, verify on the Invoice_Item__c the lookup should have a child relationship name. That is the one you should use in the query instead of Invoice_Item__c.
For example if you use a subquery of Account to search their contacts you dont use From Contact, you use From Contacts, as that is the relationship name defined on the custom relationship field.
Vinu VargheseVinu Varghese
Thank you. I used the Child relationship name : Invoice_Items. Bust still I have same errors

trigger InvoiceTotal on Invoice_Item__c (after insert, after update) {
    set<ID> setId = new set<ID>();
    List<invoice__c> lstInvoice = new List<invoice__c>();
    
    for(Invoice_Item__c fndId :trigger.new){
        setId.add(fndId.invoice__c);
    }
    
    for(invoice__c parentInvoice :[select id, name, invoice_no__c, (select id, invoice__c, Total_Price__c, name  from invoice_Items) from Invoice__c where id in :setId]){
        Invoice__c Inv = new Invoice__c();
        inv.id = parentInvoice.id;
        inv.Total_amount__c =parentInvoice.Total_price__c;
        lstInvoice.add(inv);
    }
    update lstInvoice;
}
Agustin BAgustin B
Hi, add the __r at the end to say it is a relationship.
[select id, name, invoice_no__c, (select id, invoice__c, Total_Price__c, name  from invoice_Items__r) from Invoice__c where id in :setId]
If it helps please like and mark as correct, it may help others.

Regards.
Vinu VargheseVinu Varghese
It works. but now the problem is that it says that 
Line 12       inv.Total_amount__c =parentInvoice.Total_price__c;

Total_price__c is in the inner loop as you can see in the query. 

Variable does not exist : Total_price__c 
 
Agustin BAgustin B
Hi Vinu, great we are making progress, please like the comments in which you are making progress this way we get rewarded for helping.

You have to grab whats inside of the relationship, like this:
  for(invoice__c parentInvoice :[select id, name, invoice_no__c, (select id, invoice__c, Total_Price__c, name  from invoice_Items) from Invoice__c where id in :setId]){
        Invoice__c Inv = new Invoice__c();
        inv.id = parentInvoice.id;
For(Invoice_Item__c item : parentInvoice.invoice_items__r){ 
//here you can acces the field with item.Total_amount__c 
}
        lstInvoice.add(inv);
    }
    update lstInvoice;

You could sum every total of the children, I dont know if it is your objective:
 inv.Total_amount__c +=item.Total_amount__c
 
This was selected as the best answer
Vinu VargheseVinu Varghese
It worked. Thank you.