You need to sign in to do that
Don't have an account?
Rocks_SFDC
How to restrict the child records based on sum of field using Apex Trigger
Hi Developers,
I have product object and custom object(Designations__c). We have look up relationship between product and designation__c(Parent: Product, Child: Designation__c)
Fields:
On Product Object: Total Amount (Data Type:Currency)
On Designations Object: Amount(Data Type: Currency)
Now, The sum of amount of related designation records should not exceed 100 for an product record.
I want to applicable the functionality for Insert, Update and Delete operations.
Please help me out with the code.
Thanks,
Rocks
I have product object and custom object(Designations__c). We have look up relationship between product and designation__c(Parent: Product, Child: Designation__c)
Fields:
On Product Object: Total Amount (Data Type:Currency)
On Designations Object: Amount(Data Type: Currency)
Now, The sum of amount of related designation records should not exceed 100 for an product record.
I want to applicable the functionality for Insert, Update and Delete operations.
Please help me out with the code.
Thanks,
Rocks
Please check and let me knwo if you afce any issue..also you can refer below code for example...
please check adn let me know if it helpos you..
Thanks,
Sandeep
All Answers
You can write a trigger on before insert, before update and before delete...
you can simplly rollup the sum adn chck if it crosses the 100 then using addError you can show the error to prevent the insertion and updation of child record.
Please implement and let me know if you face any issue in implementing the same.
Thanks,
Sandeep
Thank you for your reply !!!!
I unable to show an error message. I have displayed sum sucessfully on the Product.
Here is my code:
In code: I took "Opportunity" is the parent object and "Payments" is the child object
trigger OpportunityRollUpPayments on Payment__c (after delete, after insert, after update) {
set<Id> OpportunityIds = new set<Id>();
if(trigger.isInsert || trigger.isUpdate){
for(Payment__c p : trigger.new){
OpportunityIds.add(p.Opportunity__c);
}
}
if(trigger.isDelete){
for(Payment__c p : trigger.old){
OpportunityIds.add(p.Opportunity__c);
}
}
map<Id,Double> OpportunityMap = new map<Id,Double> ();
for(AggregateResult q : [select Opportunity__c,sum(Amount__c)
from Payment__c where Opportunity__c IN :OpportunityIds group by Opportunity__c]){
OpportunityMap.put((Id)q.get('Opportunity__c'),(Double)q.get('expr0'));
}
List<Opportunity> OpportunitiesToUpdate = new List<Opportunity>();
for(Opportunity o : [Select Id, Total_Payments__c from Opportunity where Id IN :OpportunityIds]){
Double PaymentSum = OpportunityMap.get(o.Id);
o.Total_Payments__c = PaymentSum;
OpportunitiesToUpdate.add(o);
}
update OpportunitiesToUpdate;
}
Could you please check and help me out in displaying error message on "Payments" Object through the trigger itself.
Thanks,
Anil
You can make use of addError ..please check below link ho whe is throwing error for all records..
from your code you can come to know which childs should not proceed with insert and update so you can simply iterate them adn show the eror
https://developer.salesforce.com/forums/?id=906F00000008zcBIAQ
Please check and let me know if you face any issue..
Thanks,
Sandeep
Please check and let me knwo if you afce any issue..also you can refer below code for example...
please check adn let me know if it helpos you..
Thanks,
Sandeep