You need to sign in to do that
Don't have an account?
naveen reddy 68
Question triggers
I have 2 objects product(parent object) and price(child object) and the relationship is lookup .
I have a field amount in child object and i want the sum of amont field like Roll-up summery field.
How can i get this.
I have a field amount in child object and i want the sum of amont field like Roll-up summery field.
How can i get this.
Thanks
Lokesh
try bellow code, please let me know useful or not.
01trigger UpdateOrder on Child__c (after insert, after update, after delete, after undelete) {
02
03 List<Parent__c> ct = new List<Parent__c>();
04
05 Set<Id> custord = new Set<Id>();
06
07 if(Trigger.isDelete) {
08 for(Child__c test:Trigger.Old) {
09
10 custord.add(test.Parent__c);
11
12 }
13
14 }
15 else
16 if(Trigger.isUpdate) {
17
18 for(Child__c test:Trigger.New) {
19
20 custord.add(test.Parent__c);
21
22 }
23
24 for(Child__c test:Trigger.Old) {
25
26 custord.add(test.Parent__c);
27
28 }
29
30 }
31 else
32 {
33 for(Child__c test:Trigger.New) {
34
35 custord.add(test.Parent__c);
36
37 }
38 }
39
40 AggregateResult[] groupedResults = [SELECT COUNT(Id), Parent__c FROM Child__c where Parent__c IN:custord GROUP BY Parent__c ];
41
42 for(AggregateResult ar:groupedResults) {
43
44 Id custid = (ID)ar.get('Parent__c');
45
46 Integer count = (INTEGER)ar.get('expr0');
47
48 Parent__c cust1 = new Parent__c(Id=custid);
49
50 cust1.child_count__c = count;
51
52 ct.add(cust1);
53
54 }
55
56
57 update ct;
58
59}
Thanks & Regards
Gopal
Lets assume you have an parent object as ParentObject and childObject as childObject .
The below code must work.
Do modify this code to suit your requirement.Let me know if you face any further issues in the code.
Thanks and Regards,
Shiva RV
Try this. It should work.
1) trigger Parentcount on Books__c (after insert , after delete) {
List<Books__c> book = Trigger.isInsert ? Trigger.New : Trigger.old;
list<id> b = new list<id>();
for(Books__c bo : book)
{
b.add(bo.Callcountpar__c);
}
list<Parentobj__c> parent = [select id,(select id from Books__r),Call_Count1__c from Parentobj__c where id IN :b];
for(Parentobj__c par : parent)
{
par.Call_Count1__c = par.Books__r.size();
}
update parent;
}
or this way
2) trigger Rollup on Price__c (after insert, after update, after delete) {
set<Id> pro = new set<Id>();
list<Product__c> pro1 = new list<Product__c>();
for(Price__c p : Trigger.new)
{
pro.add(p.Id);
}
if(Trigger.isdelete || trigger.isupdate)
{
for(Price__c p : Trigger.old)
{
pro.add(p.id);
}
}
list<Product__c> pr = new list<Product__c> ([select id,Product_Name__c,price__c,rollup__c,(select id from Price__r) from Product__c where id IN : pro]);
for(Product__c p1 : pr)
{
pro1.add(p1);
pro1.rollup__c = pro1.Price__r.Size();
}
}
Try this, the below code works with the Account - Contact Relationship, where it captures total Number of Contacts for each Account
Regards,
Vivek C
Sorry i missed that amont field, if you want to display all the total amount values in Account. Where the total amount fields are calculated from the each account linked Contact.
Regards,
Vivek C