You need to sign in to do that
Don't have an account?
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
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
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
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.
unfortunately this doesnt work.
Have you tried changing Product__r to Products__r ?
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
Hi avidev9, yes i tried it but unfortunately it doesnt changed anything
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
thx avidev!!
the api name was different from the child relationship name.