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
Z3roZ3ro 

Roll Up summary on lookup trigger

Hi Guys,

 

i am currently on a trigger for a rollup summary.

I´ve 2 Objects

Invoice= Parent

Product= Child

 

This is the Code what i actually have

 

trigger ProductRollup on Product__c (after delete, after insert, after update) {
 
    Set<id> InvoiceIds = new Set<id>();
    List<Invoice__c> InvoicesToUpdate = new List<Invoice__c>();
 
    for (Product__c item : Trigger.new)
        InvoiceIds.add(item.Invoice__c);
 
    if (Trigger.isUpdate || Trigger.isDelete) {
        for (Product__c item : Trigger.old)
            InvoiceIds.add(item.Invoice__c);
    }

    Map<id,Invoice__c> InvoiceMap = new Map<id,Invoice__c>([select id, test__c from Invoice__c where id IN :InvoiceIds]);

    for (Invoice__c Inv : [select Id, Name, test__c,(select id from Product__r) from Invoice__c where Id IN :InvoiceIds]) {
        InvoiceMap.get(Inv.Id).test__c = Inv.Product__r.size();
        InvoicesToUpdate.add(InvoiceMap.get(Inv.Id));
    }
 
    update InvoicesToUpdate;
 
}

 

i have an error in line 18 with (select id from Product__r)

Didn't understand relationship 'Product__r' in FROM part of query call

 

I just copied the trigger and adjusted it to my objects.

Can anyone help me with that code?

I am fairly new to apex and any help is appreciated.

 

BR

Tarek

Best Answer chosen by Admin (Salesforce Developers) 
Avidev9Avidev9
Well seems like you have messed up the child relationship name.
Do one thing go to product >> Go to Lookup field >> Go to field detail >> There should be a child relationship name

We geenrally add "__r" to this child relationship name

All Answers

Avidev9Avidev9

Modify the query it should be something like

 

 [select Id, Name, test__c,(select id from Products__r) from Invoice__c where Id IN :InvoiceIds]

 

I guess you are missing 's' it should be  Products__r

Varun YagainVarun Yagain

Hi Tarek,

 

Please see if changing:

 

[select Id, Name, test__c,(select id from Product__r) from Invoice__c where Id IN :InvoiceIds])

 

to

 

[select Id, Invoice__r.Id, Invoice__r.Name, Invoice__r.test__c from Product__c where Invoice__r.Id IN :InvoiceIds])

 

 

works.

 

If this solves your problem, please mark it as a solution.

 

-Varun.

Z3roZ3ro

unfortunately this doesnt work.

Avidev9Avidev9

Have you tried changing Product__r to Products__r ?

Z3roZ3ro

Hi Varun,

 

this seems to work but now is the error msg, in line 18

 

InvoiceMap.get(Inv.Id).test__c = Inv.Product__r.size();

 

 

Error: Invalid field product__r for SObject Invoice__c    InvoiceRollUpproducts.trigger

Z3roZ3ro

Hi avidev9, yes i tried it but unfortunately it doesnt changed anything

Avidev9Avidev9
Well seems like you have messed up the child relationship name.
Do one thing go to product >> Go to Lookup field >> Go to field detail >> There should be a child relationship name

We geenrally add "__r" to this child relationship name
This was selected as the best answer
Z3roZ3ro

thx avidev!!

 

the api name was different from the child relationship name.